2011-11-24 105 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]); 
      } 


} 
}