开发者

Images Not Showing in Java App

can someone 开发者_开发知识库please point me in the right direction? I'm trying to get my graph images to show up based on what button is clicked, but nothing is showing. I'm not getting any errors, so I'm assuming that the image files are being read properly, and that the problem is most likely in my Jpanel code.

//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();

}
}


This should be a big hint..

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

OTOH, given this is an aplet, you will want to avoid any String based constructors for images. Use URLs instead, get the URL from..

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

Edit 1: The getResource() method will work if the image is on the run time class path of the applet. That is, it is in the applet Jar or one of the other Jars listed in the manifest or applet archive attribute.

If that is not the case & instead the image is a loose resource on the server, other ways to form URLs can be had from a relative URL & either the codebase or document base. E.G.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