2

请在下面查看我附加的代码,然后在最后查看我的问题。

class events extends JFrame{
    private JLabel label;
    private JButton button;

    public events() {
        setLayout(new FlowLayout());

        button = new JButton("Click for text");
        add(button);

        label = new JLabel("");
        add(label);

        event e = new event();
        button.addActionListener(e);

    }

    public class event implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int x = 0;
            if (x == 0) {

                label.setText("the new label");
                System.out
                        .println("setting x to 0 and label to display a label");
                x = 1;
                System.out.println(x);
            } else {
                label.setText("newerer label");
                System.out.println("i reached the else segment");

                x = 0;
                System.out.println(x);
            }

        }

    }

    public static void main(String args[]) {
        events gui = new events();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setTitle("Events Test");
        gui.setSize(300, 100);
        gui.setVisible(true);

    }

}

背景:我正在学习java并试图理解一个概念。使用这个程序,我试图创建一个带有按钮的小 gui,单击该按钮会为 JLabel 分配“新标签”的字符串值。但是,如果再次单击,我想使用相同的按钮将标签更改为“较新的标签”,如果单击第三次,则再次返回。我尝试使用带有变量 x 的 If/Else 语句来执行此操作,以基本上保持状态 1 或 0。在 If/Else 的每个部分结束时,我将 x 的状态适当地更改为 1 或 0。但是,当尝试在 Eclipse 中运行该程序时,我遇到了某种错误。我分配了一个system.out。

问题:

  1. If/Else 语句是否适合执行这种简单的 2 状态切换?
  2. 有没有更合适的方法来做到这一点?(我知道 switch 语句,但选择了这个,因为它只是一个两个状态的项目)。
  3. 我做错了什么,为什么当状态应该为 1 时我的 Else 路径从未实现?

谢谢你的回复,帕诺

4

4 回答 4

3

您的变量“x”必须声明为类成员。

public class event implements ActionListener{
    private int x = 0;

    public void actionPerformed(ActionEvent e){
        if(x == 0){
            label.setText("the new label");
            System.out.println("setting x to 0 and label to display a label");
            x = 1;
            System.out.println(x);
        }
        else {
            label.setText("newerer label");
            System.out.println("i reached the else segment");
            x = 0;
            System.out.println(x);
        }
    }
}
于 2013-04-19T20:20:59.043 回答
1

x每次调用动作侦听器时,您都在进行初始化,因此x始终为 0。移动x到其他地方,可能将其声明为类成员。

于 2013-04-19T20:20:47.963 回答
0
  1. Yes, I personally would use an if/else statement here, although if you are likely to add more states a switch statement may be appropriate, but I think given you have more than 1 line of code in the if/else blocks I would keep it as if/else as it has better readability in my experience.

  2. I guess my answer to 1 covers this!

  3. The reason your else block isn't being reached is that your x variable is only in scope (only exists) inside that method. As soon as you leave the method the value is no longer in scope so as far as your code in concerned it no longer exists. The code P Lalonde posted shows the correct way: having the variable as a member variable (object scope). Read more about member variable scope here: http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

于 2013-04-19T20:25:24.473 回答
0

如果您只是在寻找状态切换,您实际上可以执行以下操作:

if( label.getText().equals("the new label") ) {
  label.setText("newerer label");
} else {
  label.setText("the new label");
}
于 2013-04-19T20:27:33.833 回答