单击时将JButton循环三种颜色?

时间:2015-03-17 07:06:37

标签: java jbutton actionlistener

如何点击JButton颜色以循环为绿色,黄色,白色?如果我点击它必须变为黄色,再次点击然后再白色,再次点击然后变为绿色等等。

public void mouseClicked(MouseEvent e) 
{
     countClick++;       
    switch (countClick) 
    {
        case 0:
            m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.yellow);
            break;
        case 1:
            m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.yellow);
            break;

        case 2:
            m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.red);
            countClick =0;
    }

}

如果按钮已经是黄色,那么我必须点击两次。 注意: Button存储在数组列表中。我用loop将它添加到网格布局中。从文本文件中读取彩色。这就是我在GridlLayout上添加JBUtton的方法。这是另一堂课。

 btn = new ArrayList<>();

        while((text = br.readLine())!=null)
        {
            tmp=text.split(",");

            for(int i=0; i<tmp.length;i++)
            {
                System.out.print(tmp[i]);

                switch (tmp[i]) 
                {
                    case "0":
                        btn.add( i,new JButton() );
                        btn.get(i).setBackground(Color.GRAY);
                        btn.get(i).setEnabled(false);
                        newPanel.add(btn.get(i));
                        break;
                    case "1":
                        btn.add( i,new JButton("A") );
                        btn.get(i).addMouseListener(new controller(this));
                        newPanel.add(btn.get(i));
                        break;
                    case "2":
                        btn.add( i,new JButton("G") );
                        btn.get(i).setBackground(Color.GREEN);
                        btn.get(i).addMouseListener(new controller(this));
                        newPanel.add(btn.get(i));
                        break;
                    case "3":
                        btn.add( i,new JButton("Y") );
                        btn.get(i).setBackground(Color.YELLOW);
                        btn.get(i).addMouseListener(new controller(this));
                        newPanel.add(btn.get(i));
                        break;
                    case "4":
                        btn.add( i,new JButton("R") );
                        btn.get(i).setBackground(Color.RED);
                        btn.get(i).addMouseListener(new controller(this));
                        newPanel.add(btn.get(i));
                        break;
                }
            }
        }

3 个答案:

答案 0 :(得分:1)

你可以试试这个

private int countClicks =0;

   public void actionPerformed(ActionEvent e) {
         countClicks++;
         switch (countClicks){
              case 0:
                  boutton.setBackground(Color.YELLOW);
                  break;
              case 1:
                  boutton.setBackground(Color.WHITE);
                  break;
              case 2:
                  boutton.setBackground(Color.GREEN);
                  countClicks=0;
                  break;
     }

答案 1 :(得分:0)

我会这样做,因为它可以让你轻松改变颜色并重新使用&#34;换色器&#34;与其他按钮。

public  class ButtonBackgroundColorChanger implements ActionListener {
    private int actualColorIndex = 0;
    private Color[] colors;

    public ButtonBackgroundColorChanger(Color... colors) {
        if (colors.length < 1) {
            throw new IllegalArgumentException(
                    "At least one color must be provided");
        }
        this.colors = colors;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        AbstractButton abstractButton = (AbstractButton) e.getSource();

        Color nextColor = nextColor();
        abstractButton.setBackground(nextColor);
    }

    private Color nextColor() {
        Color actualColor = colors[actualColorIndex++];
        actualColorIndex = actualColorIndex % colors.length;
        return actualColor;
    }

}

然后使用它,例如

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame();

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new FlowLayout(FlowLayout.CENTER));

        ButtonBackgroundColorChanger buttonBackgroundColorChanger1 = new ButtonBackgroundColorChanger(
                Color.YELLOW, Color.WHITE, Color.GREEN);
        AbstractButton button1 = new JButton("click to change color");
        contentPane.add(button1);
        button1.addActionListener(buttonBackgroundColorChanger1);

        ButtonBackgroundColorChanger buttonBackgroundColorChanger2 = new ButtonBackgroundColorChanger(
                Color.BLUE, Color.CYAN, Color.MAGENTA);
        AbstractButton button2 = new JButton("click to change color");
        contentPane.add(button2);
        button2.addActionListener(buttonBackgroundColorChanger2);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

答案 2 :(得分:0)

@Override
public void mouseClicked(MouseEvent e) 
{
    if(Color.green==( m.btn.get(m.btn.indexOf(e.getSource())).getBackground()))
    {
        m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.red);
    }
    else if(Color.red==( m.btn.get(m.btn.indexOf(e.getSource())).getBackground()))
    {
        m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.yellow);
    }
    else if(Color.yellow==( m.btn.get(m.btn.indexOf(e.getSource())).getBackground()))
    {
        m.btn.get(m.btn.indexOf(e.getSource())).setBackground(Color.green);
    }

}

我想现在这样做了,抱歉找麻烦的人。