如何使这个JFrame代码更简单?

时间:2018-06-03 20:29:54

标签: java swing

我刚开始学习JFrame,我试着制作一个简单的计算器。它可以工作,但它也是重复的,必须为每个按钮创建一个ActionListener。有没有办法让它更简单?

public class gui extends JFrame
{
    private JButton one;
    private JButton two;
    private JButton three;
    private JButton four;
    private JButton five;
    private JButton six;
    private JButton seven;
    private JButton eight;
    private JButton nine;
    public double first  = 0;
    public double second = 0;
    public double sum    = 0;

    public gui() 
    {   
        super("title");
        setLayout(new FlowLayout());

        one   = new JButton("1"); //makes a new button to click.
        two   = new JButton("2");
        three = new JButton("3");
        four  = new JButton("4");
        five  = new JButton("5");
        six   = new JButton("6");
        seven = new JButton("7");
        eight = new JButton("8");
        nine  = new JButton("9");
        one.addActionListener( //MAKES NEW INNER CLASS TO DEFINE WHAT ONE DOES
            new ActionListener() 
            {
                public void actionPerformed(ActionEvent event) 
                { 
                    // checks if clicked. 
                    if (first == 0) first = 1;
                    else second = 1;

                    if (first != 0 && second != 0) 
                    {
                        sum = first + second;
                        System.out.println(sum);
                        first = 0;
                        second = 0;
                    }
                }
            });
        two.addActionListener( 

                new ActionListener() {

                    public void actionPerformed(ActionEvent event) { 
                        if(first == 0) {
                        first = 2;
                        }else {
                            second = 2;
                        }
                        if(first != 0 && second != 0) {
                            sum = first + second;
                            System.out.println(sum);
                            first = 0;
                            second = 0;
                        }
                    }
                }
            );
        three.addActionListener( 

                new ActionListener() {

                    public void actionPerformed(ActionEvent event) { 
                        if(first == 0) {
                        first = 3;
                        }else {
                            second = 3;
                        }
                        if(first != 0 && second != 0) {
                            sum = first + second;
                            System.out.println(sum);
                            first = 0;
                            second = 0;
                        }
                    }
                }
            );
        four.addActionListener(

                new ActionListener() {

                    public void actionPerformed(ActionEvent event) {
                        if(first == 0) {
                        first = 4;
                        }else {
                            second = 4;
                        }
                        if(first != 0 && second != 0) {
                            sum = first + second;
                            System.out.println(sum);
                            first = 0;
                            second = 0;
                        }
                    }
                }
            );
        five.addActionListener(

                new ActionListener() {

                    public void actionPerformed(ActionEvent event) { 
                        if(first == 0) {
                        first = 5;
                        }else {
                            second = 5;
                        }
                        if(first != 0 && second != 0) {
                            sum = first + second;
                            System.out.println(sum);
                            first = 0;
                            second = 0;
                        }
                    }
                }
            );
        six.addActionListener( 

                new ActionListener() {

                    public void actionPerformed(ActionEvent event) { 
                        if(first == 0) {
                        first = 6;
                        }else {
                            second = 6;
                        }
                        if(first != 0 && second != 0) {
                            sum = first + second;
                            System.out.println(sum);
                            first = 0;
                            second = 0;
                        }
                    }
                }
            );
        seven.addActionListener(

                new ActionListener() {

                    public void actionPerformed(ActionEvent event) { 
                        if(first == 0) {
                        first = 7;
                        }else {
                            second = 7;
                        }
                        if(first != 0 && second != 0) {
                            sum = first + second;
                            System.out.println(sum);
                            first = 0;
                            second = 0;
                        }
                    }
                }
            );
        eight.addActionListener(

                new ActionListener() {

                    public void actionPerformed(ActionEvent event) { 
                        if(first == 0) {
                        first = 8;
                        }else {
                            second = 8;
                        }
                        if(first != 0 && second != 0) {
                            sum = first + second;
                            System.out.println(sum);
                            first = 0;
                            second = 0;
                        }
                    }
                }
            );
        nine.addActionListener( 

                new ActionListener() {

                    public void actionPerformed(ActionEvent event) { 
                        if(first == 0) {
                        first = 9;
                        }else {
                            second = 9;
                        }
                        if(first != 0 && second != 0) {
                            sum = first + second;
                            System.out.println(sum);
                            first = 0;
                            second = 0;

                        }
                    }
                }
            );

        add(one);
        add(two);
        add(three);
        add(four);
        add(five);
        add(six);
        add(seven);
        add(eight);
        add(nine);

    }

}

此外,我找不到一种方法来“合并”我拥有的两个数字。如果用户按下9和2,我想将这两个数字组合成92.我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以做的一件事就是为所有按钮使用一个ActionListener。您可以在每个按钮上设置actionCommand属性,动作侦听器将使用该属性来检测按下了哪个按钮。

以下是一般说明:

private ActionListener btnListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        switch (event.getActionCommand()) {
            case "0": 
            case "1": 
               // ...
            case "9":
                enterDigit(event.getActionCommand());
                break;                    
        }
    }
};

public gui(){
    one = new JButton("1");
    one.setActionCommand("1");
    one.addActionListener(btnListener);
    two = new JButton("2");
    two.setActionCommand("2");
    two.addActionListener(btnListener);
    // ...
}

至于如何组合单独的数字按钮以形成更大的数字,可能有多种方式,具体取决于您希望如何存储数字。
最简单的方法是使用String开始,在这种情况下,您只需将包含输入数字(来自String的actionCommand)的JButton附加到现有String上。 1}}先前输入的数字:

private String numberInDisplay = "";

// remember enterDigit from the ActionListener above?  This is it...
private void enterDigit(String digit){
    numberInDisplay = numberInDisplay + digit;
}

如果您想使用数字类型,例如long,您只需将现有数字乘以10并添加新数字的值:

private long numberInDisplay = 0;
private void enterDigit(String digit){ 
    numberInDisplay = numberInDisplay * 10 + Long.valueOf(digit); 

当然这是一个相当简化的例子。如果你想处理小数点,负号,科学记数法等,会有一些额外的复杂情况,但它至少应该让你开始走正确的道路。