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

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

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

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

import javax.swing.*;

public class sad implements ActionListener{
    JFrame f;
    JPanel p;
    JButton  b1,b2;
    JLabel l1,l2;
    JTextField t1,t2;
    public sad(){
        f=new JFrame("窗口");
        p=new JPanel();
        p.setLayout(null);
        p.setBounds(500, 500, 500, 500);
        l1=new JLabel("账号");
        t1=new JTextField();
        l2=new JLabel("密码");
        t2=new JTextField();
        b1=new JButton("登陆");
        b2=new JButton("注册");
        b2.addActionListener(this);
        t1.setBounds(140, 50, 200, 20);
        b1.setBounds(120, 150, 80, 30);
        t2.setBounds(140, 100, 200, 20);
        b2.setBounds(230, 150, 80, 30);
        l1.setBounds(100, 50, 40, 20);
        l2.setBounds(100, 100, 40, 20);
        f.add(p);
        p.add(l1);
        p.add(t1);
        p.add(l2);
        p.add(t2);
        p.add(b1);
        p.add(b2);
        p.setBackground(Color.pink);
        f.setVisible(true);
        f.setSize(500, 300);
    }
    public static void main(String[]args) {
        new sad();
    }
    public void actionPerformed(ActionEvent e) {
        JFrame f1=new JFrame("注册窗口");
        JPanel p1=new JPanel();
        JLabel l3=new JLabel("注册成功!");
        l3.setBounds(100, 50, 100, 50);
        p1.setLayout(null);
        p1.setBounds(500, 500, 500, 500);
        f1.add(p1);
        p1.add(l3);
        p1.setBackground(Color.pink);
        f1.setLocation(700,800);
        f1.setVisible(true);
        f1.setLocation(400,200);
        f1.setSize(500, 300);
        
    }
}

感想:以前编写程序时总是想到什么就打什么,经常是毫无目的性的,但在老师这几次课程对思考过程的强调后,我也渐渐地开始在编写程序时现在脑中想好步骤再有序编码。而且在这次编写程序过程中也遇到了诸多问题,例如鼠标点击语句b2.addActionListener(this);我忘记了,就导致点击了注册按钮,但新的窗口一直不出现,后来还是查看了笔记才记起有这么一条语句。如果对课堂知识的及时巩固是很有必要的。

posted on 2019-05-13 00:03  龚智  阅读(184)  评论(0编辑  收藏  举报