将多个计算分配给一个JButton

时间:2019-03-06 03:30:51

标签: java button graphics jbutton

我需要使用一个按钮“计算”来运行(使用其他未显示的正确运行的字段)三个单独的计算。如何使此按钮全部打印3个?它将运行,但仅打印最后一个。是否以某种方式刷新并仅打印一个值?谢谢!

JLabel lblTipPerPerson = new JLabel("Tip Per Person");
    JTextField tfTipPerPerson = new JTextField(20);
    lblTipPerPerson.setLabelFor(tfTipPerPerson);

    btnCalculate.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    double TipPerPerson;
                    TipPerPerson = ( (Double.parseDouble(tfTipPercent.getText()) / 100.0) * 
                            Double.parseDouble(tfBillAmount.getText()) )  / 
                            Double.parseDouble(tfNumberofPeople.getText());
                    tfTipPerPerson.setText(String.format("$ %.2f", TipPerPerson));
                }
            }
            );

    JLabel lblTotalPerPerson = new JLabel("Total Per Person");
    JTextField tfTotalPerPerson = new JTextField(20);
    lblTotalPerPerson.setLabelFor(tfTotalPerPerson);

    btnCalculate.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    double TotalPerPerson;
                    TotalPerPerson = Double.parseDouble(tfTipPerPerson.getText()) + 
                            ( Double.parseDouble(tfBillAmount.getText()) / 
                            Double.parseDouble(tfNumberofPeople.getText()) );
                    tfTotalPerPerson.setText(String.format("$ %.2f", TotalPerPerson));
                }
            }
            );


    JLabel lblBillTotal = new JLabel("Bill Total");
    JTextField tfBillTotal = new JTextField(20);
    lblBillTotal.setLabelFor(tfBillTotal);

    btnCalculate.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    double BillTotal;
                    BillTotal = Double.parseDouble(tfBillAmount.getText()) *
                            ( 1 + Double.parseDouble(tfTipPercent.getText()) / 100.0 );
                    tfBillTotal.setText(String.format("$ %.2f", BillTotal));
                }
            }
            );

0 个答案:

没有答案