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.

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(); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值