我想闪烁的JButton的背景颜色,但只有'睡眠'它的工作。如何闪烁任何JComponent的背景颜色?
我的代码:
@Override
public void actionPerformed(ActionEvent e){
if(!empty){
}else{
myButton.setBackground(Color.RED);
try {TimeUnit.MILLISECONDS.sleep(200);} catch (InterruptedException e2){}
myButton.setBackground(Color.LIGHT_GRAY);
try {TimeUnit.MILLISECONDS.sleep(200);} catch (InterruptedException e1) {}
myButton.setBackground(Color.RED);
try {TimeUnit.MILLISECONDS.sleep(200);} catch (InterruptedException e1) {}
myButton.setBackground(Color.LIGHT_GRAY);
}
}
}
编辑: 不能发布整个代码,那么多行。 该按钮是一个的GridBagLayout内:
myButton= new Jbutton("Button!");
myButton.setBackground(Color.White);
myButton.setHorizontalAlignment(JTextField.CENTER);;
myButton.setForeground(Color.black);
GridBagConstraints gbc_myButton = new GridBagConstraints();
gbc_myButton.fill = GridBagConstraints.BOTH;
gbc_myButton.gridx = 0;
gbc_myButton.gridy = 1;
gbc_myButton.gridwidth=3;
panel.add(myButton, gbc_myButton);
编辑2: 我刚找出它不设置在运行时任何颜色(具有或不具有任何延迟/睡眠)。
你是什么意思“只有'sleep'工作”? – ItamarG3
延时工作,但颜色不变。 – tomyforever
a)200毫秒很短,请尝试更长的睡眠时间。 b)你在循环该代码吗? – ItamarG3