JAVA SWing實現登錄界面

一、咱們先看演示效果java

二、下面看具體實現,啓動eclipse,建立java 工程服務器

三、新建添加類和包名eclipse

四、工程中須要導入的系統中的包ide

import java.awt.*;             this

import javax.swing.*;orm

//對按鍵監控須要用到的包server

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;blog

 

//對鼠標操做監控須要用到的包
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;事件

 

五、java 代碼以下:工程正常工做,須要加入背景圖片和logo圖片,背景圖片大小是600*544, logo圖片大小是150*150,圖片所在目錄是工程所在目錄的子目錄img中圖片

 

package com.fnn.login;

/*import com.fnn.main.MainFrame;*/

//所須要的包

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

 


public class LoginFrame extends JFrame {

 Color logoPaneColor = new Color(230, 230, 250);
 Color mainPaneColor = new Color(230, 230, 250);
 Color mainFrameColor = new Color(186, 110 ,64);
 Color logoFramColor = new Color(186, 110, 64);
 JLayeredPane layeredPane = new JLayeredPane();                // 面板層
 ImageIcon bgImage = new ImageIcon("./img/bgImage.png");        // 背景圖片
 ImageIcon lgImage  =new  ImageIcon("./img/lgImage.png");
 JPanel bgPanel = new JPanel();                              // 背景面板
 JPanel mainPanel = new JPanel();                             // 登錄面板
 JPanel logoPanel  = new JPanel();                                                   //企業logo
 final JLabel server_ip = new  JLabel("服務器:");                        
 final JLabel user_name = new JLabel("用戶名:");
 final JLabel user_password = new JLabel("密   碼:");
 JButton button_ok = new JButton("登錄");                      // 確認按鈕
 JButton button_cansel = new JButton("取消");                  //取消按鈕
 JComboBox text_ip   = null;                                                  //服務器IP和端口號
 JTextField text_name = new JTextField();          // 登錄用戶名
 JPasswordField text_password = new JPasswordField();    // 登錄密碼

 private static final long serialVersionUID = 1L;
 // 用於處理拖動事件,表示鼠標按下時的座標,相對於JFrame
 int xOld = 0;
 int yOld = 0;

 class LoginOKAction implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   setVisible(false);
   // new MainFrame().setVisible(true);

  }
  
 }
 
 class LoginCanselAction implements ActionListener {
  public void actionPerformed(ActionEvent e) {
   
   System.exit(0);
   
  }
  
 }
 

 public LoginFrame() {
  super();
  initialize();
 }

 public void initialize() {
  this.setLayout(null);
  // 處理拖動事件
  this.addMouseListener(new MouseAdapter() {
   @Override
   public void mousePressed(MouseEvent e) {
    xOld = e.getX();
    yOld = e.getY();
   }
  });
  this.addMouseMotionListener(new MouseMotionAdapter() {
   @Override
   public void mouseDragged(MouseEvent e) {
    int xOnScreen = e.getXOnScreen();
    int yOnScreen = e.getYOnScreen();

    int xx = xOnScreen - xOld;
    int yy = yOnScreen - yOld;
    LoginFrame.this.setLocation(xx, yy);
   }
  });

 

  layeredPane.setBounds(0, 0, 600, 544);
  this.add(layeredPane);

  // 背景Panel
  bgPanel.setBounds(0, 0, 600, 544);
  layeredPane.add(bgPanel, new Integer(Integer.MIN_VALUE));

  // 背景圖片,添加到背景Panel裏面
  JLabel bgLabel = new JLabel(bgImage);
  bgPanel.add(bgLabel);
  JPanel jp = (JPanel) this.getContentPane();
  jp.setOpaque(false);

 

 //logo界面, 傳播企業文化
  logoPanel.setBounds(50, 150,  150,  170);
  logoPanel.setLayout(null);
  logoPanel.setBackground(Color.WHITE);
  logoPanel.setBorder(BorderFactory.createTitledBorder(""));
 // logoPanel.setBorder(BorderFactory.createLineBorder(logoFramColor)); 
  layeredPane.add(logoPanel);
  
  JLabel logo = new JLabel(lgImage);
  logoPanel.add(logo);
  logo.setBounds(new Rectangle(0, 0,  150,  150));
 
  
  // 主界面,登錄界面,包含服務器ip,用戶名,密碼等

  mainPanel.setBounds(200, 150, 350, 170);
  mainPanel.setLayout(null);
  mainPanel.setBackground(mainPaneColor);
  mainPanel.setBorder(BorderFactory.createTitledBorder(""));
//  mainPanel.setBorder(BorderFactory.createLineBorder(mainFrameColor));
  layeredPane.add(mainPanel);

  mainPanel.add(server_ip);
  server_ip.setBounds(new Rectangle(25, 40,  62,  25));
  server_ip.setFont(new java.awt.Font("微軟雅黑", 1, 14));
  
  
  mainPanel.add(user_name);
  user_name.setBounds(new Rectangle(25, 70, 62, 25));
  user_name.setFont(new java.awt.Font("微軟雅黑", 1, 14));

  
  mainPanel.add(user_password);
  user_password.setBounds(new Rectangle(25, 100, 62, 25));
  user_password.setFont(new java.awt.Font("微軟雅黑", 1, 14));

  
  String[] default_ip={"127.0.0.1:8080"};
  text_ip = new JComboBox(default_ip);
  mainPanel.add(text_ip);
  text_ip.setBounds(new Rectangle(95, 40, 240 , 25));
  text_ip.setFont(new java.awt.Font("微軟雅黑", 1, 12));
  text_ip.setEditable(true);
 
  
  mainPanel.add(text_name);
  text_name.setBounds(new Rectangle(95, 70, 240, 25));
  text_name.setFont(new java.awt.Font("微軟雅黑", 1, 12));

  mainPanel.add(text_password);
  text_password.setBounds(new Rectangle(95, 100, 240, 25));
  text_password.setFont(new Font("", Font.PLAIN, 30)); // 設置回顯字符大小

  mainPanel.add(button_ok);
  button_ok.setBounds(new Rectangle(115, 130,  80, 25));
  button_ok.addActionListener(new LoginOKAction()); // 給按鈕加事件監聽器
  button_ok.setFont(new java.awt.Font("微軟雅黑", 1, 12));
  
  mainPanel.add(button_cansel);
  button_cansel.setBounds(new Rectangle(225, 130,  80, 25));
  button_cansel.addActionListener(new LoginOKAction()); // 給按鈕加事件監聽器
  button_cansel.setFont(new java.awt.Font("微軟雅黑", 1, 12));
  

  this.setBounds(0, 0, 600, 544);
  this.setUndecorated(true);
  this.setLocationRelativeTo(null);
  this.setVisible(true);
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new LoginFrame();

 }
 

}

相關文章
相關標籤/搜索