Java的Swing组件创建简易 的登录注册界面

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**

  • 构造登录界面
    /
    public class DengLu extends JFrame{
    public DengLu() {
    setTitle(“登录界面”);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setBounds(200, 200, 500, 400);
    Container c=getContentPane();
    c.setLayout(null);
    JLabel jl1=new JLabel(“用户名:”);
    jl1.setBounds(100, 100, 80, 30);
    JLabel jl2=new JLabel(“密 码:”);
    jl2.setBounds(100, 140, 80, 30);
    JTextField jt1=new JTextField();
    jt1.setBounds(180, 100, 150, 30);
    JPasswordField jp1=new JPasswordField();
    jp1.setBounds(180, 140, 150, 30);
    JButton jb1=new JButton(“登录”);
    jb1.setBounds(100, 230, 80, 40);
    /
    *
    * 为登录按钮添加监听
    */
    jb1.addActionListener(new ActionListener() {

     	@Override
     	public void actionPerformed(ActionEvent e) {
     		if (jt1.getText().equals("admin") & new String(jp1.getPassword()).equals("666666")) {
     			JOptionPane.showMessageDialog(null, "登陆成功");
     		}else {
     			JOptionPane.showMessageDialog(null, "请确认您的用户名和密码");
     		}
     	}
     });
     JButton jb2=new JButton("注册");
     jb2.setBounds(250, 230, 80, 40);
     /**
      * 为注册按钮添加监听
      */
     jb2.addActionListener(new ActionListener() {
     	
     	@Override
     	public void actionPerformed(ActionEvent e) {
     		JFrame zhuce1=new JFrame();
     		Zhuce zhu=new Zhuce(zhuce1);
     	}
     });
     c.add(jb1);
     c.add(jb2);
     c.add(jt1);
     c.add(jp1);
     c.add(jl1);
     c.add(jl2);
     setVisible(true);
    

    }
    public static void main(String[] args) {
    new DengLu();
    }

}
/**
注册按钮弹出新的JDialog
/
class Zhuce extends JDialog{
public Zhuce(JFrame f) {
super(f,“注册界面”,true);
setBounds(200, 200, 500, 400);
Container c2=getContentPane();
c2.setLayout(null);
JLabel jl3=new JLabel(“用户名:”);
jl3.setBounds(100, 100, 80, 30);
JLabel jl4=new JLabel(“请输入密码:”);
jl4.setBounds(100, 140, 80, 30);
JLabel jl5=new JLabel(“请确认密码:”);
jl5.setBounds(100, 180, 80, 30);
JTextField jt2=new JTextField();
jt2.setBounds(180, 100, 150, 30);
JPasswordField jp2=new JPasswordField();
jp2.setBounds(180, 140, 150, 30);
JPasswordField jp3=new JPasswordField();
jp3.setBounds(180, 180, 150, 30);
JButton jb3=new JButton(“确认信息”);
jb3.setBounds(160, 240, 120, 40);
/

* 添加确认信息的监听
*/
jb3.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (!(jt2.getText().equals("")) & !(new String(jp2.getPassword()).equals("")) & !(new String(jp3.getPassword()).equals("")) & new String(jp2.getPassword()).equals(new String(jp3.getPassword())) ) {
				JOptionPane.showMessageDialog(null,"注册成功");
			}else {
				JOptionPane.showMessageDialog(null,"请检查您的注册信息");
			}
		}
	});
	c2.add(jl3);
	c2.add(jl4);
	c2.add(jl5);
	c2.add(jt2);
	c2.add(jp2);
	c2.add(jp3);
	c2.add(jb3);
	setVisible(true);
}

}
//初学者,注册的信息不能当做登陆的信息使用;写的不好的位置还请批评指正

你可能感兴趣的:(Java的Swing组件创建简易 的登录注册界面)