Java-我可以';我不能让两个ActionListener正常工作
下面是我代码的ActionListener部分。关注重置按钮Java-我可以';我不能让两个ActionListener正常工作,java,actionlistener,Java,Actionlistener,下面是我代码的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) { Ha
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();
}
}
如果这对您有什么意义,下面是设置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]);
}
inti;
字符串缓冲区;
a=新按钮[26];
topPanel.setLayout(新网格布局(4,0,10,10));
对于(i=0;i可能您只是忘记了将ActionListener
添加到reset
按钮?上面的代码中缺少此项
作为旁注,有一些建议可以使代码更简洁:
- 不需要
StringBuffer
:只需使用String.valueOf((char)(i+'a'))
- 我不会对您所有的按钮使用相同的
ActionListener
,因为这会扰乱您的actionPerformed
方法。在这里可能很有用
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]);
}