编写简单的用户登录界面

//1、如果用户名和密码不填写
	//弹出消息对话框提示进行输入用户名和密码
	//2、如果填写了用户名和密码
	//(1)、如果用户名和密码的填写都是admin弹出消息对话框,提示登陆成功
	//(2)、如果用户名和密码的填写的两个都不是admin弹出对话框提示登录名或密码错误
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.AncestorListener;

public class  UserTest extends JFrame implements ActionListener(){
//添加标签
JLable ul  = new JLable(用户名);
JLable pl  = new JLable(密码);

//文本框 ,长度为13
JTextFiled ut = new JTextField(13);
//密码框,长度为13
JTexedField pt = new JTextField(13);

//按钮
JButton login = new JButton("登录");
JButton cancel = new JButton("取消 ");

//面板
JPanel p1 = new JPanel();
Jpanel p2 = new JPanel();
JPanel p3 = new JPanel();

//构造方法
public UserTest(){
this.setTitle("用户登录测试“);
//把控件添加到面板
p1.add(ul);
p1.add(ut);
p2.add(pl);
p2.add(ut);
p3.add(login);
p3.add(cancel);
//设置窗体大小
this.setSize(200,200);
//设置窗体布局方式 网格布局2行3列
this.setLayout(new GridLayout(2,3));
//把面板放到窗体
this.add(p1);
this.add(p2);
this.add(p3);
//窗体可见
this.setVisible(true);
//窗体可关闭 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @Override //重写监听方法 public void actionPerformed(ActionEvent e) { if(e.getSource()== login){//监听登录按钮 if(ut.getText().equals("admin")&&pt.getText().equals("admin")){ //不覆盖窗体 JOptionPane.showMessageDialog(null, "登录成功"); } else if(ut.getText().equals("")||pt.getText().equals("")){ JOptionPane.showMessageDialog(null, "用户名或密码不能为空"); }else{ JOptionPane.showMessageDialog(null, "用户名或密码错误"); } } } } public static void main(String[] args){ new UserTest();//调用无参构造方法测试程序 }



 

 

posted @ 2016-05-09 19:49  桜の  阅读(446)  评论(0编辑  收藏  举报