《java---高仿QQ登陸介面》---完全手寫
阿新 • • 發佈:2019-01-24
package com.fenghuo.view; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JRootPane; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.UIManager; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; import com.fenghuo.utils.ViewUthil; public class LoginView extends JFrame{ private static final long serialVersionUID = 7410365204674471699L; private JLabel username_JLabel = null; private JLabel password_JLabel = null; private JLabel forgotPwd_JLabel = null; private JLabel register_JLabel = null; private JLabel login_JLabel = null; private JLabel close_JLabel = null; private JLabel head_JLabel = null; private JLabel advert_JLabel = null; private JPanel up_JPanel = null; private JPanel south_JPanel = null; private JPanel north_JPanel = null; private JPanel west_JPanel = null; private JTextField username_JTextField = null; private JTextField password_JTextField = null; private JButton login_JButton = null; private JButton cancel_JButton = null; private JCheckBox remeber_JCheckBox = null; private JCheckBox hide_JCheckBox = null; private JCheckBox auto_JCheckBox = null; Icon login_ImageIcon_normal = new ImageIcon(getClass().getResource("/com/fenghuo/resource/button_normal.png")); Icon login_ImageIcon_press = new ImageIcon(getClass().getResource("/com/fenghuo/resource/button_press.png")); Icon login_ImageIcon_normal_press = new ImageIcon(getClass().getResource("/com/fenghuo/resource/button_normal_press.png")); Icon close_ImageIcon_normal = new ImageIcon(getClass().getResource("/com/fenghuo/resource/btn_close_normal.png")); Icon close_ImageIcon_down = new ImageIcon(getClass().getResource("/com/fenghuo/resource/btn_close_down.png")); Icon close_ImageIcon_highlight = new ImageIcon(getClass().getResource("/com/fenghuo/resource/btn_close_highlight.png")); Icon head_ImageIcon_login = new ImageIcon(getClass().getResource("/com/fenghuo/resource/head_img.png")); Icon advert_ImageIcon_login = new ImageIcon(getClass().getResource("/com/fenghuo/resource/advert_img.png")); Icon mima_ImageIcon_press = new ImageIcon(getClass().getResource("/com/fenghuo/resource/mima_press.png")); Icon mima_ImageIcon_hover = new ImageIcon(getClass().getResource("/com/fenghuo/resource/mima_hover.png")); Icon zhuce_ImageIcon_press = new ImageIcon(getClass().getResource("/com/fenghuo/resource/zhuce_press.png")); Icon zhuce_ImageIcon_hover = new ImageIcon(getClass().getResource("/com/fenghuo/resource/zhuce_hover.png")); ImageIcon background = new ImageIcon(getClass().getResource("/com/fenghuo/resource/background_blue.png")); JLabel backImage; JPanel backPanel; private int WIDTH; private int HEIGHT; // 滑鼠的在空件上的位置 public static int M_X; public static int M_Y; public LoginView(){ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } // 初始化介面範圍 this.WIDTH = background.getIconWidth(); this.HEIGHT = background.getIconHeight(); // 設定整體的背景圖 backImage = new JLabel(background); backImage.setBounds(0, 0, background.getIconWidth(),background.getIconHeight()); backPanel = (JPanel) this.getContentPane(); backPanel.setOpaque(false); this.getLayeredPane().setLayout(null); this.getLayeredPane().add(backImage, new Integer(Integer.MIN_VALUE)); backPanel.setLayout(new BorderLayout()); // 北部佈局 north_JPanel = new JPanel(); north_JPanel.setLayout(null); north_JPanel.setOpaque(false); north_JPanel.setPreferredSize(new Dimension(background.getIconWidth(), (close_ImageIcon_normal.getIconHeight())+90)); close_JLabel = new JLabel(close_ImageIcon_normal); close_JLabel.setBounds(this.WIDTH-close_ImageIcon_normal.getIconWidth(), 0, close_ImageIcon_normal.getIconWidth(), close_ImageIcon_normal.getIconHeight()); advert_JLabel = new JLabel(advert_ImageIcon_login); advert_JLabel.setBounds(1, close_ImageIcon_normal.getIconHeight()+5, advert_ImageIcon_login.getIconWidth(), advert_ImageIcon_login.getIconHeight()); north_JPanel.add(close_JLabel); north_JPanel.add(advert_JLabel); // 西部佈局 west_JPanel = new JPanel(null); west_JPanel.setOpaque(false); west_JPanel.setPreferredSize(new Dimension(head_ImageIcon_login.getIconWidth()+25, head_ImageIcon_login.getIconHeight())); head_JLabel = new JLabel(head_ImageIcon_login); head_JLabel.setBounds(20, 10, head_ImageIcon_login.getIconWidth(), head_ImageIcon_login.getIconHeight()); west_JPanel.add(head_JLabel); // 中間佈局 up_JPanel = new JPanel(new GridLayout(3,3)); up_JPanel.setOpaque(false); // up_JPanel.setPreferredSize(new Dimension(250, 100)); username_JLabel = new JLabel("賬號",JLabel.CENTER); password_JLabel = new JLabel("密碼",JLabel.CENTER); forgotPwd_JLabel = new JLabel(mima_ImageIcon_press, JLabel.CENTER); register_JLabel = new JLabel(zhuce_ImageIcon_press, JLabel.CENTER); username_JTextField = new JTextField(); username_JTextField.setPreferredSize(new Dimension(150, 10)); password_JTextField = new JPasswordField(10); remeber_JCheckBox = new JCheckBox("記住密碼"); hide_JCheckBox = new JCheckBox("隱身登陸"); auto_JCheckBox = new JCheckBox("自動登陸"); remeber_JCheckBox.setOpaque(false); hide_JCheckBox.setOpaque(false); auto_JCheckBox.setOpaque(false); up_JPanel.setBackground(new Color(202, 231, 244)); up_JPanel.add(username_JLabel); up_JPanel.add(username_JTextField); up_JPanel.add(forgotPwd_JLabel); up_JPanel.add(password_JLabel); up_JPanel.add(password_JTextField); up_JPanel.add(register_JLabel); up_JPanel.add(remeber_JCheckBox); up_JPanel.add(auto_JCheckBox); up_JPanel.add(hide_JCheckBox); // 南部佈局 south_JPanel = new JPanel(); south_JPanel.setOpaque(false); south_JPanel.setPreferredSize(new Dimension(WIDTH, 60)); login_JLabel = new JLabel(login_ImageIcon_normal); south_JPanel.add(login_JLabel); // 新增面板到窗體 this.add(north_JPanel, "North"); this.add(up_JPanel, "Center"); this.add(west_JPanel, "West"); this.add(south_JPanel, "South"); // 去掉窗體的樣式 this.setUndecorated(true); // this.getRootPane().setWindowDecorationStyle(JRootPane.QUESTION_DIALOG); // this.setExtendedState(this.ICONIFIED); //最小化 // this.setExtendedState(JFrame.MAXIMIZED_BOTH); //最大化 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(ViewUthil.getPositionX(400), ViewUthil.getPositionY(300), background.getIconWidth(), background.getIconHeight()); this.setVisible(true); MyEvent(this); } private void MyEvent(final JFrame jFrame){ // 登陸按鈕滑鼠事件 login_JLabel.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { login_JLabel.setIcon(login_ImageIcon_normal_press); } @Override public void mouseReleased(MouseEvent e) { login_JLabel.setIcon(login_ImageIcon_press); } @Override public void mouseEntered(MouseEvent e) { login_JLabel.setIcon(login_ImageIcon_press); } @Override public void mouseExited(MouseEvent e) { login_JLabel.setIcon(login_ImageIcon_normal); } }); // 關閉按鈕相關事件 close_JLabel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { System.exit(0); } @Override public void mousePressed(MouseEvent e) { close_JLabel.setIcon(close_ImageIcon_down); } @Override public void mouseExited(MouseEvent e) { close_JLabel.setIcon(close_ImageIcon_normal); } @Override public void mouseEntered(MouseEvent e) { close_JLabel.setIcon(close_ImageIcon_highlight); } }); // 忘記密碼標籤 forgotPwd_JLabel.addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent e) { forgotPwd_JLabel.setIcon(mima_ImageIcon_press); } @Override public void mouseReleased(MouseEvent e) { forgotPwd_JLabel.setIcon(mima_ImageIcon_hover); } @Override public void mouseEntered(MouseEvent e) { forgotPwd_JLabel.setIcon(mima_ImageIcon_hover); } @Override public void mouseExited(MouseEvent e) { forgotPwd_JLabel.setIcon(mima_ImageIcon_press); }}); // 註冊 register_JLabel.addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent e) { register_JLabel.setIcon(zhuce_ImageIcon_press); } @Override public void mouseReleased(MouseEvent e) { register_JLabel.setIcon(zhuce_ImageIcon_hover); } @Override public void mouseEntered(MouseEvent e) { register_JLabel.setIcon(zhuce_ImageIcon_hover); } @Override public void mouseExited(MouseEvent e) { register_JLabel.setIcon(zhuce_ImageIcon_press); }}); // 北部佈局新增事件 north_JPanel.addMouseMotionListener(new MouseMotionAdapter() { // 按下並拖動 public void mouseDragged(MouseEvent e) { int X = LoginView.M_X; int Y = LoginView.M_Y; jFrame.setLocation(e.getXOnScreen()-X, e.getYOnScreen()-Y); // System.out.println(e.getX()); } }); north_JPanel.addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent e) { LoginView.M_X = e.getX(); LoginView.M_Y = e.getY(); } }); } }