如何向JButton添加两个动作?提示计算器程序

时间:2017-09-09 22:17:57

标签: java swing jbutton actionlistener jlabel

我正在尝试创建一个简单的小费计算器程序,但是,我对一般的动作听众有些新意,所以我不确定我做错了什么。 我希望按钮更新num的值,将其格式化为字符串,然后更改标签的文本(显示用户输入的内容(即帐单金额))。

这是我的代码:

package calculatorCW;

import java.awt.Color;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.ParseException;
import javax.swing.*;

public class calGui {
    double tip = 0;
    String num = "000.00", output1 = "00.00", output2 = "00.00";
    JFrame frame = new JFrame();
    JPanel myPanel = new JPanel();
    JLabel label1, label2, label3, label4;
    JTextField myText1, myText2;
    JButton buttons, tBtn1, tBtn2, tBtn3, calc, btn0;
    DecimalFormat formatter = new DecimalFormat();
    private int i = 1;

    public calGui() {
        initializeLabels();
        initializeTextFields();
        initializeButtons();
        setPanels();
        setBounds();
        guiExtras();
        setActionListeners();
    }

    public static void main(String[] args) {
        new calGui();
    }

    public void initializeLabels() {

        label1 = new JLabel("<html> <p style='font-size: 15px; color: red;'> TIP CALCULATOR </p></html>");
        label2 = new JLabel("<html> <p style='color: white;'> Bill Amount:</p></html> ");
        label3 = new JLabel("<html> <p style='color: white;'>Tip Percentage: </p></html>");
        label4 = new JLabel("<html> <p style='color: white;'>Total Cost: </p></html>");
    }

    public void initializeTextFields() {
        myText1 = new JTextField(output1);
        myText2 = new JTextField(output2);
    }

    public void initializeButtons() {
        tBtn1 = new JButton("<html> <p style='color: blue;'>15 %</p></html>");
        tBtn2 = new JButton("<html> <p style='color: blue;'>18 %</p></html>");
        tBtn3 = new JButton("<html> <p style='color: blue;'>20 %</p></html>");
        calc = new JButton("<html> <p style='color: black;'>CALC</p></html>");
        btn0 = new JButton("0");
        //Buttons Loop
        for (int col = 50; col <= 190; col = col + 70) {
            for (int row = 253; row <= 393; row = row + 70) {
                buttons = new JButton("" + i);
                myPanel.add(buttons);
                buttons.setBounds(row, col, 60, 50);
                buttons.setActionCommand(String.valueOf(i));
                i++;
            }
        }
    }

    public void setPanels() {
        myPanel.add(tBtn1);
        myPanel.add(tBtn2);
        myPanel.add(tBtn3);
        myPanel.add(calc);
        myPanel.add(btn0);
        myPanel.add(label1);
        myPanel.add(label2);
        myPanel.add(label3);
        myPanel.add(label4);
        myPanel.add(myText1);
        myPanel.add(myText2);
    }

    public void setBounds() {
        tBtn1.setBounds(23, 90, 60, 50);
        tBtn2.setBounds(93, 90, 60, 50);
        tBtn3.setBounds(163, 90, 60, 50);
        calc.setBounds(13, 190, 100, 50);
        btn0.setBounds(323, 260, 60, 50);
        label1.setBounds(155, -5, 200, 50);
        label2.setBounds(13, 50, 100, 20);
        label3.setBounds(13, 70, 200, 20);
        label4.setBounds(13, 160, 100, 20);
        myText1.setBounds(96, 50, 100, 20);
        myText2.setBounds(96, 160, 100, 20);
    }

    public void guiExtras() {
        myPanel.setLayout(null);
        myPanel.setSize(500, 400);
        frame.add(myPanel);
        frame.setBounds(600, 50, 500, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myText1.setEditable(false);
        myText2.setEditable(false);
        //myPanel.setOpaque(true);
        myPanel.setBackground(Color.black);
    }

    public void setActionListeners() {
        //bill amounts
        buttons.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                num = num + (e.getActionCommand());
            }
        });

        buttons.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    output1 = formatter.format(formatter.parse(num));
                    myText1.setText(output1);
                } catch (ParseException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        //tips
        tBtn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tip = .15;
            }
        });
        tBtn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tip = .18;
            }
        });
        tBtn3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tip = .20;
            }
        });
    }
}

3 个答案:

答案 0 :(得分:3)

您目前将ActionListener添加到&#39;按钮&#39;这只是对在&quot; initializeButtons()&#39;中创建的按钮的临时参考。呼叫&#39; setActionListeners&#39;然后将ActionListener添加到初始化按钮时创建的最后一个按钮。

我建议不要在按钮上使用类变量,而是使用局部变量按钮。

您可以通过在初始化按钮时添加ActionListener来解决此问题:

public void initializeButtons() {
    ...
    //Buttons Loop
    for (int col = 50; col <= 190; col = col + 70) {
        for (int row = 253; row <= 393; row = row + 70) {
            JButton button = new JButton("" + i);
            // bill amounts
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    num = num + (e.getActionCommand());
                    try {
                        output1 = formatter.format(formatter.parse(num));
                        myText1.setText(output1);
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                    }
                }
            });
            ...
        }
    }
}

答案 1 :(得分:1)

你可以在同一个监听器上做两件事

button.addActionListener(new Actionlistener{
public void actionPerformed(ActionEvent ae){
 //add code to update value of num
//change text of Jlabel
}
});

答案 2 :(得分:0)

我的新问题是:我的代码现在有两个问题。第一个问题是,当我按任意数字按钮添加账单金额时,它不会以我想要的方式显示在账单金额textFeild中。如果用户输入9.我希望它显示“000.09”。如果现在用户输入5我想要账单金额现在显示“000.95”。我理解我的数学如果我在Button动作监听器中设置它的方式错了。我遇到的第二个问题是我添加了一个重置​​按钮,并希望它将textFields中的值重置回原始值。在setResetListener()方法中,我为重置按钮创建了一个动作侦听器,以便它将双精度值的值转换回原始值。我希望它然后显示在textField上。但是,它没有这样做。我也希望它将myPanel的背景重新着色回原来的黑色。我猜这是由于线程的某些东西?这是我到目前为止的代码。

$(x).is('input[type=hidden]')