Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在我的 JFrame 中添加一个颜色指示器。当我点击一个按钮时它应该变成红色,如果我不点击一个按钮它应该是绿色的。我应该如何实现这个?
JLabel lblLed = new JLabel("•"); lblLed.setForeground(Color.Green);
将 ChangeListener 添加到您的 JButton 并在 StateChanged() 方法中添加:
if (buttonIsPressed) { lblLed.setForeground(Color.Red); } else { lblLed.setForeground(Color.Green); }