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]
- package com.han;
-
- import java.awt.Container;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Arrays;
-
- import javax.swing.*;
-
- /**
- * 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.
- * <p>
- * The Swing JFrame used in the same time the GridLayout for the Container and the FlowLayout for the JPanel.
- * @author han
- *
- */
- public class SwingJFrameLogin {
- /*define all the necessary member variables*/
- String s1=null;
- char[] s2=null;
- JFrame frame=new JFrame();
- Container c=frame.getContentPane();
- /*the construct function*/
- public SwingJFrameLogin() {
- c.setLayout(new GridLayout(3,1,10,10));//the Container uses the GridLayout for 3 JPanels
- JPanel panel1=new JPanel(new FlowLayout(FlowLayout.CENTER));//each JPanel uses the FlowLayout
- JPanel panel2=new JPanel(new FlowLayout(FlowLayout.CENTER));
- JPanel panel3=new JPanel(new FlowLayout());
- JLabel label1=new JLabel("用戶名:");
- final JTextField jt=new JTextField(10);
- JLabel label2=new JLabel("密碼:");
- final JPasswordField jp=new JPasswordField(6);
- jp.setEchoChar((char) 0);//set the display words as visible.
- final JButton jb1 = new JButton("提交");
- final JButton jb2 = new JButton("重置");
- panel1.add(label1);
- panel1.add(jt);
- panel2.add(label2);
- panel2.add(jp);
- panel3.add(jb1);
- panel3.add(jb2);
- c.add(panel1);
- c.add(panel2);
- c.add(panel3);
- jb1.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- String s1=jt.getText();
- char[] s2=jp.getPassword();
- System.out.println(s1);
- System.out.println(s2);
- char[] pw={'u','p','s'};
- /*System.out.println(Arrays.equals(s2,pw));
- System.out.println(s1.equals("han"));*/
- if (s1.equals("han") && Arrays.equals(s2,pw)) {
- JOptionPane.showMessageDialog(frame,
- "登錄成功 !","Message",JOptionPane.INFORMATION_MESSAGE);
- //frame.dispose();(等同於點擊關閉窗口時執行frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );)
- System.exit(0);//正常退出(等同於點擊關閉窗口時執行frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );)
- }
- else {
- JOptionPane.showMessageDialog(frame,//it is a JAVA internal STD message BOX
- "You had input a wrong userID !!","Warning",JOptionPane.WARNING_MESSAGE);
- }
- Arrays.fill(s2,'0'); //Zero out the possible password, for security.
- }
- });
- jb2.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {//set all the fields empty.
- jt.setText("");
- jp.setText("");
- }
- });
- frame.pack();//automatically resize all the components to their preferred sizes.
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
- }
-
- public static void main(String[] args) {
- new SwingJFrameLogin();
- }
- }