首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何让这段JFrame代码更简单?

如何让这段JFrame代码更简单?
EN

Stack Overflow用户
提问于 2018-06-04 04:29:54
回答 1查看 41关注 0票数 0

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

代码语言:javascript
复制
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。我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-04 05:12:24

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

下面是一个通用的插图:

代码语言:javascript
复制
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);
    // ...
}

至于如何组合不同的数字按钮来形成更大的数字,可能有几种方法,这取决于你想要存储数字的方式。

开始使用Strings可能是最简单的,在这种情况下,您只需将包含输入数字(来自JButton的actionCommand )的String附加到先前输入数字的现有String上:

代码语言:javascript
复制
private String numberInDisplay = "";

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

如果要使用数字类型,如long,只需将现有数字乘以十,然后将新数字的值相加:

代码语言:javascript
复制
private long numberInDisplay = 0;
private void enterDigit(String digit){ 
    numberInDisplay = numberInDisplay * 10 + Long.valueOf(digit); 

当然,这是一个相当简单的例子。如果您想要处理小数点、负号、科学记数法等,将会有一些额外的复杂性,但它至少应该让您沿着正确的道路开始。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50670688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档