问题描述
一旦我将鼠标悬停在任何圆形按钮上,它后面就会出现一个方形背景,并且再也不会消失。该按钮仍然可点击,而方形背景则不可点击。我没有实现任何 MouseEvents 所以我不知道为什么会出现背景
左:被点击后,中心:鼠标悬停后,右:原始状态代码示例
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GUI implements ActionListener {
private JLabel textLabel;
private JButton buttonL,buttonConfirm,buttonR;
public GUI() {
JPanel maindisplay = new JPanel();
maindisplay.setBackground(new Color(172,181,176));
maindisplay.setPreferredSize(new Dimension(150,160));
maindisplay.setLayout(new BorderLayout());
textLabel = new JLabel("1");
textLabel.setHorizontalAlignment(SwingConstants.CENTER);
maindisplay.add(textLabel,BorderLayout.CENTER);
JPanel container = new JPanel();
container.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.gridwidth = GridBagConstraints.REMAINDER;
gc.insets = new Insets(75,0);
container.add(maindisplay,gc);
Dimension buttonDimension = new Dimension(18,18);
Color buttonColor = new Color(242,240,241);
buttonL = new RoundButton();
buttonL.setBackground(buttonColor);
buttonL.setPreferredSize(buttonDimension);
buttonL.addActionListener(this);
gc.gridwidth = GridBagConstraints.RELATIVE;
gc.insets = new Insets(10,20,0);
container.add(buttonL,gc);
buttonConfirm = new RoundButton();
buttonConfirm.setBackground(buttonColor);
buttonConfirm.setPreferredSize(buttonDimension);
buttonConfirm.addActionListener(this);
gc.insets = new Insets(40,-35,0);
container.add(buttonConfirm,gc);
buttonR = new RoundButton();
buttonR.setBackground(buttonColor);
buttonR.setPreferredSize(buttonDimension);
buttonR.addActionListener(this);
gc.insets = new Insets(10,-50,0);
container.add(buttonR,gc);
JFrame frame = new JFrame();
frame.add(container);
frame.pack();
frame.setLocationRelativeto(null);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttonL) {
textLabel.setText("L");;
} else if(e.getSource() == buttonConfirm) {
textLabel.setText("");
} else if(e.getSource() == buttonR) {
textLabel.setText("R");
}
}
public static void main(String[] args) {
new GUI();
}
}
圆形 JButton
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;
public class RoundButton extends JButton {
Shape shape;
@Override
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.filloval(0,getSize().width - 1,getSize().height - 1);
}
@Override
protected void paintBorder(Graphics g) {
g.setColor(Color.darkGray);
g.drawoval(0,getSize().height - 1);
}
@Override
public boolean contains(int x,int y) {
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0,getWidth(),getHeight());
}
return shape.contains(x,y);
}
}
解决方法
进行自定义绘制时,基本代码应为:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// add custom painting here
}
这将确保清除背景,因此没有绘画伪影。
但是,在您的情况下,您希望在绘制按钮之前绘制父面板的背景,因此您需要使用:
button.setOpaque(false);
此外,当您单击按钮时,矩形仍会出现,表示按钮处于按下状态。要删除这幅画,您需要使用:
button.setContentAreaFilled( false );
查看:Change JButton focus area 以了解圆形按钮的另一种实现方式。