当我按下1按钮时,我希望所有其他按钮改变状态

时间:2017-11-14 18:06:22

标签: java swing jpanel jbutton actionlistener

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

但是当我按下按钮时,我希望所有OTHER按钮都使用方法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);
}
}

我考虑了一些解决方案,例如在actionlistener中添加一个新的CMain class,这是我为此写的代码:

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创建按钮时,我可以将所有按钮保存在列表中,然后在ToggleState()中创建一个新的CMain,切换列表中未按下的每个按钮,但是我不知道这样的解决方案在code中会是什么样的。

编辑: 我尝试(在评论部分提示后)从actionListener删除MyButton class并将其放入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();
            }
    }
});

1 个答案:

答案 0 :(得分: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();
}
相关问题