本文实例为大家分享了Java编写登陆窗口的具体代码,供大家参考,具体内容如下
要求:用户名和密码正确后进入首页,错误提示错误,并且3次错误后将退出窗口。
我初学JavaGUI,现在我把我的思路写一下,
1、主窗口,窗口上有文本输入框和登陆按钮,如图
2、在登陆按钮和取消按钮上设置时间监听机制,
取消:退出程序;
登陆:获取用户名和密码,然后与自己保存的用户名密码作比较,现在两种情况,正确与错误,
正确:关闭登陆窗口,并弹出新窗口,新窗口即为首页;
错误:弹出新窗口,提示错误,并且累加,超过三次则推出程序,
现在粘上代码,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
import java.awt.Container; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class Test10_3 { public static void main(String [] args){ Denglu de = new Denglu(); } } class Denglu{ private JFrame jf = new JFrame( "登陆窗口" ); private JButton jb1 = new JButton( "用户名" ); private JButton jb2 = new JButton( "密码" ); private JButton jbLogin = new JButton( "登陆" ); private JButton jbquxiao = new JButton( "取消" ); private JTextField jtName = new JTextField(); private JTextField jtPwd = new JTextField(); private final String mima = "sss111" ; //此为预设的密码 private int count = 0 ; private JFrame jfZhu = new JFrame(); ImageIcon image = new ImageIcon( "C:\\Users\\sss\\Desktop\\index.jpg" ); //这为我的图片路径 private JLabel jlZhu = new JLabel(image); private JFrame jfCuo = new JFrame(); private JLabel jlCuo = new JLabel( "你输入的用户名密码不匹配!" ); public Denglu(){ Container cp = jf.getContentPane(); cp.setLayout( null ); jtName.setBounds( 150 , 50 , 100 , 20 ); cp.add(jtName); jb1.setBounds( 10 , 50 , 100 , 20 ); cp.add(jb1); jtPwd.setBounds( 150 , 100 , 100 , 20 ); cp.add(jtPwd); jb2.setBounds( 10 , 100 , 100 , 20 ); cp.add(jb2); jbLogin.setBounds( 20 , 150 , 100 , 20 ); jbquxiao.setBounds( 150 , 150 , 100 , 20 ); cp.add(jbLogin); cp.add(jbquxiao); jbquxiao.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit( 0 ); } }); jbLogin.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ String name = jtName.getText(); String pwd = jtPwd.getText(); String aa = name+pwd; if (aa.equals(mima)){ //Java中判断两个字符串相等用equals()函数。 jf.dispose(); //关闭登陆面板 Container cp2 = jfZhu.getContentPane(); cp2.setLayout( null ); jlZhu.setBounds( 0 , 0 , 400 , 300 ); cp2.add(jlZhu); jfZhu.setSize( 400 , 300 ); jfZhu.setVisible( true ); } else { if ( 0 <=count&&count< 3 ){ Container cp3 = jfCuo.getContentPane(); cp3.setLayout( null ); jlCuo.setBounds( 0 , 0 , 400 , 300 ); cp3.add(jlCuo); jfCuo.setSize( 400 , 300 ); jfCuo.setVisible( true ); count++; } else { if (count>= 3 ) System.exit( 0 ); } } } }); jf.setSize( 400 , 300 ); jf.setVisible( true ); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq1332479771/article/details/27082597