運(yùn)行的結(jié)果:
源碼以及圖片:
鏈接: https://pan.baidu.com/s/13JJFyg14CbShs_HzLAoW6w?pwd=pwu3 提取碼: pwu3?
功能的實(shí)現(xiàn):
?實(shí)現(xiàn)1:
對每個(gè)文本框進(jìn)行監(jiān)控,鼠標(biāo)定在哪的時(shí)候,下邊框變?yōu)樗{(lán)色
實(shí)現(xiàn)2:
用戶光標(biāo)定到文本框的時(shí)候,要是沒有輸入信息,就把里面的提示文字給清除掉,給用戶一個(gè)好的體驗(yàn)
實(shí)現(xiàn)3:
登錄的時(shí)候?qū)~號和密碼檢驗(yàn),要是賬號和密碼都是admin是登錄成功,否則登錄失敗并提醒用戶
沒輸入密碼和賬號的時(shí)候也進(jìn)行提示
?
分析:
這個(gè)的頁面分為是三個(gè)部分,分別是上 中 下.其中上是一個(gè)背景,中是登錄需要輸入和選擇的,下是注冊賬號和二維碼
布局:
知識點(diǎn):
定位:
定位用的大部分都是絕對定位的,在學(xué)web時(shí)候,感覺絕對定位是真的好用,在這里面也是. 怎么去設(shè)置為絕對定位呢?
絕對定位的相關(guān)知識點(diǎn):
絕對定位是一種在 GUI 編程中使用的布局方式,它允許你直接指定組件在容器中的精確位置和尺寸。與其他布局管理器相比,絕對定位提供了更大的靈活性,可以精確控制組件的位置和大小。
使用絕對定位時(shí),你需要將容器的布局管理器設(shè)置為 null,這樣容器就不會自動調(diào)整組件的位置和大小。然后,你可以使用 setBounds() 方法來設(shè)置組件的位置和大小。setBounds() 方法接受四個(gè)參數(shù):x、y、width 和 height,分別表示組件的左上角在容器中的 x 和 y 坐標(biāo),以及組件的寬度和高度。
panel.setLayout(null); // 使用絕對定位
panel2.setBounds(0, 0, 591, 216); // 使用setBounds方法設(shè)置組件的位置和大小
代碼:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyQQLogin {
public static void main(String[] args) {
JFrame f = new JFrame("模擬QQ登錄");
f.setSize(600, 500);
f.setLocation(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null); // 使用絕對定位
panel.setBackground(Color.WHITE); // 設(shè)置背景顏色為白色
f.add(panel);
JPanel panel2 = new JPanel();
JLabel label = new JLabel(new ImageIcon("D:\\雜論\\background.png"));
panel2.add(label);
panel2.setBounds(0, 0, 591, 216); // 使用setBounds方法設(shè)置組件的位置和大小
panel2.setBackground(Color.WHITE); // 設(shè)置背景顏色為白色
panel.add(panel2);
JPanel panelCenter = new JPanel();
panelCenter.setLayout(null);//絕對定位
panelCenter.setBackground(Color.WHITE); // 設(shè)置背景顏色為白色
/*----------------qq號碼start--------------------*/
JPanel QQNumber = new JPanel();
JLabel labelQQ = new JLabel(new ImageIcon("D:\\雜論\\qq.png"));
JTextField Number = new JTextField("QQ號碼/手機(jī)/郵箱", 20);
Number.setPreferredSize(new Dimension(400, 35));
Number.setBorder(null);
Number.setFont(new Font("宋體", Font.PLAIN, 16));
QQNumber.add(labelQQ);
QQNumber.add(Number);
QQNumber.setBounds(-100, 0, 400, 40);
QQNumber.setBackground(Color.WHITE); // 設(shè)置背景顏色為白色
panelCenter.add(QQNumber);
//對Number輸入框進(jìn)行監(jiān)控
// 添加焦點(diǎn)監(jiān)聽器
Number.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
Number.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
if (Number.getText().equals("QQ號碼/手機(jī)/郵箱")) {
Number.setText("");
Number.setForeground(Color.BLACK);
}
QQNumber.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLUE));
Number.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLUE));
}
@Override
public void focusLost(FocusEvent e) {
if (Number.getText().isEmpty()) {
Number.setForeground(Color.GRAY);
Number.setText("QQ號碼/手機(jī)/郵箱");
}
QQNumber.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
Number.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
}
});
/*---------------qq號碼end--------------------*/
/*---------------qq密碼start------------------*/
JPanel passWord = new JPanel();
JLabel labelPassWord = new JLabel(new ImageIcon("D:\\雜論\\password.png"));
JTextField pass = new JTextField("密碼", 20);
pass.setPreferredSize(new Dimension(400, 35));
pass.setBorder(null);
pass.setFont(new Font("宋體", Font.PLAIN, 16));
pass.setForeground(Color.GRAY);
passWord.add(labelPassWord);
passWord.add(pass);
passWord.setBounds(-100, 40, 400, 40);
passWord.setBackground(Color.WHITE); // 設(shè)置背景顏色為白色
panelCenter.add(passWord);
passWord.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
pass.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
pass.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
String password = new String(pass.getText());
if (password.equals("密碼")) {
pass.setText("");
pass.setForeground(Color.BLACK);
}
passWord.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLUE));
pass.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLUE));
}
@Override
public void focusLost(FocusEvent e) {
String password = new String(pass.getText());
if (password.isEmpty()) {
pass.setForeground(Color.GRAY);
pass.setText("密碼");
}
passWord.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
pass.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
}
});
/*---------------qq密碼end------------------*/
/*---------------多選按鈕start--------------*/
JPanel choice = new JPanel();
choice.setLayout(null);
JCheckBox automaticLogin = new JCheckBox("自動登錄");
automaticLogin.setBackground(Color.WHITE);
automaticLogin.setBounds(0, 0, 100, 40);
JCheckBox rememberPass = new JCheckBox("記住密碼");
rememberPass.setBounds(125, 0, 100, 40);
JLabel findPass = new JLabel("找回密碼");
findPass.setBounds(250, 0, 100, 40);
automaticLogin.setOpaque(false); // 設(shè)置為不透明
rememberPass.setOpaque(false); // 設(shè)置為不透明
findPass.setForeground(Color.black); // 設(shè)置字體顏色為藍(lán)色
choice.add(automaticLogin);
choice.add(rememberPass);
choice.add(findPass);
choice.setBounds(0, 80, 400, 40);
choice.setBackground(Color.WHITE); // 設(shè)置背景顏色為白色
panelCenter.add(choice);
/*------------多選按鈕end------------*/
/*-------------登錄界面start-------------*/
JPanel login = new JPanel();
JButton secureLogin = new JButton("secureLogin", new ImageIcon("D:\\雜論\\安全登錄.png"));
secureLogin.setPreferredSize(new Dimension(320, 40));
secureLogin.setBackground(new Color(8, 186, 253));
secureLogin.setFont(new Font("Arial", Font.BOLD, 16)); // 設(shè)置字體為Arial,加粗,大小為16
secureLogin.setForeground(Color.WHITE); // 設(shè)置字體顏色為白色
secureLogin.setBorderPainted(false); // 去除按鈕的邊框繪制
login.add(secureLogin);
login.setBounds(0, 120, 320, 50);
login.setBackground(new Color(8, 186,253));
panelCenter.add(login);
secureLogin.addActionListener(new ActionListener() {//事件的監(jiān)控
@Override
public void actionPerformed(ActionEvent e) {
String account = Number.getText();
String password = pass.getText();
if (account.equals("admin") && password.equals("admin")) {
JOptionPane.showMessageDialog(f, "登錄成功");
} else if(account.equals("QQ號碼/手機(jī)/郵箱")) {
JOptionPane.showMessageDialog(f, "請輸入賬號");
} else if(password.equals("密碼")) {
JOptionPane.showMessageDialog(f, "請輸入密碼");
}
else {
JOptionPane.showMessageDialog(f, "賬號或密碼錯(cuò)誤");
}
}
});
/*------------登錄界面end----------------------*/
JPanel last = new JPanel();
last.setBounds(20, 420, 60, 30);
last.setBackground(Color.WHITE); // 設(shè)置背景顏色為白色
JPanel register = new JPanel();//注冊賬號
register.setBackground(Color.WHITE);
JLabel signIn = new JLabel("注冊賬號");
signIn.setFont(new Font("宋體", Font.PLAIN, 14));// 設(shè)置字體
signIn.setForeground(Color.GRAY);// 設(shè)置字體顏色
register.add(signIn);
last.add(register);
JPanel QRCode = new JPanel();
QRCode.setBackground(Color.WHITE);
QRCode.setBounds(500, 400, 45, 46);
JLabel QR = new JLabel(new ImageIcon("D:\\雜論\\QRCode.png"));
QRCode.add(QR);
panelCenter.setBounds(130, 240, 340, 180);//居中顯示
panel.add(panelCenter); // 將panelCenter添加到panel中
panel.add(last);
panel.add(QRCode);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e){
f.requestFocus(); // 將焦點(diǎn)設(shè)置到窗體上
}
});
}
}
代碼各個(gè)部分的分析:
創(chuàng)建JFrame對象
JFrame frame = new JFrame("QQ登錄");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
通過new JFrame("QQ登錄")創(chuàng)建一個(gè)JFrame對象,并設(shè)置標(biāo)題為"QQ登錄"。使用setSize()方法設(shè)置窗口的大小為400x300像素。使用setLocationRelativeTo(null)方法將窗口居中顯示。使用setResizable(false)方法禁止調(diào)整窗口大小。
創(chuàng)建主面板JPanel對象
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
添加JLabel組件
JPanel panel2 = new JPanel();
ImageIcon imageIcon = new ImageIcon("logo.png");
JLabel label = new JLabel(imageIcon);
panel2.add(label);
panel.add(panel2);
使用new JPanel()創(chuàng)建一個(gè)JPanel對象,并將其賦給panel2變量。使用new ImageIcon("logo.png")創(chuàng)建一個(gè)帶有指定圖像的ImageIcon對象,其中"logo.png"是圖片文件的路徑。使用new JLabel(imageIcon)創(chuàng)建一個(gè)帶有圖像的JLabel對象,并將其賦給label變量。將label添加到panel2中,然后將panel2添加到主面板panel中。
創(chuàng)建輸入框組件和多選按鈕組件
JPanel panelCenter = new JPanel();
panelCenter.setLayout(null);
panelCenter.setBackground(Color.WHITE);
JTextField accountField = new JTextField("QQ號碼/手機(jī)/郵箱");
accountField.setBounds(50, 50, 300, 30);
// 添加焦點(diǎn)監(jiān)聽器,以清除和恢復(fù)默認(rèn)文本
JPasswordField passwordField = new JPasswordField("密碼");
passwordField.setBounds(50, 100, 300, 30);
// 添加焦點(diǎn)監(jiān)聽器,以清除和恢復(fù)默認(rèn)文本
JCheckBox autoLoginCheckbox = new JCheckBox("自動登錄");
autoLoginCheckbox.setBounds(50, 150, 100, 30);
JCheckBox rememberPwdCheckbox = new JCheckBox("記住密碼");
rememberPwdCheckbox.setBounds(150, 150, 100, 30);
JLabel forgetPwdLabel = new JLabel("找回密碼");
forgetPwdLabel.setBounds(250, 150, 100, 30);
// 添加鼠標(biāo)事件監(jiān)聽器
使用new JPanel()創(chuàng)建一個(gè)JPanel對象,并將其賦給panelCenter變量。使用setLayout(null)方法將面板的布局管理器設(shè)置為絕對定位,這樣可以手動指定組件的位置和大小。通過setBackground(Color.WHITE)方法將面板的背景顏色設(shè)置為白色。
使用new JTextField("QQ號碼/手機(jī)/郵箱")創(chuàng)建一個(gè)文本框組件,并將其賦給accountField變量。使用setBounds(50, 50, 300, 30)方法設(shè)置組件的位置和大小。你可以根據(jù)需要修改這些值。
類似地,使用new JPasswordField("密碼")創(chuàng)建一個(gè)密碼輸入框組件,并將其賦給passwordField變量。使用setBounds(50, 100, 300, 30)方法設(shè)置組件的位置和大小。
使用new JCheckBox("自動登錄")創(chuàng)建一個(gè)多選按鈕組件,并將其賦給autoLoginCheckbox變量。使用setBounds(50, 150, 100, 30)方法設(shè)置組件的位置和大小。
類似地,使用new JCheckBox("記住密碼")創(chuàng)建一個(gè)多選按鈕組件,并將其賦給rememberPwdCheckbox變量。使用setBounds(150, 150, 100, 30)方法設(shè)置組件的位置和大小。
使用new JLabel("找回密碼")創(chuàng)建一個(gè)標(biāo)簽組件,并將其賦給forgetPwdLabel變量。使用setBounds(250, 150, 100, 30)方法設(shè)置組件的位置和大小。
監(jiān)聽輸入框組件
為輸入框組件添加焦點(diǎn)監(jiān)聽器,以在用戶點(diǎn)擊輸入框時(shí)清除默認(rèn)文本,并在用戶離開輸入框時(shí)恢復(fù)默認(rèn)文本
添加登錄按鈕和事件監(jiān)聽器
JButton loginButton = new JButton("登錄");
loginButton.setBounds(150, 200, 100, 30);
// 添加點(diǎn)擊事件監(jiān)聽器
使用new JButton("登錄")創(chuàng)建一個(gè)按鈕組件,并將其賦給loginButton變量。使用setBounds(150, 200, 100, 30)方法設(shè)置組件的位置和大小。
你可以為登錄按鈕添加點(diǎn)擊事件監(jiān)聽器,以執(zhí)行登錄操作。
創(chuàng)建小面板并添加到主面板中
JPanel registerPanel = new JPanel();
registerPanel.setBounds(0, 250, 200, 50);
// 添加鼠標(biāo)事件監(jiān)聽器
JPanel qrcodePanel = new JPanel();
qrcodePanel.setBounds(200, 250, 200, 50);
// 添加鼠標(biāo)事件監(jiān)聽器
panel.add(registerPanel);
panel.add(qrcodePanel);
使用new JPanel()創(chuàng)建一個(gè)JPanel對象,并將其賦給registerPanel變量。使用setBounds(0, 250, 200, 50)方法設(shè)置組件的位置和大小。
類似地,使用new JPanel()創(chuàng)建一個(gè)JPanel對象,并將其賦給qrcodePanel變量。使用setBounds(200, 250, 200, 50)方法設(shè)置組件的位置和大小。
將registerPanel和qrcodePanel添加到主面板panel中。
設(shè)置窗口可見
frame.add(panel);
frame.setVisible(true);
// 添加窗口事件監(jiān)聽器,以確保窗口獲取焦點(diǎn)
使用add(panel)方法將主面板panel添加到窗口frame中。使用setVisible(true)方法將窗口設(shè)置為可見狀態(tài)。文章來源:http://www.zghlxwxcb.cn/news/detail-772567.html
添加一個(gè)窗口事件監(jiān)聽器,以確保窗口獲取焦點(diǎn)。文章來源地址http://www.zghlxwxcb.cn/news/detail-772567.html
到了這里,關(guān)于Java模擬QQ登錄界面(GUI)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!