歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java實現一個登錄窗體功能

This program shows a "Login" window based on Swing JFrame. When you input the correct userID and Password, you can obtain a confirmation, or else you will be alerted by a JAVA standard message window.

The Swing JFrame used in the same time the GridLayout for the Container and the FlowLayout for the JPanel.

[java]
  1. package com.han;  
  2.   
  3. import java.awt.Container;  
  4. import java.awt.FlowLayout;  
  5. import java.awt.GridLayout;  
  6. import java.awt.event.ActionEvent;  
  7. import java.awt.event.ActionListener;  
  8. import java.util.Arrays;  
  9.   
  10. import javax.swing.*;  
  11.   
  12. /** 
  13.  * This program shows a "Login" window based on Swing JFrame. 
  14.  * When you input the correct userID and Password, you can obtain a confirmation, 
  15.  * or else you will be alerted by a JAVA standard message window. 
  16.  * <p> 
  17.  * The Swing JFrame used in the same time the GridLayout for the Container and the FlowLayout for the JPanel. 
  18.  * @author han 
  19.  * 
  20.  */  
  21. public class SwingJFrameLogin {  
  22.     /*define all the necessary member variables*/  
  23.     String s1=null;  
  24.     char[] s2=null;  
  25.     JFrame frame=new JFrame();  
  26.     Container c=frame.getContentPane();  
  27.     /*the construct function*/  
  28.     public SwingJFrameLogin() {  
  29.         c.setLayout(new GridLayout(3,1,10,10));//the Container uses the GridLayout for 3 JPanels    
  30.         JPanel panel1=new JPanel(new FlowLayout(FlowLayout.CENTER));//each JPanel uses the FlowLayout   
  31.         JPanel panel2=new JPanel(new FlowLayout(FlowLayout.CENTER));  
  32.         JPanel panel3=new JPanel(new FlowLayout());  
  33.         JLabel label1=new JLabel("用戶名:");  
  34.         final JTextField jt=new JTextField(10);  
  35.         JLabel label2=new JLabel("密碼:");  
  36.         final JPasswordField jp=new JPasswordField(6);  
  37.         jp.setEchoChar((char0);//set the display words as visible.   
  38.         final JButton jb1 = new JButton("提交");  
  39.         final JButton jb2 = new JButton("重置");  
  40.         panel1.add(label1);  
  41.         panel1.add(jt);  
  42.         panel2.add(label2);  
  43.         panel2.add(jp);  
  44.         panel3.add(jb1);  
  45.         panel3.add(jb2);  
  46.         c.add(panel1);  
  47.         c.add(panel2);  
  48.         c.add(panel3);        
  49.         jb1.addActionListener(new ActionListener(){  
  50.             @Override  
  51.             public void actionPerformed(ActionEvent e) {  
  52.                 String s1=jt.getText();  
  53.                 char[] s2=jp.getPassword();  
  54.                 System.out.println(s1);  
  55.                 System.out.println(s2);  
  56.                 char[] pw={'u','p','s'};  
  57.                 /*System.out.println(Arrays.equals(s2,pw)); 
  58.                 System.out.println(s1.equals("han"));*/  
  59.                 if (s1.equals("han") && Arrays.equals(s2,pw)) {  
  60.                     JOptionPane.showMessageDialog(frame,  
  61.                             "登錄成功 !","Message",JOptionPane.INFORMATION_MESSAGE);  
  62.                     //frame.dispose();(等同於點擊關閉窗口時執行frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );)   
  63.                     System.exit(0);//正常退出(等同於點擊關閉窗口時執行frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );)   
  64.                 }  
  65.                 else {                    
  66.                     JOptionPane.showMessageDialog(frame,//it is a JAVA internal STD message BOX   
  67.                             "You had input a wrong userID !!","Warning",JOptionPane.WARNING_MESSAGE);                     
  68.                 }  
  69.                 Arrays.fill(s2,'0'); //Zero out the possible password, for security.   
  70.             }             
  71.         });  
  72.         jb2.addActionListener(new ActionListener(){  
  73.             @Override  
  74.             public void actionPerformed(ActionEvent e) {//set all the fields empty.   
  75.                 jt.setText("");  
  76.                 jp.setText("");               
  77.             }         
  78.         });  
  79.         frame.pack();//automatically resize all the components to their preferred sizes.   
  80.         frame.setVisible(true);  
  81.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );  
  82.     }  
  83.       
  84.     public static void main(String[] args) {  
  85.         new SwingJFrameLogin();  
  86.     }  
  87. }  
Copyright © Linux教程網 All Rights Reserved