【问题标题】:Button color Issue in Linux platformLinux平台中的按钮颜色问题
【发布时间】:2013-02-22 05:09:13
【问题描述】:

当我运行我的应用程序时,按钮颜色没有得到更新,而是没有正常显示以及动态显示。此问题仅在 Linux 环境中出现,并且相同的代码在 windows 下也可以正常工作。

 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 中会遇到异常。所以我改变了外观并作为 Linux 的 GDK。

    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 

【问题讨论】:

    标签: java swing concurrency jbutton event-dispatch-thread


    【解决方案1】:

    这...

    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 的单线程规则 - 您绝不能在 Event Dispatching Thread 之外创建或更新任何 UI 组件,如您所见,这样做可能会导致意外行为。

    您应该使用SwingTimer 来执行相同的任务...

    查看Concurrency in Swing了解更多信息

    【讨论】:

      【解决方案2】:

      您的代码不遵守 Swing 线程规则。您应该只在 Swing 事件线程 (EDT) 上更改组件的属性。使用 SwingWorker 执行此操作,您的问题可能会消失。

      更好的是,为什么不简单地使用摇摆计时器?

      您的代码格式也很差(例如 -- } } } } ),这使我们难以阅读您的代码并为您提供帮助。如果您希望我们努力帮助您,请努力在此处发布格式更好的代码。

      【讨论】:

      • 我得到了我得到这个异常的地方。 button.add(totalsLabel, BorderLayout.CENTER); totalsLabel.setHorizo​​ntalAlignment(JButton.CENTER); button.add(statusLabel, BorderLayout.SOUTH); statusLabel.setHorizo​​ntalAlignment(JButton.CENTER);
      • @user1280096:请注意任何更改作为对原始问题的补充。编辑原始问题,在末尾添加新代码和新解释,然后评论我们的答案以通知我们更改。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      相关资源
      最近更新 更多