有人可以指出我正确的方向吗?我正在尝试根据点击的按钮显示我的图形图像,但没有显示任何内容。我没有收到任何错误,所以我假设图像文件正在被正确读取,并且问题很可能出现在我的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();
}
}
答案 0 :(得分:3)
这应该是一个很大的提示..
private Icon createImageIcon(String string) {
// TODO Auto-generated method stub
return null;
}
OTOH,鉴于这是一个aplet,你会想要避免任何基于String的图像构造函数。请改用URL,从..获取URL
URL urlToImageOnClassPath = this.getClass().getResource("path/to/image.png");
编辑1: 如果图像位于applet的运行时类路径上,则getResource()方法将起作用。也就是说,它位于applet Jar或清单或applet存档属性中列出的其他Jars之一。
如果情况并非如此&amp;相反,图像是服务器上的松散资源,其他形成URL的方法可以来自相对URL&amp;代码库或文档库。 E.G。
URL urlToImageOnServer = new URL(applet.getDocumentBase(), "../files/image.png");