【问题标题】:When I press 1 button, I want all other buttons to change states当我按下 1 个按钮时,我希望所有其他按钮更改状态
【发布时间】:2017-11-14 18:06:21
【问题描述】:

现在使用下面的代码,我的输出是 10 个带有文本/颜色的按钮。当我按下一个按钮时,它使用ToggleState() 方法来改变它的'文本/颜色。

但是当我按下一个按钮时,我希望所有其他按钮都使用方法ToggleState()。所以基本上,我希望按下的按钮保持相同的文本/颜色,而所有其他按钮都可以更改文本/颜色。

MyButton class:

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 class:

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 class 中添加一个新的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 class 中创建按钮时,也许我可以将所有按钮保存在一个列表中,然后在CMain 中创建一个新的ToggleState() 来切换列表中未按下的每个按钮,但我不知道如何类似的解决方案可以查看 code

编辑: 我尝试(在评论部分提示后)从我的MyButton class 中删除actionListener 并将其放入CMain class,问题是它会切换我的所有按钮,包括我按下的那个。我现在更近了,但我仍然需要我按下的按钮保持不变:

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();
            }
    }
});

【问题讨论】:

  • 让那个监听器 out 离开 MyButton。创建一个按钮集合 (ArrayList),并让侦听器通过 for 循环更改按钮的状态,遍历集合。
  • @HovercraftFullOfEels 抱歉,我的编码速度还是有点慢。检查我编辑的帖子,感谢您的意见!
  • 添加的代码有效吗?我自己,我会创建一个 ActionListener 并将其添加到所有按钮中,无论是在创建时,还是在遍历集合的 for 循环中(在它被按钮填充之后)。
  • 编辑:我看到您编辑的文本:您需要确定按下了哪个按钮。为此使用e.getSource()。简单的。在 actionPerformed 的 for 循环中,更改所有按钮除了源按钮(一个 if 测试)。
  • 我对@9​​87654340@ 语句有点迷茫,不知道应该如何设置。

标签: java swing jpanel jbutton actionlistener


【解决方案1】:

迷失在 if 语句中,不知道该如何设置。

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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多