JAVA SWing实现登陆界面

1、我们先看演示效果

技术分享

2、下面看具体实现,启动eclipse,创建java 工程

3、新建添加类和包名

4、工程中需要导入的系统中的包

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;

 

5、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();

 }
 

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。