这是我的目标。我扩展了JButton并覆盖了paintComponent方法,创建了我所需的圆角按钮效果和当鼠标滚过按钮时的颜色渐变效果。一切都很好。但是,JButton仍在绘制一个白色矩形区域,如图所示。
我希望 1) 去掉白色角落,2) 按钮的中心显示其后面的面板。我已经尝试过以下方法:
1- 绘制按钮时使用getParent().getBackground(),并首先绘制按钮。这对不透明面板非常有效。但是,我希望此按钮能在部分或完全透明的面板上工作。对于透明面板,它会绘制颜色,但在白色背景上隐藏面板后面的任何内容(如图像)。
2- 我尝试了许多组合,例如setOpaque(false)或setContentAreaFilled(false)。我尝试了在调用super.paintComponent(g)时和不调用它时使用它们。但是,这些都似乎不起作用。
3- 当我不使用g2.clearRect(0,0,width,height)方法(在绘制之前清除图形区域)时,按钮看起来正确。但由于图形对象从未被覆盖,淡入效果在按钮滚动一次后停止工作。
4- 我使用JLabel来设置文本,并尝试了设置它的opaque或仅不使用它,但问题仍然存在。因此,我认为这不是问题所在。
由于我只想为JButton而不是其他swing组件添加效果,因此我真的希望避免制作自己的ButtonUI。
谢谢,希望这很清楚。以下是我的按钮代码:
我希望 1) 去掉白色角落,2) 按钮的中心显示其后面的面板。我已经尝试过以下方法:
1- 绘制按钮时使用getParent().getBackground(),并首先绘制按钮。这对不透明面板非常有效。但是,我希望此按钮能在部分或完全透明的面板上工作。对于透明面板,它会绘制颜色,但在白色背景上隐藏面板后面的任何内容(如图像)。
2- 我尝试了许多组合,例如setOpaque(false)或setContentAreaFilled(false)。我尝试了在调用super.paintComponent(g)时和不调用它时使用它们。但是,这些都似乎不起作用。
3- 当我不使用g2.clearRect(0,0,width,height)方法(在绘制之前清除图形区域)时,按钮看起来正确。但由于图形对象从未被覆盖,淡入效果在按钮滚动一次后停止工作。
4- 我使用JLabel来设置文本,并尝试了设置它的opaque或仅不使用它,但问题仍然存在。因此,我认为这不是问题所在。
由于我只想为JButton而不是其他swing组件添加效果,因此我真的希望避免制作自己的ButtonUI。
谢谢,希望这很清楚。以下是我的按钮代码:
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
/**
* WButton.java
*
* An extension of JButton but with custom graphics
*
*/
public class WButton extends JButton{
private Timer timer;
private float[] background = {.3f,.6f,.8f,0f};
private boolean fadeUp = true;
private boolean fadeDown = false;
private JLabel label;
/**
* Default Constructor
*/
public WButton(){
super();
label = new JLabel();
setupButton();
}
/**
* Text constructor
*/
public WButton(String text){
super(text);
label = new JLabel(text);
setupButton();
}
/**
* common setup functions
*/
private void setupButton(){
timer = new Timer(24,new TimerAction(this));
label.setLabelFor(this);
add(label);
}
/**
* Set the background color
*/
@Override
public void setBackground(Color bg){
background = bg.getRGBComponents(background);
background[3] = 0f;
super.setBackground(new Color(background[0],background[1],
background[2],background[3]));
repaint();
}
/**
* get background
*/
@Override
public Color getBackground(){
if(background!=null)
return new Color(background[0],background[1],background[2],background[3]);
return new Color(.5f,.5f,.5f);
}
/**
* Set the font of the button
*/
@Override
public void setFont(Font font){
super.setFont(font);
if(label!=null)
label.setFont(font);
}
/**
* Override the set text method
*/
@Override
public void setText(String t){
super.setText(t);
if(label!=null)
label.setText(t);
}
/**
* Paint the button
*/
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
Graphics2D g2 = (Graphics2D)g;
g2.clearRect(0,0,width,height);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//Check Button Model state
if(model.isPressed())
paintPressedButton(g2,width,height);
else{
if(model.isRollover()){
if(fadeUp){
fadeUp = false;
timer.start();
}
}
else{
if(fadeDown){
fadeDown = false;
timer.start();
}
}
g2.setPaint(new Color(background[0],background[1],background[2],background[3]));
g2.fillRoundRect(0,0,width-1,height-1,height,height);
}
}
/**
* Draw a pressed button
*/
private void paintPressedButton(Graphics2D g2,int width,int height){
float[] temp = new float[4];
for(int i=0;i<background.length;i++)
temp[i] = background[i]-.4f < 0f ? 0f : background[i]-.4f;
g2.setPaint(new Color(temp[0],temp[1],temp[2],temp[3]));
g2.fillRoundRect(0,0,width-1,height-1,height,height);
}
/**
* paint the border
*/
public void paintBorder(Graphics g){
int width = getWidth();
int height = getHeight();
g.setColor(Color.BLACK);
g.drawRoundRect(0,0,width-1,height-1,height,height);
}
/**
* Inner action listener class
*/
private class TimerAction implements ActionListener{
private float alphaInc = .2f;
WButton button;
public TimerAction(WButton b){
button = b;
}
public void actionPerformed(ActionEvent e){
if(model.isRollover()){
background[3] += alphaInc;
if(background[3] > 1.0f){
timer.stop();
background[3] = 1.0f;
fadeDown = true;
}
}
else{
background[3] -= alphaInc;
if(background[3] < 0f){
timer.stop();
background[3] = 0f;
fadeUp = true;
}
}
button.repaint();
}
}
}
编辑 1
aly建议的方法让我更接近目标,但仍未达成。与其使用g2.clearRect(),我按照建议用透明颜色涂抹了物体。这样就去掉了白色方框,但出现了不同的颜色。经过调查,发现它是父面板的颜色,但没有透明度。以下为示例图片(面板透明度为70%)。第一张图片是程序启动时的情况。第二张图片是鼠标悬停后的情况。