簡體   English   中英

無法在運行時設置checkbox的背景顏色

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

有一個類AgentHome擴展了JFrame。 AgentHome有一個JPanel rem_panel。 復選框被動態添加到rem_panel ...復選框的數量,具體取決於數據庫表中的條目數,從中讀取文本框要顯示的文本。

AgentHome有一個整數變量x和一個復選框arraylist rem_cbarr。

rem_cbarr存儲復選框,因為它們已創建並添加到rem_panel。 當程序執行時,當變量x設置為1時,我試圖將這些復選框的背景顏色設置為紅色。 我已經實現了JADE框架的TickerBehaviour來檢查變量x是否設置為1。

我無法將復選框的背景顏色設置為紅色。 這是我實施的代碼。 請幫忙。 謝謝。

 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操作需要在EDT(事件調度程序線程)上完成。 在java中,這通過調用SwingUtilities.invokeLater(Runnable run)

很多事情......

  • UI組件應該只在Event Dispatching Thread的上下文中更新
  • 您永遠不應該執行任何可能阻止事件調度線程的操作(比如使用循環或Thread#Sleep嘗試更新屏幕)
  • Event Dispatching Thread負責調度paint更新......
  • 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