当我运行我的应用程序时,按钮颜色没有得到更新,而是没有正常显示和动态显示。此问题仅在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中遇到异常。所以我改变了外观和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
答案 0 :(得分:3)
您的代码不尊重Swing线程规则。您应该仅在Swing事件线程(EDT)上更改组件的属性。使用SwingWorker执行此操作,您的问题可能会消失。
更好的是,为什么不简单地使用Swing Timer?
您的代码格式也很差(例如 - } } } }
),这使我们很难阅读您的代码并帮助您。如果您希望我们努力为您提供帮助,请努力在此处发布更好的格式化代码。
答案 1 :(得分: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了解更多信息