歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java實現一個登錄窗體功能

Java實現一個登錄窗體功能

日期:2017/3/1 10:35:58   编辑:Linux編程

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