仿QQ登录界面(JAVA GUI)

仿QQ登录界面(JAVA GUI)

需求:仿QQ登录界面(未添加事件监听,只有界面)

分析:

布局如图所示:

实现:

package com.login;

import java.awt.*;

import javax.swing.*;


public class Login extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static final String LOG_TITLE="登录界面设计";
	public static final int WINDOW_WIDTH=430;
	public static final int WINDOW_HEIGHT=330;
	
	
	
	public static void login(){
		Login login=new Login();
		login.setTitle(LOG_TITLE);
		login.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		login.setLocationRelativeTo(null);

		login.setUndecorated(true);   //设置frame边框不可见   
		login.setResizable(false);    //禁止改变窗口大小

		BorderLayout border_layout=new BorderLayout();
		login.setLayout(border_layout);

		/**
		 * 北部面板
		 */
		JPanel panel_north=CreatePanel.CreateNorthPanel();
		login.add(panel_north,BorderLayout.NORTH);

		/**
		 * 中部面板
		 */
		JPanel panel_center=CreatePanel.CrateCenterPanel();
		login.add(panel_center,BorderLayout.CENTER);


		/**
		 * 南部面板
		 */
		JPanel panel_south=CreatePanel.CreateSouthPanel();
		login.add(panel_south,BorderLayout.SOUTH);


		login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		login.setVisible(true);
	}

	public static void main(String[] args) {
		login();
	}

}

创建面板类

package com.login;

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

import javax.swing.*;

public class CreatePanel extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static final int WINDOW_WIDTH=430;
	public static final int WINDOW_HEIGHT=330;
	
	/**
	 * 创建北部面板
	 * @return
	 */
	public static JPanel CreateNorthPanel(){
		JPanel panel=new JPanel();
		panel.setLayout(null);
		panel.setPreferredSize(new Dimension(430, 180));
		//图片大小440x210
		ImageIcon image=new ImageIcon("images/top.jpg");
		JButton close=new JButton("✖");
		
		close.setContentAreaFilled(false);  //设置按钮透明
		
		JLabel background=new JLabel(image);
		
		background.setBounds(0,0,430,180);   
		close.setBounds(380, 0, 50,30);
		close.setForeground(Color.white);
		panel.add(close);
		panel.add(background);
		
		close.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		return panel;
	}
	
	
	/**
	 * 创建南部面板
	 */
	public static JPanel CreateSouthPanel(){
		JPanel panel=new JPanel();
		panel.setLayout(null);
		panel.setPreferredSize(new Dimension(420, 40));
	
		MyLineBorder myLineBorder = new MyLineBorder(new Color(192, 192, 192), 1 , true);
		
		/**
		 * 登录按钮
		 */
		ImageIcon image=new ImageIcon("images/02.jpg");
		JButton btn=new JButton(image);
		btn.setBounds(128,0,image.getIconWidth()-10,image.getIconHeight()-10);		
		btn.setBorder(myLineBorder);
				
		panel.add(btn);
		return panel;
	}
	
	/**
	 * 创建中部面板
	 */
	public static JPanel CrateCenterPanel(){
		JPanel panel=new JPanel();
		panel.setLayout(null);
		panel.setPreferredSize(new Dimension(420, 160));
		MyLineBorder myLineBorder = new MyLineBorder(new Color(192, 192, 192), 1 , true);
		
		
		JLabel JLUserName = new JLabel("用户名:");
		JLabel JLUserPaw = new JLabel("密  码:");
		JLUserName.setBounds(65, 20, 80, 20);
		JLUserName.setForeground(new Color(0,0,0));
		JLUserName.setFont(new Font("楷体",0,16));
		JLUserPaw.setBounds(65, 50, 60, 20);
		JLUserPaw.setForeground(new Color(0,0,0));
		JLUserPaw.setFont(new Font("楷体",0,16));
		
		/**
		 * 用户名框
		 */
		JTextField username=new JTextField();
		username.setBounds(130, 15, 175, 30);
		username.setBorder(myLineBorder);
 
		/**
		 * 密码框
		 */
		JPasswordField password=new JPasswordField(JPasswordField.LEFT);
		password.setBounds(130, 44, 175, 30);
		password.setBorder(myLineBorder);
		
		/**
		 * 注册
		 */
		JLabel regeist=new JLabel("注册");
		regeist.setForeground(new Color(100,149,238));
		regeist.setBounds(310, 20, 30, 20);
		regeist.setFont(new Font("宋体",0,14));
		
		/**
		 * 注册
		 */
		JLabel reset=new JLabel("重置");
		reset.setForeground(new Color(100,149,238));
		reset.setBounds(310, 50, 30, 20);
		reset.setFont(new Font("宋体",0,14));
		
		
		panel.add(JLUserName);
		panel.add(JLUserPaw);
		panel.add(username);
		panel.add(password);
		panel.add(regeist);
		panel.add(reset);
		
		
		return panel;
	}

}

工具类(圆边框):来源网络

package com.login;

import java.awt.*;

import javax.swing.border.LineBorder;

public class MyLineBorder extends LineBorder{  
  
  
    private static final long serialVersionUID = 1L;  
      
    public MyLineBorder(Color color, int thickness, boolean roundedCorners) {  
        super(color, thickness, roundedCorners);  
    }  
  
     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {  
           
         RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,  
                                                RenderingHints.VALUE_ANTIALIAS_ON);   
         Color oldColor = g.getColor();  
         Graphics2D g2 = (Graphics2D)g;  
         int i;  
         g2.setRenderingHints(rh);  
         g2.setColor(lineColor);  
         for(i = 0; i < thickness; i++)  {  
         if(!roundedCorners)  
             g2.drawRect(x+i, y+i, width-i-i-1, height-i-i-1);  
         else  
             g2.drawRoundRect(x+i, y+i, width-i-i-1, height-i-i-1, 5, 5); 
         }  
         g2.setColor(oldColor);  
    }  
}  

结果:

猜你喜欢

转载自blog.csdn.net/qq_40270579/article/details/82556095