java 给大家一到题 JFrame程序相应esc 退出

来源:百度知道 编辑:UC知道 时间:2024/07/03 11:29:06
JFrame 上有几个控件 JButton,JTextField,当按下esc后程序退出
做好送50分.有点难哦

//多给楼主做了些.显示按键.按ESC时程序退出

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class PressESC extends JFrame implements KeyListener {
private String line1 = "", line2 = "";
private JTextArea text;
private JButton button;

public PressESC() {
super("Press ESC");
setLayout(new FlowLayout(FlowLayout.LEFT));
text = new JTextArea(10, 15);
button = new JButton("button");
text.setText("Press ESC to exit");
text.setEnabled(false);
text.setFont(new Font("TimesRoman", Font.BOLD + Font.ITALIC, 14));
getContentPane().add(text);
addKeyListener(this);
//getContentPane().add(button);

setSize(150, 100);
show();
}

public void keyPressed(KeyEvent e){
line1 = "键盘按下:" + e.getKeyText(e.getKeyCode());
setString(e);
if(e.