现在使用下面的代码,我的输出是 10 个带有文本/颜色的按钮。当我按下按钮时,它使用方法 ToggleState()
来更改其文本/颜色。
但是当我按下一个按钮时,我希望所有其他按钮都使用方法 ToggleState()
。所以基本上,我希望按下的按钮保持相同的文本/颜色,而所有其他按钮更改文本/颜色。
MyButton 类
:
public class MyButton extends JButton implements ActionListener {
private Color col1;
private Color col2;
private String text1;
private String text2;
public MyButton(Color col1, Color col2, String text1, String text2) {
this.col1 = col1;
this.col2 = col2;
this.text1 = text1;
this.text2 = text2;
this.setText(text1);
this.setBackground(col1);
this.setOpaque(true);
this.addActionListener(this);
}
public void ToggleState() {
Color initialBackground = this.getBackground();
if (initialBackground == col1) {
this.setBackground(col2);
this.setText(text2);
} else if (initialBackground == col2) {
this.setBackground(col1);
this.setText(text1);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this) {
this.ToggleState();
}
}
}
CMain 类
:
public class CMain{
private static JPanel panel = new JPanel();
public static void main(String[] args) {
Random randomGenerator = new Random();
JFrame frame = new JFrame("MyButton testing");
for (int i = 0; i < 10; i++) {
float r = randomGenerator.nextFloat();float g = randomGenerator.nextFloat();float b = randomGenerator.nextFloat();float r2 = randomGenerator.nextFloat();float g2 = randomGenerator.nextFloat();float b2 = randomGenerator.nextFloat();
String theText = "button nr: " + i;
String theOtherText = "SWITCH ME";
Color theColor = new Color(r, g, b);
Color theOtherColor = new Color(r2, g2, b2);
MyButton myb = new MyButton(theColor, theOtherColor, theText, theOtherText);
panel.add(myb);
}
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我考虑了一些解决方案,例如在CMain类
中添加一个新的actionlistener
,这是我为此编写的代码:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this) {
for (int j=0 ; j < panel.getComponentCount() ; j++) {
((CMain)panel.getComponent(j)).ToggleState();
}
}
}
但这并没有解决我的问题,我的输出与以前完全相同。当我按下 1 个按钮时,只有该按钮被切换。我知道上面的代码会 ToggleState()
我的所有按钮(而不仅仅是我没有按下的按钮),我只是想尝试一下。
也许当我在 CMain 类
中创建按钮时,我可以将所有按钮保存在列表中,然后在 CMain
中创建一个新的 ToggleState()
code> 会切换列表中未按下的每个按钮,但我不知道这样的解决方案在 code
中会是什么样子。
编辑: 我尝试(在评论部分的提示之后)从我的 MyButton 类中删除 actionListener 并将其放入 CMain 类中,问题是,它切换我的所有按钮,包括我按下的按钮。我现在更接近了,但我仍然需要按下的按钮保持不变:
ArrayList<MyButton> knappar = new ArrayList<MyButton>();
MyButton myb = new MyButton(theColor, theOtherColor, theText, theOtherText);
knappar.add(myb);
panel.add(myb);
myb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == knappar);
for (int k=0; k<knappar.size();k++) {
knappar.get(k).ToggleState();
}
}
});
最佳答案
lost with the if statement, not sure how I should set it up.
if 语句需要位于循环内部。
for (int k=0; k<knappar.size();k++)
{
MyButton button = knappar.get(k);
if (button != e.getSource())
button.toggleState();
}
另外,请注意我如何重命名该方法。方法名称不应以大写字符开头。
或者您使用以下循环语法:
for (MyButton button: knapper)
{
if (button != e.getSource())
button.toggleState();
}
关于java - 当我按下 1 个按钮时,我希望所有其他按钮都改变状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47292516/