import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent
import javax.swing.JFrame
import javax.swing.JLabel
public class Print {
public static void main(String[] args) {
JFrame jFrame = new JFrame ()
final JLabel jLabel = new JLabel ("按回车键!")
jFrame.setLayout(null)
jLabel.setBounds(80,50,500,80)
jFrame.add(jLabel)
jFrame.setSize(200, 200)
jFrame.setLocation(200, 300)
jFrame.setVisible(true)
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
jFrame.addKeyListener(new KeyAdapter () {
int n = 0
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode()
if (keyCode == KeyEvent.VK_ENTER) {
n++
if (n == 1)
jLabel.setText("绿")
else if (n == 2) {
jLabel.setText("红")
} else if (n == 3) {
jLabel.setText("黄")
n = 0
}
}
}
})
}
}