📄 alabel.java
字号:
package support;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ALabel extends JLabel implements ActionListener{
/**
* @param args
*/
private String content;//标签内容
private Color backColor;
private Timer t1;//文字特效控制
private Timer t2;//控件尺寸特效控制
private int owidth=0,oheight=0,efcw=0,efch=0;
private Color fontcolor=Color.WHITE;//背景字体颜色
private boolean spcefct=false;//特效控制端
private Font f=new Font("TimesRoman",Font.PLAIN,12);//设置字体
private int left=0,top=0;//设置标签打印位置
private char [] anich;//特效字符
private String anistr="";//特效字符串
private int aniconter=-1;//特效字符串
private boolean recycle=false;//特效循环控制
private int wwidth=0,wheight=0;//设定想要的控件尺寸
//设置构造函数
public ALabel()
{
content="";
init();
}
public ALabel(String str)
{
content=str;
init();
}
public void init()
{
//初始化函数
}
//设定属性控制函数
public void setContent(String str)
{/*设定控件中显示的文字*/
content=str;
}
public String getContent()
{
return content;
}
public void setPrintPos(int x,int y)
{
left=x;top=y;
}
public int getLeft()
{
return left;
}
public int getTop()
{
return top;
}
public void setWantedSize(int w,int h)
{/*设定想要的控件初始大小*/
wwidth=w;wheight=h;
}
public int getWantedWidth()
{
return wwidth;
}
public int getWantedHeight()
{
return wheight;
}
public void setFontColor(Color c)
{/*设定背景字体颜色*/
fontcolor=c;
}
public Color getFontColor()
{
return fontcolor;
}
public void setBackColor(Color c)
{/*设定背景颜色*/
backColor=c;
}
public Color getBackColor()
{
return backColor;
}
public void setFont(Font font)
{//设定控件字体
f=font;
}
public Font getFont()
{
return f;
}
public void setFontSize(int size)
{//设定字体大小
f=new Font("TimesRoman",Font.PLAIN,size);
}
public int getFontSize()
{
return f.getSize();
}
public void setRecycle(boolean b)
{/*设定该控件特效是否可重复*/
recycle=b;
}
public boolean getRecycle()
{
return recycle;
}
/*文字功能辅助函数*/
public String getTrimed(String str,char c)
{
/*过滤掉句子中的选定字符*/
char[] ch=str.toCharArray();
String temp="";
for(int i=0;i<ch.length;i++)
{
if(ch[i]!=c)
{
temp+=ch[i];
}
}
return temp;
}
public String [] getPartStr(String str,char c)
{
/*使用规定字符将所传入的字符串分段*/
char[] ch=str.toCharArray();
int conter=0;
for(int i=0;i<ch.length;i++)
{/*清点共有多少处特殊断点*/
if(ch[i]==c)
{
conter+=1;
}
}
String []part=new String[conter+1];
part[conter]="";
int conter2=0;
for(int k=0;k<ch.length;k++)
{/*对字符串进行分段处理*/
if(ch[k]==c)
{
part[conter2]=part[conter];
conter2+=1;
part[conter]="";
continue;
}
part[conter]+=ch[k];
}
return part;
}
public String getStrSized(String str,char c,int width)
{
//按照控件的大小设定字符串的断点
int font=this.getFont().getSize(),
div=2*(width-font/2)/(font);
char [] ch=str.toCharArray();
String tempstr="";
try{
for(int i=1;i<ch.length+1;i++)
{
tempstr+=ch[i-1];
if(i!=1&&i%div==0)
{
tempstr+=c;
}
}}catch(Exception e){}
return tempstr;
}
/*特效控制函数*/
public void Start(int interval)
{/*开启控件特效*/
t1=new Timer(interval,this);
t1.start();
spcefct=true;
anich=content.toCharArray();//为特效字符串设置
anistr="";
aniconter=-1;
this.repaint();//重新绘制标签中的字体
}
public void Stop()
{/*关闭特效*/
t1.stop();
spcefct=false;
anistr="";
aniconter=-1;
this.repaint();
}
public void Pause()
{//暂停特效
t1.stop();
this.repaint();
}
public void Restart(int interval)
{//重启特效
t1=new Timer(interval,this);
t1.start();
this.repaint();
}
public void SizeStart(int interval)
{
t2=new Timer(interval,this);
owidth=wwidth;
oheight=wheight;
efcw=0;
efch=0;
this.setSize(efcw,efch);
t2.start();
}
public void actionPerformed(ActionEvent e)
{/*时间特效处理*/
if(e.getSource().equals(t1))
{
if(aniconter<anich.length-1)
{//如果不超出范围那么计数器加1
aniconter+=1;
}else
{
if(recycle)
{//如果超出范围且允许循环那么计数器清灵并且字符串清零
aniconter=0;
anistr="";
}else{
//如果超出范围且不允许循环那么停止计数器
this.Stop();}
}
try{//捕捉异常,防止数组超出范围
anistr+=anich[aniconter];}catch(Exception exp){}
this.repaint();
//System.out.println("aniconter="+aniconter+"\t");
}
if(e.getSource().equals(t2))
{
if(efcw<owidth)
{
efcw+=15;
}
if(efch<oheight)
{
efch=(oheight*efcw)/owidth;
}
this.setSize(efcw,efch);
if(efcw>=owidth&&efch>=oheight)
{
t2.stop();
this.setSize(owidth,oheight);
}
}
}
public void strpack()
{
//设定此函数以使得标签能够容纳全部字体
int height=this.getPartStr
(this.getStrSized(content,'\n',this.getWidth()),'\n').length;
int h=height*(5+this.getFontSize());
this.setSize(this.getWidth(),h);
}
public void DrawString(Graphics g,String str,int x,int y)
{/*新控件中的打印字符串功能函数*/
String []s=this.getPartStr(this.getStrSized(str,'\n',this.getWidth()),'\n');
int ldiv=g.getFont().getSize();
for(int i=0;i<s.length;i++)
g.drawString(s[i],x,y+i*ldiv);
}
public void paint(Graphics g)
{/*重载绘图功能*/
/*设定背景颜色*/
if(backColor!=null){
g.setColor(backColor);
g.fillRect(0,0,this.getWidth(),this.getHeight());}
/*背景图片控制部分*/
super.paint(g);
//字体内容控制部分
g.setColor(fontcolor);
g.setFont(f);
int pos=g.getFont().getSize();//取得字体大小
if(!spcefct)
{
this.DrawString(g,content,left+pos/2,top+pos);
}else
{
this.DrawString(g,anistr,left+pos/2,top+pos);
}
/*绘图试验*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -