当按下字母a时,我有一些标签可见。
private void formKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if(evt.getKeyCode()==KeyEvent.VK_A){
jLabel7.setVisible(true);
jLabel8.setVisible(true);
jLabel9.setVisible(true);
myBlink();
}
我在计时器myBlink()
上有Label8public void myBlink()
{
new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("begin");
jLabel8.setVisible(false);
jLabel8.setVisible(true);
System.out.println("Timer");
}
}).start();
}
我已经放置了printlns以查看计时器是否开始和结束,当我按下键#34; a"我的输出显示多次开始计时器,但我的标签不会出现和消失。这段代码需要什么调整?我错过了什么?感谢额外的一双眼睛。
答案 0 :(得分:3)
这可能是因为您连续拨打setVisible(false)
和setVisible(true)
这样做太快而无法看到,您应该使用变量并在Timer
的任何时候修改其值被称为下一个:
public void myBlink()
{
new Timer(1000, new ActionListener() {
boolean visible = true;
public void actionPerformed(ActionEvent e) {
jLabel8.setVisible(visible = !visible);
}
}).start();
}