有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Linux平台下java按钮颜色问题

当我运行应用程序时,按钮颜色没有得到更新,而不是正常显示和动态显示。这个问题只在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 look and feel,那么我将在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 

共 (2) 个答案

  1. # 1 楼答案

    您的代码不遵守Swing线程规则。您应该仅在Swing事件线程(EDT)上更改组件的属性。用一个SwingWorker来做这件事,你的问题很可能会消失

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

    此外,您的代码格式很差(例如} } } }),这使得我们很难阅读您的代码并帮助您。如果您希望我们帮助您,请在这里发布更好的格式化代码

  2. # 2 楼答案

    这个

    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了解更多信息