[英]Java - I can't get two actionListeners to work right
這是我代碼的ActionListener部分。 專注於重置按鈕。
public void actionPerformed( ActionEvent e) {
int i;
for (i = 0; i < 26; i++) {
if (e.getSource() == a[i]) {
consultWord(i);
}
}
if (e.getSource() == reset) {
Hangman gui = new Hangman();
System.out.print("test");
gui.go();
}
}
顯然,它上面還有更多的內容(因為這是非常極端的)。 按鈕數組1(if語句頂部)可以完美工作。 按鈕2(if語句下方)根本不起作用。 測試輸出文本不會出現。 這是我聲明變量的地方。 (它們可用於所有代碼)。
JButton reset = new JButton("Reset");
private Button a[];
如果對您來說有任何意義,這是設置a []按鈕的代碼。
int i;
StringBuffer buffer;
a = new Button[26];
topPanel.setLayout( new GridLayout( 4,0, 10, 10) );
for (i = 0; i <26; i++) {
buffer = new StringBuffer();
buffer.append((char)(i+'a'));
a[i] = new Button(buffer.toString());
a[i].setSize(100,100);
a[i].addActionListener( this );
topPanel.add(a[i]);
}
有什么想法為什么我的底部按鈕沒有下蹲嗎? 如果需要,我將粘貼整個代碼。
也許您只是忘記了將ActionListener
添加到您的reset
按鈕中? 上面的代碼中缺少此信息...
附帶說明一下一些使代碼更整潔的建議:
StringBuffer
:只需使用String.valueOf((char)(i+'a'))
ActionListener
,因為這會使您的actionPerformed
方法混亂。 匿名內部類在這里可能很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.