[英]How do I put an ActionListener on a JButton created with an ArrayList of Jbuttons
[英]How do I remove the border from multiple JButtons attached to one ActionListener (Created with for loop)?
我正在创建一个使用虚拟键盘进行输入的子手游戏。 我使用for循环创建了键盘布局,以创建按钮,然后将其放入JButton ArrayList中。 所有按钮都使用一个ActionListener。
在游戏结束时,我需要重置所有按钮,以使它们不再具有边框(如果玩家猜对了,则在游戏过程中会在它们周围放置绿色或红色边框)。 我试图遍历JButton ArrayList来更改边框,但是它仅对最后按下的按钮起作用。 有办法可以做到吗?
我还想知道如何将按钮恢复为原始设计,因为当我使用button.setBorder(null)
它会使按钮基本上消失了。 我想删除我在程序前面设置的MatteBorder
。
public void createKeyboard() {
String buttonLabel;
char buttonLabelChar;
for (char c = 'A'; c <= 'Z'; c ++) {
buttonLabelChar = c;
buttonLabel = "" + buttonLabelChar;
JButton button = new JButton(buttonLabel);
keyboardArray = new ArrayList<JButton>();
keyboardArray.add(button);
button.setPreferredSize(new Dimension(40, 40));
pnlKeyboard.add(button);
button.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
hm.setLetterGuessed(event.getActionCommand().charAt(0));
hm.revealLetter();
if (hm.revealLetter()) {
button.setBorder(new MatteBorder(4, 4, 4, 4, Color.GREEN));
button.setEnabled(false);
}
else {
button.setBorder(new MatteBorder(4, 4, 4, 4, Color.RED));
button.setEnabled(false);
hm.loseLife();
setImage();
}
if (hm.winCheck()) {
JOptionPane.showMessageDialog(mainFrame, "You win!", "Game Over",
JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Hangman.class.getResource("images/11rightleg.png")));
for (int i = 0; i < keyboardArray.size(); i ++) { // THIS DOESN'T WORK
button.setBorder(null);
}
}
else if (hm.getLives() == 0) {
JOptionPane.showMessageDialog(mainFrame, "You lose :(", "Game Over",
JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Hangman.class.getResource("images/11rightleg.png")));
}
setWord();
}
}
);
}
}
一种选择是完全避免使用匿名类,以便侦听器可以在createKeyboard()
方法外部使用keyboardArray
:
ArrayList<JButton> keyboardArray;
public void createKeyboard() {
String buttonLabel;
char buttonLabelChar;
for (char c = 'A'; c <= 'Z'; c ++) {
// ...
keyboardArray = new ArrayList<JButton>();
keyboardArray.add(button);
// ...
button.addActionListener(new ButtonListener());
}
}
class ButtonListener {
public void actionPerformed(ActionEvent event) {
hm.setLetterGuessed(event.getActionCommand().charAt(0));
hm.revealLetter();
if (hm.revealLetter()) {
button.setBorder(new MatteBorder(4, 4, 4, 4, Color.GREEN));
button.setEnabled(false);
} else {
button.setBorder(new MatteBorder(4, 4, 4, 4, Color.RED));
button.setEnabled(false);
hm.loseLife();
setImage();
}
if (hm.winCheck()) {
JOptionPane.showMessageDialog(mainFrame, "You win!", "Game Over", JOptionPane.INFORMATION_MESSAGE, new ImageIcon( Hangman.class.getResource("images/11rightleg.png")));
for (int i = 0; i < keyboardArray.size(); i ++) { // THIS DOESN'T WORK
keyboardArray.get(i).setBorder(null);
}
} else if (hm.getLives() == 0) {
JOptionPane.showMessageDialog(mainFrame, "You lose :(", "Game Over",
JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Hangman.class.getResource("images/11rightleg.png")));
}
setWord();
}
}
我发现了问题。 我必须在构造函数中创建keyboardArray ArrayList对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.