简体   繁体   English

在ActionPerformed上更改JButton颜色

[英]Change JButton Color on ActionPerformed

Despite my efforts of changing the color of my buttons, and reading everywhere on how to do it, my code still does not produce the desired effect. 尽管我努力改变按钮的颜色,并且到处都在阅读如何做,但是我的代码仍然无法产生理想的效果。 How can I change the color of a JButton when clicked? 单击时如何更改JButton的颜色?

public void generateButtons
{
  //Global field
  firstBoard = new JButton[9][9];

        for(int x = 0; x < firstBoard.length; x++)
        {
            for(int y = 0; y < firstBoard[0].length; y++)
            {
                firstBoard[x][y] = new JButton();
                firstBoard[x][y].setActionCommand("0|" + (x + (firstBoard.length * y)));
                firstBoard[x][y].addActionListener(this);
                //firstBoardPanel.add(firstBoard[x][y]);
            }
        }
}

@Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() instanceof JButton )
        {
            System.out.println(parseActionCommand(((JButton)e.getSource()).getActionCommand()));
            ((JButton)e.getSource()).setBackground(Color.BLUE); 
            ((JButton)e.getSource()).setContentAreaFilled(false);
            ((JButton)e.getSource()).setOpaque(true); 
        }
    }

Try repainting the container which contains that button. 尝试重新粉刷包含该按钮的容器。

button.getParent().validate();
button.getParent().repaint();

This is not an answer, but an extended test case to test the difference between Mac OS Look and Feel and Nimbus, which when run on Windows OS, works 这不是答案,而是一个扩展的测试用例,用于测试Mac OS Look和Feel与Nimbus之间的区别,当它们在Windows OS上运行时,可以正常工作

It appears, this became an answer... 看来,这成了答案...

Try this to start with... 尝试从...开始

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class ChangeButton {

    public static void main(String[] args) {
        new ChangeButton();
    }

    public ChangeButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel implements ActionListener {

        private final JButton[][] firstBoard;

        public TestPane() {
            setLayout(new GridLayout(9, 9));
            firstBoard = new JButton[9][9];

            for (int x = 0; x < firstBoard.length; x++) {
                for (int y = 0; y < firstBoard[0].length; y++) {
                    firstBoard[x][y] = new JButton();
                    firstBoard[x][y].setActionCommand("0|" + (x + (firstBoard.length * y)));
                    firstBoard[x][y].addActionListener(this);
                    add(firstBoard[x][y]);
                }
            }
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JButton) {
                System.out.println((((JButton) e.getSource()).getActionCommand()));
                ((JButton) e.getSource()).setBackground(Color.BLUE);
                ((JButton) e.getSource()).setContentAreaFilled(false);
                ((JButton) e.getSource()).setOpaque(true);
            }
        }
    }

}

Then replace UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 然后替换UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); with this... 有了这个...

for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
        UIManager.setLookAndFeel(info.getClassName());
        break;
    }
}

And try again 然后再试一次

Dont directly use the e.getSource() you need to reference it to an Object first and this use it: 不要直接使用e.getSource()您首先需要将其引用到Object,然后使用它:

example: 例:

ublic void actionPerformed(ActionEvent e)
{
    Object ob = e.getSource();
    if(ob instanceof JButton )
    {
        System.out.println(parseActionCommand(((JButton)e.getSource()).getActionCommand()));
        JButton button = ((JButton)ob);
        button.setBackground(Color.BLUE); 
        button.setContentAreaFilled(false);
        button.setOpaque(true); 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM