不能直接new一个窗体吗,比如这样:JFrame jf = new JFrame("图形");
而且继承了窗体,就没办法继承适配器了。
看代码,像第二个版本那样有什么好处:
//版本1
public class TestEventA {
private JFrame jf;
private JButton button;
private JTextArea jta;
public TestEventA() {
jf = new JFrame("事件编程-版本1");
jf.setLayout(new BorderLayout());
button = new JButton("按钮1---小花");
jta = new JTextArea(30, 40);
jf.add(button, BorderLayout.NORTH);
jf.add(jta, BorderLayout.CENTER);
}
public void showMe() {
jf.setSize(400, 300);
jf.setLocation(200, 100);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestEventA().showMe();
}
}
//版本二
public class TestEventA extends JFrame {
private JButton button;
private JTextArea jta;
public TestEventA() {
super("事件编程-版本1");
this.setLayout(new BorderLayout());
button = new JButton("按钮1---小花");
jta = new JTextArea(30, 40);
this.add(button, BorderLayout.NORTH);
this.add(jta, BorderLayout.CENTER);
}
public void showMe() {
this.setSize(400, 300);
this.setLocation(200, 100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestEventA().showMe();
}
}