java swing实现QQ账号密码输入框

作者:MH_ANG 时间:2023-01-13 06:26:52 

本文实例为大家分享了Java swing 仿QQ账号密码输入框,供大家参考,具体内容如下

主要思路是自己定义 AccountPanel 和 PasswordPanel 类,继承JPanel,通过 paintComponent(Graphics g) 方法重绘面板,实现目标样式。

先上效果图

正常显示效果:

java swing实现QQ账号密码输入框

鼠标放在控件上时的效果:

java swing实现QQ账号密码输入框

腾讯QQ登录界面的账号密码输入部分效果比对:

java swing实现QQ账号密码输入框

下面直接给出代码吧

AccountPanel:


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JPanel;

public class AccountPanel extends JPanel{

private static final long serialVersionUID = 1L;

private Color normalc=Color.LIGHT_GRAY;
private Color borderc=Color.LIGHT_GRAY;
private boolean flag=false;

public AccountPanel(){}

public AccountPanel(Color normalc,Color borderc){
this.normalc=normalc;
this.borderc=borderc;
}

public void setBorder(boolean flag){
this.flag=flag;
}

@Override
public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2=(Graphics2D) g;

//设置绘图质量
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

//画一个顶部两个角为圆角,底部两个角为直角的矩形
g2.setColor(Color.white);
g2.fillRoundRect(0, 0, 190, 30, 10, 10);
g2.fillRect(0, 10, 190, 17);

//为该矩形添加边框
if(flag){
 g2.setStroke(new BasicStroke(2.0f));
 g2.setColor(borderc);
 g2.drawArc(1, 1, 10, 10, 180, -90);//绘制圆弧
 g2.drawLine(5, 1, 185, 1);
 g2.drawArc(179, 1, 10, 10, 90, -90);//绘制圆弧
 g2.drawLine(1, 5, 1, 29);
 g2.drawLine(189, 5, 189, 29);
 g2.drawLine(1, 29, 190, 29);
}
else{
 g2.setStroke(new BasicStroke(1.0f));
 g2.setColor(normalc);
 g2.drawArc(0, 0, 10, 10, 180, -90);//绘制圆弧
 g2.drawLine(5, 0, 185, 0);
 g2.drawArc(179, 0, 10, 10, 90, -90);//绘制圆弧
 g2.drawLine(0, 5, 0, 30);
 g2.drawLine(189, 5, 189, 30);
 g2.drawLine(0, 30, 190, 30);
}

}

}

PasswordPanel:


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JPanel;

public class PasswordPanel extends JPanel{

private static final long serialVersionUID = 1L;

private Color normalc=Color.LIGHT_GRAY;
private Color borderc=Color.LIGHT_GRAY;
private boolean flag=false;

public PasswordPanel(){}

public PasswordPanel(Color normalc,Color borderc){
this.normalc=normalc;
this.borderc=borderc;
}

public void setBorder(boolean flag){
this.flag=flag;
}

@Override
public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2=(Graphics2D) g;

//设置绘图质量
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

//画一个顶部两个角为圆角,底部两个角为直角的矩形
g2.setColor(Color.white);
g2.fillRoundRect(0, 0, 190, 30, 10, 10);
g2.fillRect(0, 0, 190, 17);

//为该矩形添加边框
//为该矩形添加边框
if(flag){
 g2.setStroke(new BasicStroke(2.0f));
 g2.setColor(borderc);
 g2.drawLine(1, 1, 1, 25);
 g2.drawLine(189, 1, 189, 25);
 g2.drawLine(1, 1, 190, 1);
 g2.drawArc(1, 18, 10, 10, 180, 90);//绘制圆弧
 g2.drawArc(179, 18, 10, 10, 270, 90);//绘制圆弧
 g2.drawLine(5, 29, 185, 29);
}
else{
 g2.setStroke(new BasicStroke(1.0f));
 g2.setColor(normalc);
 g2.drawLine(0, 0, 0, 25);
 g2.drawLine(189, 0, 189, 25);
 g2.drawLine(0, 0, 190, 0);
 g2.drawArc(0, 18, 10, 10, 180, 90);//绘制圆弧
 g2.drawArc(179, 18, 10, 10, 270, 90);//绘制圆弧
 g2.drawLine(5, 29, 185, 29);
}

}
}

下面的代码就是主窗体了

Demo1:


