窗口的切换
package demo; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class window implements ActionListener{ JPanel p1,p2; JFrame f1,f2; JButton b1,b2; JLabel l1,l2; JTextField t1,t2; Container c; JPasswordField s; {} public window(){ p1 = new JPanel(); f1 = new JFrame(); c = new Container(); b1= new JButton("登录"); b1.addActionListener(this); b2= new JButton("注册"); l1 = new JLabel("用户名:"); l2 = new JLabel("密码:"); t1 = new JTextField(20); s = new JPasswordField(20); f1.setSize(400, 200); f1.setVisible(true); f1.add(p1);p1.add(l1);p1.add(t1); p1.add(l2);p1.add(s); p1.add(b1);p1.add(b2); } public static void main(String[]args){ new window(); } @Override public void actionPerformed(ActionEvent arg0) { // TODO 自动生成的方法存根 p2 = new JPanel(); f2 = new JFrame("登陆成功"); f2.setSize(500, 300); f2.setVisible(true); f2.add(p2); f1.setVisible(false); } }
这个窗口的切换真的是难以弄出来,对于有些地方不是很清楚、明白。出现了许多错误,也和同学讨论了很久,虽然弄出来了,但是自己依旧是一知半解的状态。