2011-11-24 103 views
1

我不确定如何解决此问题。当我使用它时,它运行良好,没有错误,但是当我点击其中一个按钮时没有输入。 我使用JDK 1.7.1和Windows 7中的eclipse。 代码基本上是创建3个按钮,其中TextArea显示每个按钮单击时的输入。在事件中没有显示在JTextArea中的文本

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]); 
      } 


} 
} 

回答

3

你永远不会初始化实例变量b1,b2和b3。所以等号运算符将不起作用,因为实例变量仍然为空。我建议在你的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

为了澄清它是否不明显,在方法内声明'JButton b1'作为局部变量隐藏'b1'作为实例变量。 – Bruno

+0

是的。我会将其添加到答案中。 – RoflcoptrException

+0

谢谢。我现在明白它是如何工作的 – Streak324

1

你创建本地变量,影子实例变量:

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

这就是问题所在:

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

这是宣布三个新地方影响实例变量的变量,而不是将值分配给现有变量。因此实例变量保持为空,并且您的支票如下所示:

if (e.getSource() == b1) 

永远不会计为true。

简单地改变这些行:

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

我已经试过这(加入各种进口后),它解决了这个问题。

1

通过使用

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

自己的函数中,您可以定义当前方法斯克罗普内这些变量,因此你的程序将工作打算,如果你的变量使之前删除定义,它

b1 = new JButton(choices[0]); 
b2 = new JButton(choices[1]); 
b3 = new JButton(choices[2]); 
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]); 
      } 


} 
}