Java 单击jbutton后如何更改其颜色?

Java 单击jbutton后如何更改其颜色?,java,swing,jframe,jbutton,actionlistener,Java,Swing,Jframe,Jbutton,Actionlistener,我正在制作一个代表围棋板的jFrame。我想点击一个给定的按钮来改变颜色,以代表在电路板上放置一块。在下面的代码中,我展示了一个应该能够更改按钮颜色的方法(它只更改整个帧的背景)。第一个问题:为什么按钮颜色没有改变(这不是我在点击后改变颜色的更大问题,我的初步问题是按钮颜色不会改变)。我没有得到任何错误,按钮的颜色永远不会改变 public static void showBoard() { JFrame frame2 = new JFrame("Go Board"); fram

我正在制作一个代表围棋板的jFrame。我想点击一个给定的按钮来改变颜色,以代表在电路板上放置一块。在下面的代码中,我展示了一个应该能够更改按钮颜色的方法(它只更改整个帧的背景)。第一个问题:为什么按钮颜色没有改变(这不是我在点击后改变颜色的更大问题,我的初步问题是按钮颜色不会改变)。我没有得到任何错误,按钮的颜色永远不会改变

public static void showBoard()
{
    JFrame frame2 = new JFrame("Go Board");
    frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    for(int i = 19*19; i > 0; i--)
    {
      JButton firstButton = new JButton("");

      firstButton.setBackground(Color.blue);
      firstButton.setVisible(true);
      firstButton.setContentAreaFilled(true);
      firstButton.setOpaque(true);

      firstButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() 
            {
              System.out.println("ddsd");
              //int[] arr = findMove(0);
            }
          });
        }
      });
      frame2.getContentPane().add(firstButton);
    }
    frame2.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    frame2.setLayout(new GridLayout(19,19));
    frame2.pack();
    frame2.setVisible(true);
}
我的第二个问题是,让按钮在被点击后改变颜色,这可能是因为我甚至不能改变按钮的颜色。为了让按钮在单击后更改颜色,我计划将按钮颜色更改代码放在动作侦听器中

总之,我怎样才能在点击后改变按钮的颜色呢

答复:


问题是我的mac电脑的外观和感觉。如果mac上有类似问题,请查看检查答案,了解如何解决此问题。

您不需要在
ActionListener
中调用
SwingUtilities.invokeLater
,因为
actionPerformed(ActionEvent)
方法将在事件线程上调用

以下示例演示如何在单击按钮时更改其背景色:

public class ChangeButtonColor implements Runnable {
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            System.err.println("Cannot set LookAndFeel");
        }
        SwingUtilities.invokeLater(new ChangeButtonColor());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());

        JButton button1 = new JButton("click me");
        JButton button2 = new JButton("click me too");
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if (source instanceof Component) {
                    ((Component)source).setBackground(Color.RED);
                }
            }
        };
        button1.addActionListener(listener);
        button2.addActionListener(listener);

        frame.add(button1);
        frame.add(button2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

请注意,此处使用的ActionListener可用于所有按钮。无需为每个按钮创建它的新实例。

您不需要在
ActionListener
中调用
SwingUtilities.invokeLater
,因为
actionPerformed(ActionEvent)
方法将在事件线程上被调用

以下示例演示如何在单击按钮时更改其背景色:

public class ChangeButtonColor implements Runnable {
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            System.err.println("Cannot set LookAndFeel");
        }
        SwingUtilities.invokeLater(new ChangeButtonColor());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());

        JButton button1 = new JButton("click me");
        JButton button2 = new JButton("click me too");
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if (source instanceof Component) {
                    ((Component)source).setBackground(Color.RED);
                }
            }
        };
        button1.addActionListener(listener);
        button2.addActionListener(listener);

        frame.add(button1);
        frame.add(button2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

请注意,此处使用的ActionListener可用于所有按钮。无需为每个按钮创建它的新实例。

您不需要在
ActionListener
中调用
SwingUtilities.invokeLater
,因为
actionPerformed(ActionEvent)
方法将在事件线程上被调用

以下示例演示如何在单击按钮时更改其背景色:

public class ChangeButtonColor implements Runnable {
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            System.err.println("Cannot set LookAndFeel");
        }
        SwingUtilities.invokeLater(new ChangeButtonColor());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());

        JButton button1 = new JButton("click me");
        JButton button2 = new JButton("click me too");
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if (source instanceof Component) {
                    ((Component)source).setBackground(Color.RED);
                }
            }
        };
        button1.addActionListener(listener);
        button2.addActionListener(listener);

        frame.add(button1);
        frame.add(button2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

请注意,此处使用的ActionListener可用于所有按钮。无需为每个按钮创建它的新实例。

您不需要在
ActionListener
中调用
SwingUtilities.invokeLater
,因为
actionPerformed(ActionEvent)
方法将在事件线程上被调用

以下示例演示如何在单击按钮时更改其背景色:

public class ChangeButtonColor implements Runnable {
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            System.err.println("Cannot set LookAndFeel");
        }
        SwingUtilities.invokeLater(new ChangeButtonColor());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());

        JButton button1 = new JButton("click me");
        JButton button2 = new JButton("click me too");
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if (source instanceof Component) {
                    ((Component)source).setBackground(Color.RED);
                }
            }
        };
        button1.addActionListener(listener);
        button2.addActionListener(listener);

        frame.add(button1);
        frame.add(button2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}



