简体   繁体   English

无法在运行时设置checkbox的背景颜色

[英]Unable to set background color of checkbox at runtime

There is a class AgentHome which extends JFrame. 有一个类AgentHome扩展了JFrame。 AgentHome has a JPanel rem_panel. AgentHome有一个JPanel rem_panel。 Checkboxes are added dynamically into rem_panel…number of checkboxes depending on the number of entries in the database table from where the text to be displayed by the textboxes are read. 复选框被动态添加到rem_panel ...复选框的数量,具体取决于数据库表中的条目数,从中读取文本框要显示的文本。

AgentHome has an integer variable x and a checkbox arraylist rem_cbarr. AgentHome有一个整数变量x和一个复选框arraylist rem_cbarr。

rem_cbarr stores the checkboxes as they are created and added to rem_panel. rem_cbarr存储复选框,因为它们已创建并添加到rem_panel。 I am trying to set the background color of these checkboxes to red when the variable x is set to 1 as the program executes. 当程序执行时,当变量x设置为1时,我试图将这些复选框的背景颜色设置为红色。 I have implemented the TickerBehaviour of JADE framework to check if the variable x is set to 1. 我已经实现了JADE框架的TickerBehaviour来检查变量x是否设置为1。

I am unable to set the background color of the checkboxes to red. 我无法将复选框的背景颜色设置为红色。 This is the code I have implemented. 这是我实施的代码。 Please help. 请帮忙。 Thanks. 谢谢。

 public void setup()
{
  Behaviour loop = new TickerBehaviour( this, 2000 )
  {
     protected void onTick() {

      timer();
     }
  };


   addBehaviour( loop );
 }

  public void timer()
{
    AgentHome hm=new AgentHome();
           if(hm.x==1)
       {
           for (int i = hm.rem_cbarr.size()-1; i>=0; i--)
                   {
                       JCheckBox cb=hm.rem_cbarr.get(i);
                     cb.setBackground(Color.red);
                      hm.rem_panel.revalidate();
                     hm.rem_panel.repaint();
                   }
      }
}

GUI operations need to be done on the EDT (Event Dispatcher Thread). GUI操作需要在EDT(事件调度程序线程)上完成。 In java this happens by calling SwingUtilities.invokeLater(Runnable run) . 在java中,这通过调用SwingUtilities.invokeLater(Runnable run)

A number of things... 很多事情......

  • UI components should only ever be updated within the context of the Event Dispatching Thread UI组件应该只在Event Dispatching Thread的上下文中更新
  • You should never perform any action which might block the Event Dispatching Thread (like using loops or Thread#Sleep to try and update the screen) 您永远不应该执行任何可能阻止事件调度线程的操作(比如使用循环或Thread#Sleep尝试更新屏幕)
  • The Event Dispatching Thread is responsible for dispatching paint updates... Event Dispatching Thread负责调度paint更新......
  • JCheckBox is transparent by default. JCheckBox默认是透明的。

public class FlashCheckBox {

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

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

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(new FlashyCheckBox());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class FlashyCheckBox extends JCheckBox {

        private final Color defaultBackground;
        private int flash;
        private Timer flashTimer;

        public FlashyCheckBox() {

            defaultBackground = getBackground();

            flashTimer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    flash++;
                    if (flash % 5 == 0) {
                        setOpaque(false);
                        setBackground(defaultBackground);
                        flashTimer.stop();
                    } else if (flash % 2 == 0) {
                        setBackground(Color.YELLOW);
                        setOpaque(true);
                    } else {
                        setBackground(defaultBackground);
                        setOpaque(false);
                    }
                    repaint();
                }
            });

            flashTimer.setRepeats(true);
            flashTimer.setCoalesce(true);
            flashTimer.setInitialDelay(0);

            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    flashTimer.restart();
                }
            });

        }

    }

}

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

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