在不执行操作的情况下更改 jframe 属性



我正在尝试使用 jnativehook 制作一个热键程序,但是当我按下一个键(外部焦点)时,我无法更改按钮的颜色。按键和一切正常,但我猜它需要某种操作来"刷新"gui?

目前,我只有一个简单的侦听器,每当按下f12时,它都会转到setstartbtn函数:

 boolean s = false;
 public void nativeKeyPressed(NativeKeyEvent nke) {       
    if (nke.getKeyCode() == NativeKeyEvent.VC_F12){
       setstartbtn();
    }
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

  public void setstartbtn(){
             jButton1.setBackground(Color.red);
             jstartlabel.setForeground(Color.red);
             mainrot = false;
}

如果我将第二个功能分配给按钮本身,那么它可以正常工作。

这是同样的问题,例如,

如果我有一个倒数计时器,当计时器从 0 达到 10 时,按钮应该变成绿色,但它没有,所以我猜它需要执行操作?

任何帮助将不胜感激。

编辑:这是完整的项目:https://www.dropbox.com/sh/lfqziqbvszfr2hs/AABlyJGlb_UvIljEBUwMrpRza?dl=0

 public void setstartbtn(){
         jButton1.setBackground(Color.red);
         jstartlabel.setForeground(Color.red);
         mainrot = false;
         revalidate();
         repaint();
}

如果重新验证不起作用,请尝试 repaint()。

最新更新