如何使任何 JComponent 的背景颜色闪烁?
How to twinkle the background color of any JComponent?
我正在尝试使 JButton 的背景颜色闪烁,但只有 'sleep' 它起作用了。
我的代码:
@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);
}
}
}
编辑:
不能 post 整个代码,这么多行。
该按钮位于 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:
我只是发现它在运行时没有设置任何颜色(有或没有任何 delay/sleep)。
问题是当您休眠 UI 线程(通过 TimeUnit...sleep(x)
)时,它将无法重新呈现更改。我敢打赌最后一种颜色确实被渲染了。
您需要找到触发颜色变化的替代方法,查看计时器,尤其是摇摆计时器。
你需要使用javax.swing.Timer
来做这样的动画。
final Button b = ...;
final Color[] colors = ...;
colors[0] = Color.RED;
colors[1] = Color.LIGHT_GREY;
ActionListener al = new ActionListener() {
int loop = 0;
public void actionPerformed(ActionEvent ae) {
loop = (loop + 1) % colors.length;
b.setBackground(colors[loop]);
}
}
new Timer(200, al).start();
注意:并非所有组件/JComponents 实际上都通过调用 setBackground 来更改背景
我正在尝试使 JButton 的背景颜色闪烁,但只有 'sleep' 它起作用了。
我的代码:
@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);
}
}
}
编辑: 不能 post 整个代码,这么多行。 该按钮位于 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: 我只是发现它在运行时没有设置任何颜色(有或没有任何 delay/sleep)。
问题是当您休眠 UI 线程(通过 TimeUnit...sleep(x)
)时,它将无法重新呈现更改。我敢打赌最后一种颜色确实被渲染了。
您需要找到触发颜色变化的替代方法,查看计时器,尤其是摇摆计时器。
你需要使用javax.swing.Timer
来做这样的动画。
final Button b = ...;
final Color[] colors = ...;
colors[0] = Color.RED;
colors[1] = Color.LIGHT_GREY;
ActionListener al = new ActionListener() {
int loop = 0;
public void actionPerformed(ActionEvent ae) {
loop = (loop + 1) % colors.length;
b.setBackground(colors[loop]);
}
}
new Timer(200, al).start();
注意:并非所有组件/JComponents 实际上都通过调用 setBackground 来更改背景