📄 statebtn.java
字号:
package my_cpt;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.*;
import super_cpt.SuperBtn;
public class StateBtn extends Label{
/**
* 状态键
*/
private ActionListener myListener;//我的监听器
private final ActionEvent myEvent;//我的事件
protected boolean pressed=false;
protected String label;
private boolean isFlash=false;
public StateBtn(String str)
{
label=str;
myEvent=new ActionEvent(this,ActionEvent.ACTION_PERFORMED,label);
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{//
if(e.getButton()!=MouseEvent.BUTTON1)return;
if(!isEnabled())return;
pressed=true;
repaint();
}
public void mouseReleased(MouseEvent e)
{//
if(e.getButton()!=MouseEvent.BUTTON1)return;
if(!isEnabled())return;
pressed=false;
repaint();
if(myListener!=null)
myListener.actionPerformed(myEvent);
}
public void mouseEntered(MouseEvent e)
{
isFlash=true;
repaint();
//this.m
}
public void mouseExited(MouseEvent e)
{
isFlash=false;
repaint();
}
});
}
public boolean isDoubleBuffered()
{
return true;
}
public void setEnabled(boolean bool)
{
super.setEnabled(bool);
repaint();
}
public void addActionListener(ActionListener e)
{
myListener=e;
}
BufferedImage backImage=null;
Graphics2D g2;
BasicStroke stroke=new BasicStroke(1.2f);
GradientPaint paint=null;
public void update(Graphics g)
{//初始化背景大小
int w=getWidth(),h=getHeight();
if(backImage==null || g2==null){
backImage=this.getGraphicsConfiguration().createCompatibleImage(w,h);
g2=backImage.createGraphics();
g2.setFont(new Font(g2.getFont().getFontName(),Font.PLAIN,13));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
//paint=new GradientPaint(0,0,Color.BLUE,0,h/2,Color.WHITE,true);
}
g2.setColor(Color.WHITE);
g2.fillRect(0,0,w,h);
if(isEnabled())
g2.setColor(Color.MAGENTA);
else
g2.setColor(Color.LIGHT_GRAY);
if(isEnabled() && isFlash)
g2.setColor(Color.GREEN);
if(pressed)
g2.drawString(label,8,16);
else
g2.drawString(label,8,14);
g2.setStroke(stroke);
g2.drawRect(0,0,w-1,h-1);
//g2.setPaint(paint);
//g2.fillRect(w-3,0,3,h-1);
paint(g);
}
public void paint(Graphics g)
{
if(backImage==null)
repaint();
g.drawImage(backImage,0,0,this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -