问题描述
- 为什么在GUI编程中要继承JFrame
-
不能直接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();
}
}
解决方案
GUI编程中可以不承JFrame,但这样自己的 GUI Frame 就要自己实现 JFrame 中的功能,以保证 Frame 能正确创建。
解决方案二:
你要是那么牛也可以自己写