溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java編寫怎么實(shí)現(xiàn)登陸窗口

發(fā)布時(shí)間:2022-04-26 10:22:42 來源:億速云 閱讀:136 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“Java編寫怎么實(shí)現(xiàn)登陸窗口”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Java編寫怎么實(shí)現(xiàn)登陸窗口”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。

要求:用戶名和密碼正確后進(jìn)入首頁(yè),錯(cuò)誤提示錯(cuò)誤,并且3次錯(cuò)誤后將退出窗口。

1、主窗口,窗口上有文本輸入框和登陸按鈕,如圖

Java編寫怎么實(shí)現(xiàn)登陸窗口

2、在登陸按鈕和取消按鈕上設(shè)置時(shí)間監(jiān)聽機(jī)制,

取消:退出程序;

登陸:獲取用戶名和密碼,然后與自己保存的用戶名密碼作比較,現(xiàn)在兩種情況,正確與錯(cuò)誤,

正確:關(guān)閉登陸窗口,并彈出新窗口,新窗口即為首頁(yè);

錯(cuò)誤:彈出新窗口,提示錯(cuò)誤,并且累加,超過三次則推出程序,

現(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中判斷兩個(gè)字符串相等用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);
        
    }
}

讀到這里,這篇“Java編寫怎么實(shí)現(xiàn)登陸窗口”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI