课后练习----实现窗口的切换

package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class MyFrame1 implements ActionListener {   //在类中声明组件
        JFrame f;      //窗口
        JPanel p,p1;    //面板
        JButton b,b1;     //按键
        JLabel l,l1,l2;   //标签
        JTextField t;      //文本框
        JPasswordField m;   //密码域
        public MyFrame1() {          //构造方法中new所有组件
            f=new JFrame("登陆界面");     //窗口名
            p=new JPanel();     
            p1=new JPanel();
            b=new JButton("登陆");
            b1=new JButton("注册"); //按键名
            t=new JTextField();
            m=new JPasswordField();
            //标签
            l=new JLabel("账号");   
            l1=new JLabel("密码"); 
            l2=new JLabel("注册成功!");
            
                                    //标签
            t.setBounds(110,82,200,28);       //组件  文本框在容器中的位置
            m.setBounds(110,140,200,28);      //密码域位置
            
            p.setLayout(null);  //NULL空布局
            p1.setLayout(null);
            
            
            b.setBounds(90, 210, 80, 25);  //按键位置
            b1.setBounds(240, 210, 80, 25);
             
            
            b.addActionListener(this);  //按键响应
            b1.addActionListener(this);
            
            //文本框位置
            l.setBounds(75, 80, 80,30);
            l1.setBounds(75, 140, 80, 30);
            l2.setBounds(75,150,40,50);
            //  
            
            //加组件
            f.add(p);  //面板加容器里
            //各个组件加到面板上
            p.add(b);   
            p.add(b1);
            p.add(l);
            p.add(l1);
            p.add(t);
            p.add(m);
            //
            
            p.setBackground(new Color(128,255,255)); //面板颜色
            
            //p1.add(l2);
            //l2.setBounds(130,15, 150, 60);
            //窗口位置大小设置
            f.setLocation(400,500);
            f.setSize(500,600);
            f.setVisible(true);
            //
        }
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
            new MyFrame1();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        // f.setVisible(false); //旧窗口隐藏
        JFrame f1=new JFrame("注册窗口");    //用JFrame创建新窗口
         
         f1.setVisible(true);  //新窗口可见
         
         //新窗口的设置
         f1.add(p1);  
         p1.add(l2);
         l2.setBounds(130,15, 150, 60);
         f1.setLocation(430,198);
         f1.setSize(450,180);
         
    }
    
    }

posted @ 2019-05-11 21:38  李振业  阅读(238)  评论(0编辑  收藏  举报