java swing实现QQ账号密码输入框
程序员文章站
2024-01-15 20:03:46
本文实例为大家分享了java swing 仿qq账号密码输入框,供大家参考,具体内容如下
主要思路是自己定义 accountpanel 和 passwordpanel 类...
本文实例为大家分享了java swing 仿qq账号密码输入框,供大家参考,具体内容如下
主要思路是自己定义 accountpanel 和 passwordpanel 类,继承jpanel,通过 paintcomponent(graphics g) 方法重绘面板,实现目标样式。
先上效果图
正常显示效果:
鼠标放在控件上时的效果:
腾讯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登录账号密码输入框就完成了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。