要求:用户名和密码正确后进入首页,错误提示错误,并且3次错误后将退出窗口。
1、主窗口,窗口上有文本输入框和登陆按钮,如图
2、在登陆按钮和取消按钮上设置时间监听机制,
取消:退出程序;
登陆:获取用户名和密码,然后与自己保存的用户名密码作比较,现在两种情况,正确与错误,
正确:关闭登陆窗口,并弹出新窗口,新窗口即为首页;
错误:弹出新窗口,提示错误,并且累加,超过三次则推出程序,
现在粘上代码,
import java.awt.Container; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class Test10_3 { public static void main(String [] args){ Denglu de = new Denglu(); } } class Denglu{ private JFrame jf = new JFrame("登陆窗口"); private JButton jb1 = new JButton("用户名"); private JButton jb2 = new JButton("密码"); private JButton jbLogin = new JButton("登陆"); private JButton jbquxiao = new JButton("取消"); private JTextField jtName = new JTextField(); private JTextField jtPwd = new JTextField(); private final String mima = "sss111";//此为预设的密码 private int count = 0; private JFrame jfZhu = new JFrame(); ImageIcon image = new ImageIcon("C:\\Users\\sss\\Desktop\\index.jpg");//这为我的图片路径 private JLabel jlZhu = new JLabel(image); private JFrame jfCuo = new JFrame(); private JLabel jlCuo = new JLabel("你输入的用户名密码不匹配!"); public Denglu(){ Container cp = jf.getContentPane(); cp.setLayout(null); jtName.setBounds(150,50,100,20); cp.add(jtName); jb1.setBounds(10,50,100,20); cp.add(jb1); jtPwd.setBounds(150,100,100,20); cp.add(jtPwd); jb2.setBounds(10,100,100,20); cp.add(jb2); jbLogin.setBounds(20, 150, 100, 20); jbquxiao.setBounds(150, 150, 100, 20); cp.add(jbLogin); cp.add(jbquxiao); jbquxiao.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); jbLogin.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String name = jtName.getText(); String pwd = jtPwd.getText(); String aa = name+pwd; if(aa.equals(mima)){ //Java中判断两个字符串相等用equals()函数。 jf.dispose(); //关闭登陆面板 Container cp2 = jfZhu.getContentPane(); cp2.setLayout(null); jlZhu.setBounds(0, 0, 400, 300); cp2.add(jlZhu); jfZhu.setSize(400,300); jfZhu.setVisible(true); } else{ if(0<=count&&count<3){ Container cp3 = jfCuo.getContentPane(); cp3.setLayout(null); jlCuo.setBounds(0, 0, 400, 300); cp3.add(jlCuo); jfCuo.setSize(400,300); jfCuo.setVisible(true); count++; } else{ if(count>=3) System.exit(0); } } } }); jf.setSize(400, 300); jf.setVisible(true); } }
위 내용은 Java로 로그인 창을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!