请注意,此处使用的ActionListener可用于所有按钮。没有必要为每个按钮创建一个新的实例。

不确定我是否理解您的问题,您是说当前代码没有将开始按钮颜色设置为蓝色?是的,它只是将背景(jframe)设置为蓝色。好的,我运行了代码,所有按钮都设置为蓝色。这是正确的吗,但是当我在mac上运行它时,它只会将背景变成蓝色(而不是按钮)。不确定我是否理解你的问题,你是说当前代码没有将开始按钮变成蓝色?是的,它只是将背景(jframe)变成蓝色。好的,我运行了代码,所有按钮都变成蓝色。这是正确的吗,但是当我在mac上运行它时,它只会将背景变成蓝色(而不是按钮)。不确定我是否理解你的问题,你是说当前代码没有将开始按钮变成蓝色?是的,它只是将背景(jframe)变成蓝色。好的,我运行了代码,所有按钮都变成蓝色。这是正确的吗,但是当我在mac上运行它时,它只会将背景变成蓝色(而不是按钮)。不确定我是否理解你的问题,你是说当前代码没有将开始按钮变成蓝色?是的,它只是将背景(jframe)变成蓝色。好的,我运行了代码,所有按钮都变成蓝色。这是正确的吗,但当我在mac上运行它时,它只会将背景变成蓝色(而不是按钮)。这会编译所有内容,但当我运行并单击按钮时,按钮颜色不会改变。嗯,这应该可以工作。它对我有效,所以奇怪的是它对你无效。然而,我在我的Mac电脑上进行了测试,它在那里不起作用。因此,我认为这与苹果的外观有关作为main中的第一行,它也可以在Mac OS上工作。是苹果的外观造成了问题。我把它添加到主方法的第一行,但我得到了一个错误:找不到符号符号:class MetalLookAndFeel位置:class ChangeButtonColor你能在代码中告诉我在哪里放置它,这样我就不会出错吗?@user3224584我编辑了这个示例,包括设置外观。我还包括了包名,以防止您必须导入它(这可能是您的问题)。否则,通过调用
UIManager.getInstalledLookAndFeels()
并选择其中一个,就可以获得所有已安装的LookAndFeels的列表(苹果的外观除外,因为这当然不起作用)。还要查看
UIManager
this compiles的文档以及所有内容,但是当我运行并单击按钮时,按钮颜色不会改变。嗯,这应该可以。它对我有效,所以奇怪的是它对你无效。然而,我在我的Mac电脑上进行了测试,它在那里不起作用。因此,我认为这与苹果的外观有关作为main中的第一行,它也可以在Mac OS上工作。这是苹果的外观和感觉,cau