java课程程序设计-----一个类似QQ登录的界面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013907419/article/details/36634313
import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按钮
import javax.swing.JLabel;//标签
import javax.swing.JTextField;//文本框
import java.awt.Font;//字体
import java.awt.Color;//颜色
import javax.swing.JPasswordField;//密码框
import java.awt.event.ActionListener;//事件监听
import java.awt.event.ActionEvent;//事件处理
import javax.swing.JOptionPane;//消息窗口
public class UserLogIn extends JFrame{
	public JPanel p1;
	public JLabel p2;
	public JLabel p3;
	public JLabel p4;
	public JTextField p5;
	public JPasswordField p6;
	public JButton p7;
	public JButton p8;

	public UserLogIn(){
		p1 = new JPanel();
		p2 = new JLabel();
		p3 = new JLabel();
		p4 = new JLabel();
		p5 = new JTextField();
		p6 = new JPasswordField();
		p7 = new JButton();
		p8 = new JButton();
		userInit();
	}

	public void userInit(){
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭框架的同时结束程序
		this.setSize(300,200);//设置框架大小为长300,宽200
		this.setResizable(false);//设置框架不可以改变大小
		this.setTitle("登录");//设置框架标题
		this.p1.setLayout(null);//设置面板布局管理
		this.p1.setBackground(Color.yellow);//设置面板背景颜色
		this.p2.setText("用户登录");//设置标签标题
		this.p2.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14));//设置标签字体
		this.p2.setForeground(Color.blue);//设置标签字体颜色
		this.p3.setText("用户名:");
		this.p4.setText("密    码:");
		this.p7.setText("登录");
		this.p8.setText("重置");
		this.p2.setBounds(130,20,60,20);//设置标签x坐标130,y坐标20,长60,宽20
		this.p3.setBounds(50,55,60,20);
		this.p4.setBounds(50,85,60,25);
		this.p5.setBounds(110,55,120,20);
		this.p6.setBounds(110,85,120,20);
		this.p7.setBounds(85,120,60,20);
		this.p7.addActionListener(new ActionListener()//匿名类实现ActionListener接口
		{
			public void actionPerformed(ActionEvent e){
				p7_ActionEvent(e);
			}    
		}
				); 
		this.p8.setBounds(190,120,60,20);
		this.p8.addActionListener(new ActionListener()//匿名类实现ActionListener接口
		{
			public void actionPerformed(ActionEvent e){
				p8_ActionEvent(e);
			}    
		}
				);   
		this.p1.add(p2);//加载标签到面板
		this.p1.add(p3);
		this.p1.add(p4);
		this.p1.add(p5);
		this.p1.add(p6);
		this.p1.add(p7);
		this.p1.add(p8);
		this.add(p1);//加载面板到框架
		this.setVisible(true);//设置框架可显  
	}

	public void p7_ActionEvent(ActionEvent e){
		String name = p5.getText();
		String pwd = String.valueOf(p6.getPassword());
		if(name.equals("")){
			JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE);
			return;
		}else 
			if (pwd.equals("")){
				JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE);
				return;

			}else 
				if(p5.getText().trim().equals("abc")&&(p6.getText().trim().equals("123"))){

					JOptionPane.showMessageDialog(null, "恭喜您登陆成功!", "消息", JOptionPane.INFORMATION_MESSAGE);   
					this.dispose();
				}else{
					JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE);
					return;
				}	}
	public void p8_ActionEvent(ActionEvent e){
		p5.setText("");
		p6.setText("");
	}
	public static void main(String[] args){
		new UserLogIn();
	}
}  

*******这个课程设计虽然有借鉴网上的,但是风格和一些细节是自己做的,毕竟别人的不一定好,还有老师的帮助才完成得了中间的if算法的一部分*************感谢他们的帮助

猜你喜欢

转载自blog.csdn.net/u013907419/article/details/36634313