以Java實現一個簡單的登錄界面


import java.awt.*;//導入awt包
import javax.swing.*;//導入swing包
import java.awt.event.ActionListener;//導入awt包中的監聽器事件包
import java.awt.event.ActionEvent;//導入awt包中的ActionEvent事件包

public class EnterScreen extends JFrame {
    static int s=0;
    public EnterScreen() { 
        Yanzhencode vcode = new  Yanzhencode();
        setSize(300,290);//設計窗體的大小
        setTitle("請登錄");
        setBackground(Color.RED);//設置背景顏色
        JLabel a=new JLabel("登錄名"); //實例化JLabel對象
        JLabel b=new JLabel("密    碼");
        JLabel g=new JLabel("忘記用戶名/密碼?");
        JLabel h=new JLabel("驗證碼");
        g.setForeground(Color.BLUE); 
        JTextField c=new JTextField(15);//實例化用戶名文本框
        JPasswordField d=new JPasswordField(15);//實例化密碼框
        JTextField k=new JTextField(4);//實例化驗證碼框
        d.setEchoChar('*');//將輸入密碼框中的密碼以*顯示出來
        JButton e=new JButton("登錄");
        JButton f=new JButton("快速注冊");
        e.setBackground(Color.YELLOW);//設置登錄按鈕字體顏色
        f.setForeground(Color.GRAY);//設置快速登錄按鈕填充色
        setVisible(true);//使窗體可視化
        Container m=getContentPane();//獲取一個容器
        getContentPane().setBackground(Color.WHITE);//設置窗體填充色
//        將用戶名、密碼的Jlabel和用戶名JTextField文本框、密碼JPasswordField密碼框以及確定JButton、快速注冊JButton添加到container容器里面                         //
        m.add(a);
        m.add(b);
        m.add(c);
        m.add(d);
        m.add(e);
        m.add(f);
        m.add(g);
        m.add(h);
        m.add(k);
        m.add(vcode);
        setBounds(300,300,300,300);//設置窗體的長寬各為300、300  讓其顯示在左上方的300、300處
        m.setLayout(null);
//        a、b、c、d、e、f顯示在container容器中的位置坐標
        a.setBounds(10,40,50,18);
        b.setBounds(10,80,50,18);
        c.setBounds(60,40,130,18);
        d.setBounds(60,80,130,18);
        h.setBounds(10,120,50,18);
        k.setBounds(60,120,80,18);
        e.setBounds(90,180,100,30);
        f.setBounds(90,220,100,30);
        g.setBounds(190,75,100,30);
        vcode.setBounds(140,110,80,30);
        e.addActionListener(new ActionListener() {//對登錄按鈕添加監聽事件
             
            @SuppressWarnings("deprecation")
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                
                if(c.getText().trim().equals("xiaoyang")&&new String(d.getPassword()).equals("123456")&&s==1) {//equals函數進行用戶名和密碼的匹配
                    JOptionPane.showMessageDialog(null,"登錄成功");
                    
                    new NewFrame();//進入到NewFrame這個窗體中
                }else if(c.getText().trim().equals("xiaoyang")&&new String(d.getPassword()).equals("123456")&&s==0) {
                    JOptionPane.showMessageDialog(null,"驗證碼輸入錯誤");
                }else {
                    JOptionPane.showMessageDialog(null, "登錄失敗,用戶名、密碼或驗證碼輸入錯誤");
                }
            }
                
        });
        f.addActionListener(new ActionListener(){//對快速注冊按鈕添加監聽事件
            @SuppressWarnings("deprecation")
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                new zhuce();//進入都到zhuce窗體中
            }
            
        });
        //判斷輸入驗證碼是否正確
            if(k.getText()== null) {
                s=0;
            }else if(vcode.getCode() == null) {
                s=1;
            }else if(vcode.getCode() .equals(k.getText())) {
                s=1;
            }else { 
            s=0;    
        }
    }
    
        public static void main(String[] args) {
            new EnterScreen();
            
        }
        
    }

截圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM