gpt4 book ai didi

java - 图像未在 Java 应用程序中显示

转载 作者:行者123 更新时间:2023-12-02 00:45:21 26 4
gpt4 key购买 nike

有人可以指出我正确的方向吗?我试图根据单击的按钮显示我的图形图像,但没有显示任何内容。我没有收到任何错误,因此我假设图像文件已正确读取,并且问题很可能出在我的 Jpanel 代码中。

//import all needed functionality
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
public class Calculator extends JApplet implements ActionListener{

private static final long serialVersionUID = 1L;

// Declare variables and put code application logic here

String userInput = null;
JLabel loanAmountLabel = new JLabel("Loan Amount: ");
JTextField loanAmount = new JTextField();
double[] ratesList = {0.0535, 0.055, 0.0575};
JLabel rateLabel=new JLabel("Interest Rate: ");
JTextField rate=new JTextField();
String[] yearsList = {"7","15","30"};
JLabel yearsLabel=new JLabel("Years of Payment: ");
JTextField years=new JTextField();
JLabel chooseLabel=new JLabel("Choose a yearly term: ");
JRadioButton sevenButton = new JRadioButton("7 years at 5.35%");
JRadioButton fifteenButton = new JRadioButton("15 years at 5.5%");
JRadioButton thirtyButton = new JRadioButton("30 years at 5.75%");
JLabel pictureLabel=new JLabel("Graph:");
JLabel picture;
JLabel payLabel=new JLabel("Monthly Payment: ");
JLabel payment=new JLabel();
JButton calculate=new JButton("Calculate");
JButton clear=new JButton("Clear");
JButton quit=new JButton("Quit");
JTextArea payments=new JTextArea();
JScrollPane iterations=new JScrollPane(payments);
Container mortCalc = getContentPane();


public void init() {
//Configure the radio buttons to input data into fields
sevenButton.setActionCommand("Radio1");
fifteenButton.setActionCommand("Radio2");
thirtyButton.setActionCommand("Radio3");

ButtonGroup chooseYears = new ButtonGroup();
chooseYears.add(sevenButton);
chooseYears.add(fifteenButton);
chooseYears.add(thirtyButton);

//Register a listener for the radio buttons.
sevenButton.addActionListener(this);
fifteenButton.addActionListener(this);
thirtyButton.addActionListener(this);

//Set up the picture label.
picture = new JLabel(createImageIcon("images/"
+ years.getText()
+ ".gif"));
picture.setPreferredSize(new Dimension(356, 290));

//Config GUI
JPanel panelMort=new JPanel();
panelMort.setLayout(new GridLayout(1,2));
panelMort.add(loanAmountLabel);
panelMort.add(loanAmount);

JPanel panelMort0=new JPanel();
panelMort0.setLayout(new GridLayout(1,2));
panelMort0.add(chooseLabel);
panelMort0.add(sevenButton);
panelMort0.add(fifteenButton);
panelMort0.add(thirtyButton);

JPanel panelMort1=new JPanel();
panelMort1.setLayout(new GridLayout(3,2));
panelMort1.add(yearsLabel);
panelMort1.add(years);
panelMort1.add(rateLabel);
panelMort1.add(rate);
panelMort1.add(payLabel);
panelMort1.add(payment);

JPanel panelMort3=new JPanel();
panelMort3.setLayout(new GridLayout(1,2));
panelMort3.add(pictureLabel);
panelMort3.add(picture);

JPanel buttons=new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
buttons.add(calculate);
buttons.add(clear);
buttons.add(quit);

JPanel panelMort2=new JPanel();
panelMort2.setLayout(new BoxLayout(panelMort2, BoxLayout.Y_AXIS));
panelMort2.add(panelMort);
panelMort2.add(panelMort0);
panelMort2.add(panelMort1);
panelMort2.add(panelMort3);
panelMort2.add(buttons);

mortCalc.add(BorderLayout.NORTH, panelMort2);
mortCalc.add(BorderLayout.CENTER, iterations);

}

private Icon createImageIcon(String string) {
// TODO Auto-generated method stub
return null;
}

public void actionPerformed(ActionEvent e) {
if ("Radio1".equals(e.getActionCommand())) {
years.setText("7");
rate.setText("0.0535");
}
if ("Radio2".equals(e.getActionCommand())) {
years.setText("15");
rate.setText("0.055");
}
if ("Radio3".equals(e.getActionCommand())) {
years.setText("30");
rate.setText("0.0575");
}
{
picture.setIcon(createImageIcon("images/"
+ e.getActionCommand()
+ ".png"));
}


calculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Perform the calculation

double principalCalc=Double.parseDouble(loanAmount.getText());
double rateCalc=Double.parseDouble(rate.getText())/12;
double yearsCalc=Integer.parseInt(years.getText())*12;

double monthlyPayment=principalCalc*Math.pow(1+rateCalc,yearsCalc)*rateCalc/(Math.pow(1+rateCalc,yearsCalc)-1);

DecimalFormat df = new DecimalFormat("$###,###.00");
payment.setText(df.format(monthlyPayment));

// Perform extra calculations to show the loan amount after each subsequent payoff
double principal=principalCalc;
int month;
StringBuffer buffer=new StringBuffer();
buffer.append("Month\tAmount\tInterest\tBalance\n");
for (int f=0; f<yearsCalc; f++) {
month=f+1;
double interest=principal*rateCalc;
double balance=principal+interest-monthlyPayment;
buffer.append(month+"\t"); buffer.append(new String(df.format(principal))+"\t");
buffer.append(new String(df.format(interest))+"\t");buffer.append(new String(df.format(balance))+"\n");
principal=balance;
}
payments.setText(buffer.toString());
} catch(Exception ex) {
System.out.println(ex);
}
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loanAmount.setText(""); payment.setText(""); payments.setText("");
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});



}
public static void main(String[] args) {
JApplet applet = new Calculator();
JFrame frameMort = new JFrame("Calculator");
frameMort.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frameMort.getContentPane().add(applet);
frameMort.setSize(550,800);

frameMort.setResizable(true);
frameMort.setVisible(true);

applet.init();
applet.start();

}
}

最佳答案

这应该是一个很大的提示..

private Icon createImageIcon(String string) {
// TODO Auto-generated method stub
return null;
}

OTOH,鉴于这是一个 aplet,您将希望避免任何基于字符串的图像构造函数。请改用 URL,从...获取 URL。

URL urlToImageOnClassPath = this.getClass().getResource("path/to/image.png");
<小时/>

编辑1:如果图像位于小程序的运行时类路径上,则 getResource() 方法将起作用。也就是说,它位于 applet Jar 或 list 或 applet 存档属性中列出的其他 Jars 之一中。

如果情况并非如此,并且图像是服务器上的松散资源,则可以从相对 URL 以及代码库或文档库中获得形成 URL 的其他方法。例如

URL urlToImageOnServer = new URL(applet.getDocumentBase(), "../files/image.png");

关于java - 图像未在 Java 应用程序中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5125948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com