2013-02-22 43 views
0

按鈕顏色沒有得到更新,而是在我運行我的應用程序時沒有正常顯示以及動態顯示。這個問題只發生在Linux環境中,並且相同的代碼可以很好地與Windows一起工作。Linux平臺中的按鈕顏色問題

private JButton button = new JButton(); 
       button.setLayout(buttonLayout); 
       button.add(totalsLabel, BorderLayout.CENTER); 
       totalsLabel.setHorizontalAlignment(JButton.CENTER); 
       button.add(statusLabel, BorderLayout.SOUTH); 
       statusLabel.setHorizontalAlignment(JButton.CENTER); 
       button.setMargin(new Insets(0, 0, 0, 0)); 
       button.setVerticalAlignment(SwingConstants.TOP); 
       button.setHorizontalTextPosition(SwingConstants.CENTER); 
       button.setEnabled(true); 
       button.setPreferredSize(PREFERRED_SIZE); 
       button.setRequestFocusEnabled(false); 
       button.setVerifyInputWhenFocusTarget(false); 
       button.setFocusPainted(false); 
       button.setBackground(mementoTO.getBackGroundColor()); 
       private void initializeAlternatingColorsThread() { 

       alternatingColors = new Thread(new Runnable() { 
        public void run() { 
         while(true) { 
          while(continueAlternatingColors()) { 
           try { 
            if(button.getBackground().equals(BACKGROUND_PAY_LATER)) { 
             button.setBackground(BACKGROUND_BUSY); } 
            else { 
             button.setBackground(BACKGROUND_PAY_LATER); } 
            Thread.sleep(500); } 
           catch(InterruptedException ex) { 
            getLogger().error(this + " - Error occured in initializeAlternatingColorsThread: ", ex); } } 
          synchronized(lockVariable) { 
           try { 
            lockVariable.wait(); } 
           catch(InterruptedException e) { 
           } } } } 
       }, "AlternatingColors"); } 


    GuiExecutor.getInstance().update(new Runnable() { 
        public void run() { 
         setStaticText("RESETTING PUMP"); 
         setStatus("HANG UP NOZZLE"); 
         button.setBackground(BACKGROUND_BUSY); 
         button.repaint(); 
        } });  

如果我繼續用windows的外觀和感覺,那麼我在Linux下得到的異常。所以我改變了外觀和GDK for Linux。

INFO | jvm 1 | main | 2013/01/21 15:14:23.995 | Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    INFO | jvm 1 | main | 2013/01/21 15:14:23.995 |  at javax.swing.plaf.basic.BasicButtonUI.getMinimumSize(BasicButtonUI.java:352) 
    INFO | jvm 1 | main | 2013/01/21 15:14:23.995 |  at javax.swing.JComponent.getMinimumSize(JComponent.java:1714) 
    INFO | jvm 1 | main | 2013/01/21 15:14:23.995 |  at java.awt.BorderLayout.minimumLayoutSize(BorderLayout.java:651) 
    INFO | jvm 1 | main | 2013/01/21 15:14:23.995 |  at java.awt.Container.minimumSize(Container.java:1651) 
    INFO | jvm 1 | main | 2013/01/21 15:14:23.995 |  at java.awt.Container.getMinimumSize(Container.java:1636) 
    INFO | jvm 1 | main | 2013/01/21 15:14:23.996 |  at javax.swing.JComponent.getMinimumSize(JComponent.java:1716) 
    INFO | jvm 1 | main | 2013/01/21 15:14:23.996 |  at java.awt.FlowLayout.minimumLayoutSize(FlowLayout.java:448) 
    INFO | jvm 1 | main | 2013/01/21 15:14:23.996 |  at 

回答

3

您的代碼不尊重Swing線程規則。您應該只在Swing事件線程(EDT)上更改組件的屬性。使用SwingWorker來做到這一點,你的問題可能會消失。

更好的是,爲什麼不簡單地使用Swing Timer?

此外,您的代碼格式不佳(例如 - } } } })使我們很難閱讀您的代碼併爲您提供幫助。如果您希望我們盡力幫助您,請在此處發佈更好的格式化代碼。

+0

我得到了在那裏我得到這個異常的地方。 button.add(totalsLabel,BorderLayout.CENTER); totalsLabel.setHorizo​​ntalAlignment(JButton.CENTER); button.add(statusLabel,BorderLayout.SOUTH); statusLabel.setHorizo​​ntalAlignment(JButton.CENTER); – user1280096 2013-02-22 06:51:04

+0

@ user1280096:請注意任何更改作爲原始問題的補充。編輯原始問題,在最後添加新代碼並添加新解釋,然後評論我們的答案以通知我們有關更改。 – 2013-02-22 11:54:39

3

這...

alternatingColors = new Thread(new Runnable() { 
    public void run() { 
     while (true) { 
      while (continueAlternatingColors()) { 
       try { 
        if (button.getBackground().equals(BACKGROUND_PAY_LATER)) { 
         button.setBackground(BACKGROUND_BUSY); 
        } else { 
         button.setBackground(BACKGROUND_PAY_LATER); 
        } 
        Thread.sleep(500); 
       } catch (InterruptedException ex) { 
        getLogger().error(this + " - Error occured in initializeAlternatingColorsThread: ", ex); 
       } 
      } 
      synchronized (lockVariable) { 
       try { 
        lockVariable.wait(); 
       } catch (InterruptedException e) { 
       } 
      } 
     } 
    } 
}, "AlternatingColors"); 

違反了Swing的單線程規則 - 你永遠不能創建或更新任何UI組件出事件指派線程的一側,這樣做可能會導致,因爲你已經發現,意外的行爲。

你應該使用SwingTimer來執行相同的任務...

看看Concurrency in Swing瞭解更多信息