课后练习----实现窗口的切换
1、运用事件处理相关知识,完成两个窗口之间的切换,例如:登陆窗口------》注册窗口
package bbb;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class myframe implements ActionListener {
JFrame f1,f2;
JPanel p1,p2;
JButton b1,b2;
JLabel l1,l2,l3;
JTextField t1,t2;//文本框
public myframe() {
f1=new JFrame("LYX");
f2=new JFrame("LYX");
p1=new JPanel();
p2=new JPanel();
p1.setLayout(null);//流布局
p2.setLayout(null);
b1=new JButton("注册");
b2=new JButton("登录");
l1=new JLabel("账户");
l1.setFont(new Font("楷体",Font.BOLD,18));//自定义标签字体
l2=new JLabel("密码");
l2.setFont(new Font("楷体",Font.BOLD,18));
l3=new JLabel("登录成功!");
l3.setForeground(Color.red);
t1=new JTextField();
t2=new JTextField();
b1.addActionListener(this);
b2.addActionListener(this);
f1.add(p1);
p1.add(b1);
p1.add(b2);
p1.add(l1);
p1.add(l2);
p1.add(t1);
p1.add(t2);
b1.setBounds(150,150,60,40);
b2.setBounds(270,150,60,40);
l1.setBounds(80,50,40,30);
l2.setBounds(80,100,40,30);
t1.setBounds(120,50,300,30);
t2.setBounds(120,100,300,30);
f1.setVisible(true);
f1.setSize(500,300);
}
public static void main(String[] args) {
new myframe();
}
public void actionPerformed(ActionEvent e) {
f1.setVisible(false);
p2.setBackground(new Color(211,252,4));//自定义面板颜色
f2.setVisible(true);
f2.add(p2);
p2.add(l3);
l3.setBounds(225,75,150,60);
f2.setLocation(600,0);
f2.setSize(500,300);
}
}
2、对本次作业进行总结,在编程过程中遇到哪些问题,如何解决,有哪些收获?
总结:此次作业是难度较大的一次作业,综合运用了许许多多的知识,代码也比较复杂,我在编程时经常会忽略按钮,文本框等组件在面板上的操作方法,导致无法显示或者是尺寸,位置不对,但是后来经过自己仔仔细细去看书,还是明白了其中的原理,这告诉我对于编程语言的学习一定要有学习自主性,不能光依靠老师在课堂上所讲的知识。

浙公网安备 33010602011771号