为什么我的按钮不显示?

时间:2011-02-15 03:48:54

标签: java swing

在Eclipse中作为测试应用程序运行:

`    //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 ScrofaniWk3 extends JApplet{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * @param args
     */
        // 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();
        JComboBox chooseYears = new JComboBox(yearsList);
        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 schedulePane=new JScrollPane(payments);
        Container mortCalc = getContentPane();


        public void init() {
        //This makes the chooseYears function
        chooseYears.setSelectedIndex(0);
        chooseYears.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent choose) {
        JComboBox cb = (JComboBox)choose.getSource();
        String termYear = (String)cb.getSelectedItem();
        years.setText(termYear);
        int index=0;
        switch (Integer.parseInt(termYear)) {
        case 7: index=0; break;
        case 15: index=1; break;
        case 30: index=2; break;
        }
        rate.setText(ratesList[index]+"");
        }
        });
        calculate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {    
                    // Perform the calculation
                    double rateCalc=Double.parseDouble(rate.getText())/12;
                    double principalCalc=Double.parseDouble(loanAmount.getText());
                    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("$###,###.##");
                    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);
            }
        });     
        //Config GUI
        JPanel panelMort=new JPanel();
        panelMort.setLayout(new GridLayout(5,2));
        panelMort.add(loanAmountLabel); panelMort.add(loanAmount);  
        panelMort.add(yearsLabel); panelMort.add(years);
        panelMort.add(new Label()); panelMort.add(chooseYears);
        panelMort.add(rateLabel); panelMort.add(rate);
        panelMort.add(payLabel); panelMort.add(payment);
        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(buttons);
        mortCalc.add(BorderLayout.NORTH, panelMort);
        mortCalc.add(BorderLayout.CENTER, schedulePane);
    }
    public static void main(String[] args) {
        JApplet applet = new ScrofaniWk3();
        JFrame frameMort = new JFrame("ScrofaniWk3");
        frameMort.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frameMort.getContentPane().add(applet);
        frameMort.setSize(500,1000);

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

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

    }
}`

2 个答案:

答案 0 :(得分:1)

添加

 mortCalc.add(BorderLayout.SOUTH, buttons);

mortCalc.add(BorderLayout.NORTH, panelMort);
mortCalc.add(BorderLayout.CENTER, schedulePane);

答案 1 :(得分:0)

您只需检查最终添加按钮的位置或按住按钮的容器。

检查JPanel添加JButtons到哪个按钮的{{1}} 然后检查添加按钮的内容 - panelMort2
然后检查你添加panelMort2的内容 - 什么都没有。

这只不过是基本逻辑。