窗口的切换

1、运用事件处理相关知识,完成两个窗口之间的切换,例如:登陆窗口------》注册窗口

 1 package xunxun;
 2 import java.awt.*;
 3 import java.awt.event.ActionEvent;
 4 import java.awt.event.ActionListener;
 5 
 6 import javax.swing.*;
 7 public class MyFrame extends JFrame{
 8     JFrame f1,f2;
 9     JLabel m;
10     JButton b1;
11     JButton b2;
12     JPanel p;
13     public MyFrame(){
14         p = new JPanel();
15         f1 = new JFrame();
16         f2 = new JFrame();
17         b1 = new JButton("登录");
18         b2 = new JButton("注册账号");
19         p.setLayout(null);
20         b1.setBounds(100,150,80,30);
21         b2.setBounds(100,100,80,30);
22         f1.setSize(300,300);
23         f1.add(p);
24         p.add(b1);
25         p.add(b2);
26         b1.addActionListener(new b1Action());
27         b2.addActionListener(new b2Action());
28         f1.setVisible(true);
29 
30     }
31     
32     class b1Action implements ActionListener{
33         
34         public void actionPerformed(ActionEvent e) {
35             
36             f1.setVisible(false);
37             f2 = new JFrame("登录成功界面");
38             f2.setSize(500,500);
39             f2.setVisible(true);
40      }
41         
42     }
43     
44     
45     
46     class b2Action implements ActionListener{
47         
48         public void actionPerformed(ActionEvent e){
49         
50             f1.setVisible(false);
51             f2 = new JFrame("注册账号界面");
52             f2.setSize(500,500);
53             f2.setVisible(true);
54         }
55     }
56     
57     public static void main(String[] args){
58         
59         new MyFrame();
60     }
61 }
62     

 

2、对本次作业进行总结,在编程中遇到哪些问题,如何解决,有哪些收获?

遇到的问题:定义完类,忘记编写主函数,导致得不出结果,而且对窗口的切换知识不够熟练,不能一气呵成,需要翻书和思考。

解决方法:多看书。

收获:从事件的处理到窗口的切换,花的时间比之前少了很多,熟练了。

原文地址:https://www.cnblogs.com/liuxun1031/p/10851698.html