Java編寫實現(xiàn)登陸窗口
本文實例為大家分享了Java編寫登陸窗口的具體代碼,供大家參考,具體內(nèi)容如下
要求:用戶名和密碼正確后進(jìn)入首頁,錯誤提示錯誤,并且3次錯誤后將退出窗口。
我初學(xué)JavaGUI,現(xiàn)在我把我的思路寫一下,
1、主窗口,窗口上有文本輸入框和登陸按鈕,如圖

2、在登陸按鈕和取消按鈕上設(shè)置時間監(jiān)聽機(jī)制,
取消:退出程序;
登陸:獲取用戶名和密碼,然后與自己保存的用戶名密碼作比較,現(xiàn)在兩種情況,正確與錯誤,
正確:關(guān)閉登陸窗口,并彈出新窗口,新窗口即為首頁;
錯誤:彈出新窗口,提示錯誤,并且累加,超過三次則推出程序,
現(xiàn)在粘上代碼,
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";//此為預(yù)設(shè)的密碼
?? ?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()函數(shù)。
?? ??? ??? ??? ??? ?jf.dispose();?? ?//關(guān)閉登陸面板
?? ??? ??? ??? ??? ?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);
?? ??? ?
?? ?}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot詳解整合Spring?Cache實現(xiàn)Redis緩存流程
這篇文章主要介紹了SpringBoot整合Spring?Cache實現(xiàn)Redis緩存方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
SpringBoot項目中獲取IP地址的實現(xiàn)示例
OkHttp是一個由Square開發(fā)的高效、現(xiàn)代的HTTP客戶端庫,本文主要介紹了SpringBoot項目中獲取IP地址的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-08-08
Java日常練習(xí)題,每天進(jìn)步一點點(43)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
深入理解SpringBoot中關(guān)于Mybatis使用方法
這篇文章主要介紹了SpringBoot中關(guān)于Mybatis使用方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-03-03

