事件中JTextArea中没有显示任何文本

时间:2011-11-24 22:02:19

标签: java swing

我不确定如何修复我的错误。我使用它时运行正常,没有错误,但是当我点击其中一个按钮时没有输入显示。 我正在使用eclipse与JDK 1.7.1和Windows 7。 代码基本上是使用TextArea创建3个按钮,单击时显示每个按钮的输入。

public class Main implements ActionListener{

public JTextArea text;
public JButton b1;
public JButton b2;
public JButton b3;
public String choices[] = {"Rock", "Paper", "Scissors"};

public static void main(String[] args){
    Main gui = new Main();
    gui.go();
}

public void go(){
    JFrame frame = new JFrame("Rock Paper Scissors");
    text = new JTextArea(13,20);
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JButton b1 = new JButton(choices[0]);
    JButton b2 = new JButton(choices[1]);
    JButton b3 = new JButton(choices[2]);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    text.setEditable(false);


    JScrollPane scroller = new JScrollPane(text);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    panel1.add(scroller);
    panel2.add(b1);
    panel2.add(b2);
    panel2.add(b3);

    frame.getContentPane().add(BorderLayout.CENTER,panel1);
    frame.getContentPane().add(BorderLayout.SOUTH, panel2);
    frame.setSize(350,300);
    frame.setVisible(true);


}

public void actionPerformed(ActionEvent e) {
     if (e.getSource() == b1) {
         text.setText("Your choice was" + choices[0]);
            }
     if (e.getSource() == b2) {
         text.setText("Your choice was" + choices[1]);
            }
     if (e.getSource() == b3) {
         text.setText("Your choice was" + choices[2]);
            }


}
}

5 个答案:

答案 0 :(得分:3)

您永远不会初始化实例变量b1,b2和b3。所以equals运算符不起作用,因为实例变量仍为null。我建议你在go()方法中,不要创建新的局部变量b1,b2和b3,而是初始化实例变量。我改变了你的代码:

public void go(){
    JFrame frame = new JFrame("Rock Paper Scissors");
    text = new JTextArea(13,20);
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    b1 = new JButton(choices[0]);
    b2 = new JButton(choices[1]);
    b3 = new JButton(choices[2]);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    text.setEditable(false);


    JScrollPane scroller = new JScrollPane(text);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    panel1.add(scroller);
    panel2.add(b1);
    panel2.add(b2);
    panel2.add(b3);

    frame.getContentPane().add(BorderLayout.CENTER,panel1);
    frame.getContentPane().add(BorderLayout.SOUTH, panel2);
    frame.setSize(350,300);
    frame.setVisible(true);


}

答案 1 :(得分:1)

您正在创建影响实例变量的局部变量:

JButton b1 = new JButton(choices[0]);
JButton b2 = new JButton(choices[1]);
JButton b3 = new JButton(choices[2]);

答案 2 :(得分:1)

这是问题所在:

JButton b1 = new JButton(choices[0]);
JButton b2 = new JButton(choices[1]);
JButton b3 = new JButton(choices[2]);

这是声明三个新的 local 变量,它们遮蔽了您的实例变量,而不是为现有变量赋值。因此,实例变量保持为null,您的检查如下:

if (e.getSource() == b1)

永远不会评估为真。

只需将这些行更改为:

b1 = new JButton(choices[0]);
b2 = new JButton(choices[1]);
b3 = new JButton(choices[2]);

我试过这个(在添加各种导入之后)并解决了这个问题。

答案 3 :(得分:1)

使用

JButton b1 = new JButton(choices[0]);
JButton b2 = new JButton(choices[1]);
JButton b3 = new JButton(choices[2]);

在函数内部,您可以在当前方法scrope中定义这些变量,因此如果在变量之前删除定义,程序将按预期工作

b1 = new JButton(choices[0]);
b2 = new JButton(choices[1]);
b3 = new JButton(choices[2]);

答案 4 :(得分:0)

我确信即使我没有对其进行测试,此代码也能100%运行:

public class Main implements ActionListener{

public JTextArea text;
public JButton b1;
public JButton b2;
public JButton b3;
public String choices[] = {"Rock", "Paper", "Scissors"};

public static void main(String[] args){
    Main gui = new Main();
    gui.go();
}
public void go(){
    JFrame frame = new JFrame("Rock Paper Scissors");
    text = new JTextArea(13,20);
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    b1 = new JButton(choices[0]);
    b2 = new JButton(choices[1]);
    b3 = new JButton(choices[2]);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    text.setEditable(false);


    JScrollPane scroller = new JScrollPane(text);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    panel1.add(scroller);
    panel2.add(b1);
    panel2.add(b2);
    panel2.add(b3);

    frame.getContentPane().add(BorderLayout.CENTER,panel1);
    frame.getContentPane().add(BorderLayout.SOUTH, panel2);
    frame.setSize(350,300);
    frame.setVisible(true);


}

public void actionPerformed(ActionEvent e) {
     if (e.getSource().equals(b1)) {
         text.setText("Your choice was" + choices[0]);
            }
     if (e.getSource().equals(b2)) {
         text.setText("Your choice was" + choices[1]);
            }
     if (e.getSource().equals(b3)) {
         text.setText("Your choice was" + choices[2]);
            }


}
}