简体   繁体   English

临时更改JButton背景颜色?

[英]Changing JButton background colour temporarily?

I'm super new to Java, and in need of some help. 我是Java的新手,需要一些帮助。 I'm making a little java desktop application where I basically have a grid of 4 JButtons ( 2 x 2 grid), and I need the background colour of individual JButtons to change, and after one second, change back to the original colour (the game I'm trying to make is like Simon, where you have to follow a pattern of buttons that light up). 我正在制作一个小的Java桌面应用程序,其中基本上有4个JButton的网格(2 x 2网格),我需要更改各个JButton的背景色,然后在一秒钟后,更改回原始颜色(我要制作的游戏就像Simon,您必须遵循一种点亮的按钮模式)。 I have a vector that contains randomly generated numbers in the range of 1 to 4, and I want to be able to get each element from the vector and get the corresponding button to change to a different colour for one second (for example, if the vector contains 2 4 1, I would want button 2 to change, then button 4 to change, then button 1 to change). 我有一个包含1到4范围内随机生成的数字的向量,并且我希望能够从向量中获取每个元素,并获取对应的按钮在一秒钟内更改为另一种颜色(例如,如果向量包含2 4 1,我希望按钮2更改,然后按钮4更改,然后按钮1更改)。

Is this possible or is there a better way to go about doing this with something other than JButtons? 这是可能的,还是有更好的方法用JButtons以外的方法来做到这一点? How do I implement this? 我该如何实施?

Also, I'm running Mac OS X, which apparently (based on some things I've read on forums) doesn't like JButtons background changing (I think it's because of system look and feel), how can I change this so it works on mac? 另外,我正在运行Mac OS X,显然(基于我在论坛上阅读的某些内容)不喜欢JButtons背景更改(我认为是由于系统外观),如何更改它,因此在Mac上工作?

Thank you in advance for any help :) 预先感谢您的任何帮助 :)

One approach is to extend JToggleButton and override paintComponent() to display the color. 一种方法是扩展JToggleButton并重写paintComponent()以显示颜色。 A javax.swing.Timer can control timing. javax.swing.Timer可以控制时间。 Here is a somewhat more elaborate example . 这是一个更详细的示例

private static class SimonButton extends JToggleButton {

    private final Color color;
    Dimension size = new Dimension(100, 100);

    public SimonButton(Color color) {
        this.color = color;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (this.isSelected()) {
            g.setColor(color);
        } else {
            g.setColor(Color.lightGray);
        }
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }

    @Override
    public Dimension getPreferredSize() {
        return size;
    }
}

You can just use the setBackground(...) method to set the Color and then use a Swing Timer to reset the background when it fires. 您可以只使用setBackground(...)方法来设置颜色,然后在触发时使用Swing计时器重置背景。

Edit: 编辑:

If your problem is that the setBackground() method doesn't work on some LAF's, then you could add an Icon to the button that is simply a solid Color. 如果您的问题是setBackground()方法在某些LAF上不起作用,则可以在按钮上添加一个Icon,它只是纯色。 Then to change the background color you simple change the Icon. 然后,要更改背景颜色,只需更改图标即可。

For your purposes, you don't necessarily have to use a JButton . 出于您的目的,您不必使用JButton You can use JLabels or JPanels . 您可以使用JLabelsJPanels

During initialization, you can setBackground() on each one to set its color, and add a MouseListener to each one to detect a click. 在初始化期间,您可以在每一个上设置setBackground()来设置其颜色,并向每一个上添加一个MouseListener来检测点击。

To flash the Simon pattern, create a javax.swing.Timer that fires once every second. 要刷新Simon模式,请创建一个每秒触发一次的javax.swing.Timer (You may want to make the delay configurable if 1 second seems like a long time.) For simplicity, the timer could setOpaque(false) on all the JLabels , then setOpaque(true) on the JLabel whose color you want to flash. (如果看起来很长的时间是1秒,则可能希望使延迟可配置。)为简单起见,计时器可以在所有JLabels上设置setOpaque(false) ,然后在要闪烁其颜色的JLabel上设置setOpaque(true) Note that you might want to wait until the next timer iteration before doing setOpaque(true) , so the flashes don't run together if you want to flash the same JLabel multiple times in a row. 请注意,在执行setOpaque(true)之前,您可能要等到下一次计时器迭代时才能执行,因此,如果要连续多次闪烁同一JLabel ,则闪光灯不能一起运行。

The advantage of using setOpaque() is that you can set the MouseListener to just call setOpaque(true) on press and setOpaque(false) on release, and check whether the correct JLabel was clicked, without having to repeatedly recompute which color should be used for a given JLabel . 使用setOpaque()的优点是您可以将MouseListener设置为仅在按下时调用setOpaque(true) ,在释放时调用setOpaque(true) setOpaque(false) ,并检查是否单击了正确的JLabel ,而不必反复重新计算应使用哪种颜色。对于给定的JLabel

okJButton = new JButton() {
@Override
public void paintComponent(Graphics g) {
    g.setColor(Color.decode("0X123456"));
    g.fillRect(0, 0, getSize().width, getSize().height);
}
};
okJButton.setForeground(Color.white);
okJButton.setBorder(new LineBorder(Color.white, 1));

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

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