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
|
package com.sxt; import java.awt.Container; 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.JPasswordField; import javax.swing.JTextField; public class LoginFrame extends JFrame{ JTextField txtname= new JTextField(); JPasswordField txtpass= new JPasswordField(); JButton bl= new JButton( "登錄" ); JButton bg= new JButton( "關閉" ); //構造無參構造器把主要的方法放在構造器里,然后在main方法里面調 public LoginFrame(){ setBounds( 25 , 25 , 250 , 250 ); Container c = getContentPane(); c.setLayout( new GridLayout( 4 , 2 , 10 , 10 )); c.add( new JLabel( "用戶名" )); c.add(txtname); c.add( new JLabel( "密碼" )); c.add(txtpass); c.add(bl); c.add(bg); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible( true ); //注意:此處是匿名內部類 bg.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit( 0 ); } } ); //注意:此處是匿名內部類 bl.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String name = txtname.getText(); String pass = txtpass.getText(); if (name.equals( "tom" )&&pass.equals( "123" )){ System.out.println( "登陸成功" ); } else { System.out.println( "登錄失敗" ); } } } ); } public static void main(String[] args) { new LoginFrame(); } } |
結果展示:
總結
以上就是本文關于java通過JFrame做一個登錄系統的界面完整代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/u012060033/article/details/77116254