import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Demo1 {

public static void main(String[] args) {

//初始化窗体
JFrame frame=new JFrame("My QQ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setLayout(null);

//添加账号面板
AccountPanel accountPanel=new AccountPanel(Color.LIGHT_GRAY,new Color(56,172,239));
accountPanel.setLayout(null);
accountPanel.setBounds(30, 15, 190, 30);
frame.add( accountPanel);

//在账号面板中添加文本框
JTextField accountTF=new JTextField();
accountTF.setBounds(7, 0, 153, 30);
accountTF.setOpaque(false);
accountTF.setBorder(null);
accountPanel.add(accountTF);

//在账号面板中的文本框后添加按钮
JButton jb1=new JButton(new ImageIcon(
 Demo1.class.getClassLoader().getResource("res/images/btn1.png")));
jb1.setBounds(160, 0, 30, 30);
jb1.setBorder(null);//取消边框
jb1.setBorderPainted(false);//不绘制按钮边框
jb1.setContentAreaFilled(false);//设置按钮背景透明
jb1.setFocusPainted(false);//设置按钮不绘制焦点状态
accountPanel.add(jb1);

//密码面板
PasswordPanel passwordPanel=new PasswordPanel(Color.LIGHT_GRAY,new Color(56,172,239));
passwordPanel.setLayout(null);
passwordPanel.setBounds(30, 45, 190, 30);
frame.add( passwordPanel);

//输入密码文本框
JPasswordField passwordPF=new JPasswordField();
passwordPF.setBounds(7, 0, 153, 30);
passwordPF.setOpaque(false);
passwordPF.setBorder(null);
passwordPanel.add(passwordPF);

//在账号面板中的文本框后添加按钮
JButton jb2=new JButton(new ImageIcon(
 Demo1.class.getClassLoader().getResource("res/images/btn2.png")));
jb2.setBounds(160, 0, 30, 30);
jb2.setBorder(null);
jb2.setBorderPainted(false);
jb2.setContentAreaFilled(false);
jb2.setFocusPainted(false);
passwordPanel.add(jb2);

//为账号输入框添加鼠标 *
MouseAdapter accountListener=new MouseAdapter(){
 @Override
 public void mouseEntered(MouseEvent e) {
 // TODO Auto-generated method stub
 accountPanel.setBorder(true);
 accountPanel.updateUI();
 }

@Override
 public void mouseExited(MouseEvent e) {
 // TODO Auto-generated method stub
 accountPanel.setBorder(false);
 accountPanel.updateUI();
 }
};
accountTF.addMouseListener(accountListener);
jb1.addMouseListener(accountListener);

//为密码输入框添加鼠标 *
MouseAdapter passwordListener=new MouseAdapter(){
 @Override
 public void mouseEntered(MouseEvent e) {
 // TODO Auto-generated method stub
 passwordPanel.setBorder(true);
 passwordPanel.updateUI();
 }

@Override
 public void mouseExited(MouseEvent e) {
 // TODO Auto-generated method stub
 passwordPanel.setBorder(false);
 passwordPanel.updateUI();
 }
};
passwordPF.addMouseListener(passwordListener);
jb2.addMouseListener(passwordListener);

frame.setVisible(true);
}

}

自此,仿QQ登录账号密码输入框就完成了。

来源:https://blog.csdn.net/MH_ANG/article/details/75911633?utm_source=blogxgwz6

标签:java,swing,输入框
0
投稿

猜你喜欢

  • 一文搞懂Android RecyclerView点击展开、折叠效果的实现代码

    2023-02-17 14:52:24
  • Kotlin中常见内联扩展函数的使用方法教程

    2023-07-04 13:46:12
  • 微信Android热更新Tinker使用详解(星空武哥)

    2023-06-08 04:08:17
  • java中 Set与Map排序输出到Writer详解及实例

    2022-05-01 07:23:56
  • 深入了解Java设计模式之策略模式

    2021-06-24 22:45:56
  • Java定时器Timer使用方法详解

    2023-08-25 17:32:20
  • java的NIO管道用法代码分享

    2022-05-01 23:52:53
  • C#如何动态创建lambda表达式

    2022-04-18 21:57:16
  • C#使用FolderBrowserDialog类实现选择打开文件夹方法详解

    2022-08-11 07:28:39
  • Java @Value("${xxx}")取properties时中文乱码的解决

    2023-08-14 06:25:50
  • 一篇文章带你了解C语言二分查找

    2023-10-16 19:51:57
  • 小程序与后端Java接 口交互实现HelloWorld入门

    2023-11-04 18:55:31
  • Android RxJava与Retrofit结合使用详解

    2021-10-19 20:10:53
  • Java利用钉钉机器人实现发送群消息

    2021-06-28 23:37:33
  • Java编程调用微信接口实现图文信息推送功能

    2023-11-25 07:20:47
  • Android中基于XMPP协议实现IM聊天程序与多人聊天室

    2022-05-06 07:37:51
  • Android基于讯飞语音SDK实现语音识别

    2023-09-26 23:39:05
  • C#实现将窗体固定在显示器的左上角且不能移动的方法

    2022-03-04 18:34:17
  • 一文带你了解Java万物之基之Object类

    2023-10-09 01:58:28
  • java中为何重写equals时必须重写hashCode方法详解

    2022-10-24 01:44:39
  • asp之家 软件编程 m.aspxhome.com