动作侦听器的行为就像我单击每个按钮而不是仅单击 1 个
Action listener behaving like I click every single button instead of just 1
我是 Java GUI 编程的新手,我已经开始编写一个程序,该程序具有带有多个 JButton 的 GridBagLayout GUI。我已经为每个按钮添加了一个动作侦听器,但是当我单击任何一个按钮时,程序的响应就像我单击了每个按钮一样,包括甚至不在 GUI 上的按钮。我已经在下面发布了我的代码的相关部分。整个程序很长,所以我将下面的代码限制为仅处理 JButton 的部分。另请注意,我只想使用 1 actionPerformed() 方法,并测试源而不是在每个 addActionListener() 之后直接使用 actionPerformed() 方法。
buttons = new JButton[buttonNumber];
for (int i = 0; i < buttonNumber; i++)
{
buttons[i] = new JButton();
}
然后,在我准备好 srting[] 对象 buttonLabels 之后:
for (int i = 0; i < buttonNumber; i++)
{
buttons[i].setText(buttonLabels[i]);
}
然后,稍后:
for(int i = 0; i < buttonNumber; i++)
{
if(buttonLabels[i] != null && i < 18)
{
decorator.positionButtons(i, writer.currentNumberOfButtons);
c.gridx = decorator.xPosition;
c.gridy = decorator.yPosition;
c.gridwidth = 1;
Dimension d = new Dimension(buttonWidth, decorator.buttonHeight);
buttons[i].setPreferredSize(d);
buttons[i].addActionListener(this);
windowContents.add(buttons[i], c);
}
if(i == 18)
{
c.gridx = 0;
c.gridy = (decorator.yPosition + 1);
buttons[i].addActionListener(this);
windowContents.add(buttons[i], c);
}
else if (i == 19)
{
c.gridx = 2;
c.gridy = (decorator.yPosition + 1);
buttons[i].addActionListener(this);
windowContents.add(buttons[i], c);
}
}
最后:
public void actionPerformed(ActionEvent e)
{
boolean buttonClicked = false;
int buttonIndex = -1;
for(int i = 0; i < buttonNumber; i++)
{
if (e.getSource() == buttons[i]);
{
buttonIndex = i;
buttonClicked = true;
System.out.println("Here at button" + buttonIndex");
}
}
}
该程序最初只在屏幕上放置 6 个按钮,当我点击其中任何一个按钮时,我得到的结果是:
Here at button0
Here at button1
Here at button2
Here at button3
Here at button4
Here at button5
Here at button6
Here at button7
Here at button8
Here at button9
Here at button10
Here at button11
Here at button12
Here at button13
Here at button14
Here at button15
Here at button16
Here at button17
Here at button18
Here at button19
if (e.getSource() == buttons[i]);
// remove the semi-colon here ---^
我是 Java GUI 编程的新手,我已经开始编写一个程序,该程序具有带有多个 JButton 的 GridBagLayout GUI。我已经为每个按钮添加了一个动作侦听器,但是当我单击任何一个按钮时,程序的响应就像我单击了每个按钮一样,包括甚至不在 GUI 上的按钮。我已经在下面发布了我的代码的相关部分。整个程序很长,所以我将下面的代码限制为仅处理 JButton 的部分。另请注意,我只想使用 1 actionPerformed() 方法,并测试源而不是在每个 addActionListener() 之后直接使用 actionPerformed() 方法。
buttons = new JButton[buttonNumber];
for (int i = 0; i < buttonNumber; i++)
{
buttons[i] = new JButton();
}
然后,在我准备好 srting[] 对象 buttonLabels 之后:
for (int i = 0; i < buttonNumber; i++)
{
buttons[i].setText(buttonLabels[i]);
}
然后,稍后:
for(int i = 0; i < buttonNumber; i++)
{
if(buttonLabels[i] != null && i < 18)
{
decorator.positionButtons(i, writer.currentNumberOfButtons);
c.gridx = decorator.xPosition;
c.gridy = decorator.yPosition;
c.gridwidth = 1;
Dimension d = new Dimension(buttonWidth, decorator.buttonHeight);
buttons[i].setPreferredSize(d);
buttons[i].addActionListener(this);
windowContents.add(buttons[i], c);
}
if(i == 18)
{
c.gridx = 0;
c.gridy = (decorator.yPosition + 1);
buttons[i].addActionListener(this);
windowContents.add(buttons[i], c);
}
else if (i == 19)
{
c.gridx = 2;
c.gridy = (decorator.yPosition + 1);
buttons[i].addActionListener(this);
windowContents.add(buttons[i], c);
}
}
最后:
public void actionPerformed(ActionEvent e)
{
boolean buttonClicked = false;
int buttonIndex = -1;
for(int i = 0; i < buttonNumber; i++)
{
if (e.getSource() == buttons[i]);
{
buttonIndex = i;
buttonClicked = true;
System.out.println("Here at button" + buttonIndex");
}
}
}
该程序最初只在屏幕上放置 6 个按钮,当我点击其中任何一个按钮时,我得到的结果是:
Here at button0
Here at button1
Here at button2
Here at button3
Here at button4
Here at button5
Here at button6
Here at button7
Here at button8
Here at button9
Here at button10
Here at button11
Here at button12
Here at button13
Here at button14
Here at button15
Here at button16
Here at button17
Here at button18
Here at button19
if (e.getSource() == buttons[i]);
// remove the semi-colon here ---^