繁体   English   中英

为什么我的按钮没有显示?

[英]Why aren't my buttons showing up?

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

    }
}`

 mortCalc.add(BorderLayout.SOUTH, buttons);

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

您只需检查一下最终将按钮添加到何处或包含按钮的容器即可。

检查您将JButtons添加到-按钮的JPanel
然后检查您要添加的按钮-panelMort2
然后检查将panelMort2添加到的内容-什么都没有。

这不过是基本逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM