呵呵,我不知道你为什么要用这个类继承JWindow,我把它改为继承JFrame问题就解决了!import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.*;public class MyWindow extends JFrame {
JButton jButton1 = new JButton();
JTextArea jTextArea1 = new JTextArea();
public MyWindow() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
} this.addKeyListener(new MyKeyListener());
}
public static void main(String[] args) {
MyWindow myWindow = new MyWindow();
myWindow.setSize(300,300);
myWindow.show(true); }
private void jbInit() throws Exception {
jButton1.setText("jButton1");
jTextArea1.setText("jTextArea1");
this.getContentPane().add(jButton1, BorderLayout.NORTH);
this.getContentPane().add(jTextArea1, BorderLayout.CENTER);
}
class MyKeyListener implements KeyListener
{
public void keyTyped(KeyEvent e)
{
MyWindow.this.jTextArea1.setText(e.toString());
JOptionPane.showMessageDialog(MyWindow.this,e.toString());
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}}
import java.awt.event.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.*;public class MyWindow extends JFrame {
JButton jButton1 = new JButton();
JTextArea jTextArea1 = new JTextArea();
public MyWindow() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
} this.addKeyListener(new MyKeyListener());
}
public static void main(String[] args) {
MyWindow myWindow = new MyWindow();
myWindow.setSize(300,300);
myWindow.show(true); }
private void jbInit() throws Exception {
jButton1.setText("jButton1");
jTextArea1.setText("jTextArea1");
this.getContentPane().add(jButton1, BorderLayout.NORTH);
this.getContentPane().add(jTextArea1, BorderLayout.CENTER);
}
class MyKeyListener implements KeyListener
{
public void keyTyped(KeyEvent e)
{
MyWindow.this.jTextArea1.setText(e.toString());
JOptionPane.showMessageDialog(MyWindow.this,e.toString());
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}}
用了JFrame 后,用户可以很容易的关闭
你先参考一下 JDK 自带的源代码中的 JFrame.java ,有问题再论吧!