Java达成一个登录窗体功能
发布时间:2021-11-24 15:39:35 所属栏目:PHP教程 来源:互联网
导读: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 th
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(); } } ![]() (编辑:应用网_丽江站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |