1 package Com.Swing;
2 /*
3 * 用户名密码登录界面,添加判断用户名和密码是否正确
4 * */
5 import java.awt.*;
6 import javax.swing.*;
7 import java.awt.event.*;
8 public class Dengluchuangkou extends JFrame {
9
10 public static void main(String[] args) {
11 // TODO Auto-generated method stub
12 new Dengluchuangkou();
13 }
14 public Dengluchuangkou (){
15 JFrame jf=new JFrame ();
16 Container container=jf.getContentPane();
17 jf.setBounds(300,200,300,180);
18 container.setLayout(null);
19
20 JLabel jl1=new JLabel("用户名");
21 jl1.setBounds(10,10, 50, 20);
22 //JTextArea name=new JTextArea();//TextArea可以多行,可以添加滚动条
23 JTextField name=new JTextField();//TextField只有一行可写
24 name.setBounds(80,10,150,20);
25 JLabel jl2=new JLabel("密码");
26 jl2.setBounds(10,50,50,20);
27 JPasswordField password=new JPasswordField();
28 password.setBounds(80,50,150,20);
29 container.add(jl1);
30 container.add(name);
31 container.add(jl2);
32 container.add(password);
33
34 JButton jb1=new JButton("确定");
35 jb1.setBounds(40,90,80,30);
36 JButton jb2=new JButton("重置");
37 jb2.setBounds(170,90,80,30);
38 container.add(jb1);
39 container.add(jb2);
40
41 jf.setVisible(true);
42 jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
43
44 //设置监听
45 jb1.addActionListener(new ActionListener(){
46 public void actionPerformed(ActionEvent e){
47 if(name.getText().trim().equals("admin")&&password.getText().trim().equals("admin")){
48 JOptionPane.showMessageDialog(null,"登录成功");
49 }
50 if(name.getText().trim().length()==0||password.getText().trim().length()==0){
51 JOptionPane.showMessageDialog(null,"用户名或密码不能为空!");
52 }else{
53 JOptionPane.showMessageDialog(null,"用户名或密码错误!");
54 }
55
56 }
57 });
58
59 jb2.addActionListener(new ActionListener(){
60 public void actionPerformed(ActionEvent e){
61 name.setText("");
62 password.setText("");
63 }
64 });
65
66 }
67 }
![]()