退休计算器

时间:2014-10-05 22:27:05

标签: java swing

我坚持完成了我的任务。 信息: 我的任务是制作一个GUI退休年龄计算器。 我已经用所需的文本字段制作了gui。 我的问题是接受用户输入并单击按钮并让按钮从我的退休课程中进行计算。 我的按钮什么也没做。 以及我的清除按钮。

public class ViewGui extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = -628756272392439536L;
    private JPanel contentPane;
    private JTextField age;
    private JTextField rAge;
    private JTextField preTB;
    private JTextField postTB;
    private JTextField capGain;
    private JTextField iTax;
    private JTextField preTc;
    private JTextField postTC;
    private JTextField savings;
    private JTextField total;
    private Retirement retire;
    private JTextField rateR;

    /**
     * Create the frame.
     */
    public ViewGui() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        age = new JTextField();
        age.setBounds(21, 29, 86, 20);
        contentPane.add(age);
        age.setColumns(10);

        rAge = new JTextField();
        rAge.setBounds(21, 72, 86, 20);
        contentPane.add(rAge);
        rAge.setColumns(10);

        preTB = new JTextField();
        preTB.setBounds(21, 115, 86, 20);
        contentPane.add(preTB);
        preTB.setColumns(10);

        postTB = new JTextField();
        postTB.setBounds(21, 157, 86, 20);
        contentPane.add(postTB);
        postTB.setColumns(10);

        capGain = new JTextField();
        capGain.setBounds(168, 29, 86, 20);
        contentPane.add(capGain);
        capGain.setColumns(10);

        iTax = new JTextField();
        iTax.setBounds(168, 72, 86, 20);
        contentPane.add(iTax);
        iTax.setColumns(10);

        preTc = new JTextField();
        preTc.setBounds(168, 115, 86, 20);
        contentPane.add(preTc);
        preTc.setColumns(10);

        postTC = new JTextField();
        postTC.setBounds(168, 157, 86, 20);
        contentPane.add(postTC);
        postTC.setColumns(10);

        savings = new JTextField();
        savings.setBounds(21, 201, 86, 20);
        contentPane.add(savings);
        savings.setColumns(10);

        JLabel lblAge = new JLabel("Age");
        lblAge.setBounds(40, 11, 46, 14);
        contentPane.add(lblAge);

        JLabel lblRetirementAge = new JLabel("Retirement Age");
        lblRetirementAge.setBounds(21, 60, 98, 14);
        contentPane.add(lblRetirementAge);

        JLabel lblPostTaxBalance = new JLabel("Post Tax Balance");
        lblPostTaxBalance.setBounds(21, 145, 86, 14);
        contentPane.add(lblPostTaxBalance);

        JLabel lblPreTaxBalance = new JLabel("Pre Tax Balance");
        lblPreTaxBalance.setBounds(21, 103, 86, 14);
        contentPane.add(lblPreTaxBalance);

        JLabel lblCapitalGain = new JLabel("Capital Gain");
        lblCapitalGain.setBounds(169, 11, 71, 14);
        contentPane.add(lblCapitalGain);

        JLabel lblIncomeTax = new JLabel("Income Tax");
        lblIncomeTax.setBounds(168, 60, 86, 14);
        contentPane.add(lblIncomeTax);

        JLabel lblPreTaxCollection = new JLabel("Pre Tax Collection");
        lblPreTaxCollection.setBounds(168, 103, 104, 14);
        contentPane.add(lblPreTaxCollection);

        JLabel lblPostTaxCollection = new JLabel("Post Tax Collection");
        lblPostTaxCollection.setBounds(166, 145, 116, 14);
        contentPane.add(lblPostTaxCollection);

        JLabel lblSavings = new JLabel("Savings");
        lblSavings.setBounds(21, 188, 46, 14);
        contentPane.add(lblSavings);

        JButton Calculate = new JButton("Go!");
        Calculate.setBounds(165, 188, 89, 23);
        contentPane.add(Calculate);

        JButton Clear = new JButton("Clear");
        Clear.setBounds(165, 222, 89, 23);
        contentPane.add(Clear);

        total = new JTextField();
        total.setBounds(282, 29, 142, 211);
        contentPane.add(total);
        total.setColumns(10);

        JLabel lblRetirement = new JLabel("Retirement");
        lblRetirement.setBounds(323, 11, 71, 14);
        contentPane.add(lblRetirement);

        rateR = new JTextField();
        rateR.setBounds(21, 242, 86, 20);
        contentPane.add(rateR);
        rateR.setColumns(10);

        JLabel lblRateOfReturn = new JLabel("Rate of Return");
        lblRateOfReturn.setBounds(21, 226, 86, 14);
        contentPane.add(lblRateOfReturn);
    }


    public class Calculate implements ActionListener
    {
        public void actionPerformed(ActionEvent arg1)
        {

            retire = new Retirement(age.getText(), rAge.getText(),  preTB.getText(),  postTB.getText(), savings.getText(),
                     capGain.getText(), iTax.getText(), preTc.getText(), postTC.getText(), rateR.getText());
            total.setText("Year" +  retire.getAge() + retire.getITax());
        }
    }  


public class ClearButton implements ActionListener
    {

        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
        try
        {
            age.setText(" ");
            rAge.setText(" ");
            preTB.setText(" ");
            postTB.setText(" ");
            capGain.setText(" ");
            iTax.setText(" ");
            preTc.setText(" ");
            postTC.setText(" ");
            savings.setText(" ");
            total.setText(" ");
        }
            catch(Exception e)
        {
                age.setText("Error: "  + e.getMessage());
                rAge.setText("Error: "  + e.getMessage());
                preTB.setText(" Error: "  + e.getMessage());
                postTB.setText(" Error: "  + e.getMessage());
                capGain.setText(" Error: "  + e.getMessage());
                iTax.setText(" Error: "  + e.getMessage());
                preTc.setText(" Error: "  + e.getMessage());
                postTC.setText(" Error: "  + e.getMessage());
                savings.setText(" Error: "  + e.getMessage());
                total.setText(" Error: "  + e.getMessage());
        }
        }
    }
    }               

1 个答案:

答案 0 :(得分:1)

您创建了ActionListner,但是忘了将它们添加到按钮中! 试试这个:

Clear.addActionListener(new ClearButton());

Calculate.addActionListener(new Calculate());