生活随笔
收集整理的這篇文章主要介紹了
Java实现简单的QQ登录界面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
學習了 Java 的 GUI 后,嘗試著做了一個仿QQ登錄界面,感覺還行。
實現功能:
說明:
沒有用到數據庫,賬號和密碼是寫死的。賬號為 admin,密碼為 123456。
運行效果:
源碼如下:
import javafx.scene.layout.Background;
import util.FileUtils;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.*;
import java.util.Date;
import java.util.concurrent.TimeUnit;import javax.swing.*;public class QQInitializer extends JFrame {private int x
= 0, y
= 0; private LoginButtonListener loginButtonListener
; private JFrame frame
; private JButton miniButton
; private JButton closeButton
; private JButton loginButton
; private JPanel northPanel
; private JPanel westPanel
; private JPanel centerPanel
; private JPanel eastPanel
; private JPanel southPanel
; private JLabel northLabel
; private JLabel westLabel
; private JLabel codeLabel
; private JLabel enrollLabel
; private JTextField text
; private JPasswordField codeWord
; private JCheckBox auto
; private JCheckBox mima
; private static boolean mimaState
; private static boolean loginState
; double time
; private static String id
; private static final String ACCOUNT
= "admin"; private static final String PASSWORD
= "123456"; private static final String IMAGE_PATH
= System.getProperty("user.dir") + "\\src\\images\\";private static final String TEMP_FILE_PATH
= System.getProperty("user.dir") + "\\src\\temp\\";private final int WIDTH
= 500;private final int HEIGHT
= 330;private final int BLOCK_TIME
= 2;public static void run() {new QQInitializer();}public QQInitializer() {init();}private void init() {frame
= new JFrame("QQ登錄");Toolkit t
= Toolkit.getDefaultToolkit();Dimension d
= t
.getScreenSize();
frame
.setSize(WIDTH
, HEIGHT
);frame
.setLocation(d
.width
/ 2 - frame
.getWidth() / 2, d
.height
/ 2 - frame
.getHeight() / 2);frame
.setIconImage(t
.getImage(getImagePath("icon.jpg")));frame
.setDefaultCloseOperation(HIDE_ON_CLOSE
);frame
.setUndecorated(true);frame
.setResizable(false);northPanel
= creatNorth();westPanel
= creatWest();centerPanel
= creatCenter();southPanel
= creatSouth();eastPanel
= creatEast();frame
.add(northPanel
, BorderLayout.NORTH
);frame
.add(westPanel
, BorderLayout.WEST
);frame
.add(southPanel
, BorderLayout.SOUTH
);frame
.add(centerPanel
, BorderLayout.CENTER
);frame
.add(eastPanel
, BorderLayout.EAST
);frame
.addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent e
) {x
= e
.getX(); y
= e
.getY(); }});frame
.addMouseMotionListener(new MouseMotionAdapter() {public void mouseDragged(MouseEvent e
) {int x_screen
= e
.getXOnScreen();int y_screen
= e
.getYOnScreen();int xx
= x_screen
- x
;int yy
= y_screen
- y
;frame
.setLocation(xx
, yy
);}});frame
.setVisible(true);if (auto
.isSelected()) {loginButton
.setEnabled(false);System.out
.println(BLOCK_TIME
+ " 秒后自動登錄...");Date d1
= new Date();try {TimeUnit.SECONDS
.sleep(BLOCK_TIME
); } catch (InterruptedException e1
) {e1
.printStackTrace();}Date d2
= new Date();time
= (double) ((d2
.getTime() - d1
.getTime()) / 1000);}if (time
== BLOCK_TIME
&& auto
.isSelected()) {frame
.dispose();onSuccess(true);} else if (time
== BLOCK_TIME
) {loginButton
.setEnabled(true);File file1
= new File(getTempFilePath("autologin.txt"));OutputStream os
;try {os
= new FileOutputStream(file1
);byte[] b
= "false".getBytes();os
.write(b
);os
.close();} catch (Exception e1
) {e1
.printStackTrace();}}}public JPanel creatNorth() {JPanel northPanel
= new JPanel();northPanel
.setLayout(null);northPanel
.setPreferredSize(new Dimension(0, 190));ImageIcon in
= new ImageIcon(getImagePath("b.jpg"));JLabel cc
= new JLabel(in
);cc
.setBounds(0, 0, 500, 190);cc
.setOpaque(false);closeButton
= new JButton(new ImageIcon(getImagePath("close_normal.png"))); closeButton
.setRolloverIcon(new ImageIcon(getImagePath("close_hover.png"))); closeButton
.setPressedIcon(new ImageIcon(getImagePath("close_hover.png"))); closeButton
.setBounds(468, 0, 30, 30);closeButton
.setToolTipText("關閉");closeButton
.setContentAreaFilled(false); closeButton
.setBorderPainted(false); closeButton
.setFocusPainted(false); closeButton
.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR
)); miniButton
= new JButton(new ImageIcon(getImagePath("mini.jpg")));miniButton
.setRolloverIcon(new ImageIcon(getImagePath("mini.png")));miniButton
.setPressedIcon(new ImageIcon(getImagePath("mini.png")));miniButton
.setBounds(437, 0, 30, 30);miniButton
.setToolTipText("最小化");miniButton
.setContentAreaFilled(false);miniButton
.setBorderPainted(false);miniButton
.setFocusPainted(false);miniButton
.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR
)); northPanel
.add(closeButton
);northPanel
.add(miniButton
);northPanel
.add(cc
);closeButton
.addActionListener(new closeButtonListener());miniButton
.addActionListener(new miniButtonListener());return northPanel
;}public JPanel creatWest() {JPanel jp2
= new JPanel();jp2
.setLayout(null);jp2
.setPreferredSize(new Dimension(160, 0));ImageIcon ss
= new ImageIcon(getImagePath("qq.png"));JLabel cs
= new JLabel(ss
);cs
.setBounds(35, 0, 100, 100);jp2
.add(cs
);return jp2
;}public JPanel creatCenter() {File file
= new File(getTempFilePath("rememberme.txt"));if (file
.exists()) {InputStream input
= null;try {input
= new FileInputStream(file
);byte[] b
= new byte[(int) file
.length()];input
.read(b
);mimaState
= Boolean.parseBoolean(new String(b
));input
.close();} catch (FileNotFoundException e1
) {e1
.printStackTrace();} catch (IOException e1
) {e1
.printStackTrace();}} else {mimaState
= Boolean.parseBoolean(new String("false"));}File file1
= new File(getTempFilePath("autologin.txt"));if (file1
.exists()) {InputStream input
= null;try {input
= new FileInputStream(file1
);byte[] b
= new byte[(int) file1
.length()];input
.read(b
);loginState
= Boolean.parseBoolean(new String(b
));input
.close();} catch (FileNotFoundException e1
) {e1
.printStackTrace();} catch (IOException e1
) {e1
.printStackTrace();}} else{loginState
= Boolean.parseBoolean(new String("false"));}JPanel jp4
= new JPanel();jp4
.setLayout(null);jp4
.setPreferredSize(new Dimension(0, 220));text
= new JTextField(10); text
.setBounds(0, 10, 200, 30);text
.setFont(new Font("宋體", Font.BOLD
, 17)); text
.addKeyListener(new KeyAdapter() {public void keyTyped(KeyEvent e
) {int key
= e
.getKeyChar();int count
= 0;if ((key
>= KeyEvent.VK_0
&& key
<= KeyEvent.VK_9
))count
++;if ((!(key
>= KeyEvent.VK_0
&& key
<= KeyEvent.VK_9
)) || (count
== 10))e
.consume();}});mima
= new JCheckBox("記住密碼", mimaState
);mima
.setBounds(0, 78, 80, 18);auto
= new JCheckBox("自動登錄", loginState
);auto
.setBounds(110, 78, 80, 18);text
.addFocusListener(new JTextFieldListener(text
, mima
, "QQ號"));text
.setOpaque(false);jp4
.add(text
);codeWord
= new JPasswordField(18);codeWord
.setBounds(0, 42, 200, 30);codeWord
.setFont(new Font("宋體", Font.BOLD
, 17));codeWord
.addFocusListener(new JPasswordFielddListener(codeWord
, mima
, "密碼"));codeWord
.setOpaque(false);jp4
.add(mima
);jp4
.add(codeWord
);jp4
.add(auto
);loginButtonListener
= new LoginButtonListener(text
, codeWord
, mima
, auto
);return jp4
;}public JPanel creatSouth() {JPanel jp3
= new JPanel();jp3
.setLayout(null);jp3
.setPreferredSize(new Dimension(0, 40));loginButton
= new JButton("登 錄", new ImageIcon(getImagePath("login_normal.png"))); loginButton
.setRolloverIcon(new ImageIcon(getImagePath("login_hover.png"))); loginButton
.setPressedIcon(new ImageIcon(getImagePath("login_hover.png"))); loginButton
.setBounds(160, 0, 200, 30);loginButton
.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR
)); loginButton
.setContentAreaFilled(false);loginButton
.setFocusPainted(false);loginButton
.setBorderPainted(false); loginButton
.setFocusPainted(false); loginButton
.setVerticalTextPosition(SwingConstants.CENTER
);loginButton
.setHorizontalTextPosition(SwingConstants.CENTER
);loginButton
.setFont(new Font("宋體", Font.BOLD
, 15));loginButton
.setForeground(new Color(255, 255, 255));jp3
.add(loginButton
);loginButton
.addActionListener(loginButtonListener
);return jp3
;}public JPanel creatEast() {JPanel jp5
= new JPanel();jp5
.setLayout(null);jp5
.setPreferredSize(new Dimension(130, 0));enrollLabel
= new JLabel("注冊賬號");enrollLabel
.setBounds(0, 10, 100, 30);enrollLabel
.setFont(new Font("宋體", Font.BOLD
, 15));enrollLabel
.setForeground(new Color(100, 149, 238));enrollLabel
.addMouseListener(new LabelAdapter(enrollLabel
, "注冊賬號"));enrollLabel
.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR
)); codeLabel
= new JLabel("忘記密碼");codeLabel
.setBounds(0, 42, 100, 30);codeLabel
.setFont(new Font("宋體", Font.BOLD
, 15));codeLabel
.setForeground(new Color(100, 149, 238));codeLabel
.addMouseListener(new LabelAdapter(codeLabel
, "忘記密碼"));codeLabel
.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR
));jp5
.add(enrollLabel
);jp5
.add(codeLabel
);return jp5
;}class closeButtonListener
implements ActionListener {public void actionPerformed(ActionEvent e
) {System.exit(0);}}class miniButtonListener
implements ActionListener {public void actionPerformed(ActionEvent e
) {frame
.setExtendedState(frame
.ICONIFIED
);}}class LoginButtonListener implements ActionListener {private JTextField t
;private JPasswordField f
;private JCheckBox mima
;private JCheckBox login
;public LoginButtonListener(JTextField t
, JPasswordField f
, JCheckBox mima
, JCheckBox login
) {this.t
= t
;this.f
= f
;this.mima
= mima
;this.login
= login
;}public void actionPerformed(ActionEvent e
) {String account
= t
.getText();String password
= new String(f
.getPassword());File rememberMimaFile
= new File(getTempFilePath("rememberme.txt"));File autoLoginFile
= new File(getTempFilePath("autologin.txt"));if (ACCOUNT
.equals(account
) && PASSWORD
.equals(password
)) {if (mima
.isSelected()) {File file2
= new File(getTempFilePath("account.txt"));try {FileUtils.createFile(getTempFilePath("account.txt"));OutputStream os1
= new FileOutputStream(file2
);byte[] b1
= account
.getBytes();os1
.write(b1
);os1
.close();} catch (IOException e2
) {e2
.printStackTrace();}try {FileUtils.createFile(getTempFilePath("rememberme.txt"));OutputStream os
= new FileOutputStream(rememberMimaFile
);byte[] b
= "true".getBytes();os
.write(b
);os
.close();} catch (IOException e1
) {e1
.printStackTrace();}} else {try {FileUtils.createFile(getTempFilePath("rememberme.txt"));OutputStream os
= new FileOutputStream(rememberMimaFile
);byte[] b
= "false".getBytes();os
.write(b
);os
.close();} catch (IOException e1
) {e1
.printStackTrace();}}if (mima
.isSelected() && login
.isSelected()) {try {FileUtils.createFile(getTempFilePath("autologin.txt"));OutputStream os
= new FileOutputStream(autoLoginFile
);byte[] b
= "true".getBytes();os
.write(b
);os
.close();} catch (IOException e1
) {e1
.printStackTrace();}} else {try {FileUtils.createFile(getTempFilePath("autologin.txt"));OutputStream os
= new FileOutputStream(autoLoginFile
);byte[] b
= "false".getBytes();os
.write(b
);os
.close();} catch (IOException e1
) {e1
.printStackTrace();}}frame
.dispose();onSuccess(true);} else {JOptionPane.showMessageDialog(null, "賬號或密碼錯誤,請重新輸入", "提示消息", JOptionPane.ERROR_MESSAGE
);}}}public class JTextFieldListener implements FocusListener {private String str
;private JTextField text1
;private JCheckBox rememberMima
;public JTextFieldListener(JTextField text1
, JCheckBox rememberMima
, String str
) {this.text1
= text1
;this.rememberMima
= rememberMima
;this.str
= str
;if (rememberMima
.isSelected()) {File file
= new File(getTempFilePath("account.txt"));try {InputStream in
= new FileInputStream(file
);byte[] bn
= new byte[(int) file
.length()];in
.read(bn
);in
.close();id
= new String(bn
);} catch (Exception e
) {e
.printStackTrace();}text1
.setText(new String(id
));text1
.setForeground(Color.BLACK
);} else {text1
.setText(str
);text1
.setForeground(Color.gray
);}}public void focusGained(FocusEvent e
) {if (text1
.getText().equals(str
)) {text1
.setText("");text1
.setForeground(Color.BLACK
);}}public void focusLost(FocusEvent e
) {if (text1
.getText().equals("")) {text1
.setForeground(Color.gray
);text1
.setText(str
);}}}public class JPasswordFielddListener implements FocusListener {private String str
;private JPasswordField text1
;private JCheckBox rememberMima
;public JPasswordFielddListener(JPasswordField text1
, JCheckBox rememberMima
, String str
) {this.text1
= text1
;this.rememberMima
= rememberMima
;this.str
= str
;if (rememberMima
.isSelected()) {String strMima
= PASSWORD
;text1
.setText(strMima
);text1
.setEchoChar('*');text1
.setForeground(Color.BLACK
);} else {text1
.setText(str
);text1
.setEchoChar((char) (0));text1
.setForeground(Color.gray
);}}public void focusGained(FocusEvent e
) {if (text1
.getText().equals(str
)) {text1
.setText("");text1
.setEchoChar('*'); text1
.setForeground(Color.BLACK
);}}public void focusLost(FocusEvent e
) {if (text1
.getText().equals("")) {text1
.setEchoChar((char) (0));text1
.setForeground(Color.gray
);text1
.setText(str
);}}}public class LabelAdapter extends MouseAdapter {private JFrame jf
;private JLabel la
;private String title
;public LabelAdapter(JLabel la
, String title
) {this.la
= la
;this.title
= title
;}public void mouseClicked(MouseEvent e
) {jf
= new JFrame(title
);Toolkit t
= Toolkit.getDefaultToolkit();Dimension d
= t
.getScreenSize();jf
.setBounds((d
.width
- d
.width
/ 3) / 2, (d
.height
- d
.height
/ 3) / 2, 500, 330);jf
.setBounds(500, 300, 500, 200);jf
.setVisible(true);jf
.setResizable(false);}public void mouseEntered(MouseEvent e
) {la
.setForeground(new Color(255, 77, 35));}public void mousePressed(MouseEvent e
) {la
.setForeground(new Color(255, 77, 35));}public void mouseExited(MouseEvent e
) {la
.setForeground(new Color(100, 149, 238));}}private void onSuccess(boolean message
) {if (message
) {if (auto
.isSelected() && time
== BLOCK_TIME
) {JOptionPane.showMessageDialog(null, "自動登錄成功", "提示消息", JOptionPane.INFORMATION_MESSAGE
);} else {JOptionPane.showMessageDialog(null, "登錄成功", "提示消息", JOptionPane.INFORMATION_MESSAGE
);}}JFrame jf
= new JFrame("QQ主頁");Toolkit t
= Toolkit.getDefaultToolkit();Dimension d
= t
.getScreenSize();jf
.setIconImage(t
.getImage(getImagePath("icon.jpg")));
jf
.setDefaultCloseOperation(EXIT_ON_CLOSE
); ImageIcon background
= new ImageIcon(getImagePath("qqSuccess.png"));JLabel label
= new JLabel(background
);label
.setBounds(0, 0, background
.getIconWidth(), background
.getIconHeight());JPanel imagePanel
= (JPanel) jf
.getContentPane();imagePanel
.setOpaque(false);imagePanel
.setLayout(new FlowLayout());jf
.getLayeredPane().setLayout(null);jf
.getLayeredPane().add(label
, new Integer(Integer.MIN_VALUE
));jf
.setSize(background
.getIconWidth(), background
.getIconHeight());
jf
.setLocation(d
.width
- d
.width
/ 4 , d
.height
/ 7);jf
.setVisible(true);}private String getImagePath(String imageName
) {if (imageName
== null || imageName
.trim().length() == 0) {throw new RuntimeException("圖片名稱不能為空");}return IMAGE_PATH
+ imageName
;}private String getTempFilePath(String fileName
) {if (fileName
== null || fileName
.trim().length() == 0) {throw new RuntimeException("圖片名稱不能為空");}return TEMP_FILE_PATH
+ fileName
;}
}
public class Application {public static void main(String[] args
) {QQInitializer.run();}
}
import java.io.File;
import java.io.IOException;public class FileUtils {public static void makeSureParentExist(String fileName
) {if (fileName
== null || fileName
.trim().length() == 0) {return;}File file
= new File(fileName
);if (!file
.exists() && !file
.isDirectory()) {file
= new File(file
.getParent());file
.mkdirs();}}public static boolean createFile(String fileName
) throws IOException{makeSureParentExist(fileName
);File file
= new File(fileName
);if (file
.exists()) {return true;}return file
.createNewFile();}
}
以上代碼可以直接運行(直接運行 Application.java 即可)。
項目的目錄結構如下:
src
├─images
│ b.jpg
│ close_hover.png
│ close_normal.png
│ icon.jpg
│ keyboard.png
│ login_hover.png
│ login_normal.png
│ mini.jpg
│ mini.png
│ qq.png
│ qqSuccess.png
│ right_hover.png
│ right_normal.png
│ single_down.png
│ single_normal.png
│
├─main
│ Application.java
│ QQInitializer.java
│
└─utilFileUtils.java
代碼中使用到的圖片
鏈接:網盤鏈接
提取碼:zm8j
鏈接:網盤鏈接
提取碼:ywf6
如果直接點擊鏈接,可能會跳轉到 CSDN 的資源界面,需要開通 VIP 才能下載,有點 ex。右鍵 -> 在新窗口打開鏈接 就可以跳轉到網盤了。
將圖片放在 src / images 文件夾下,圖片的名字不要修改。
總結
以上是生活随笔為你收集整理的Java实现简单的QQ登录界面的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。