我正在研究必须在其旁边显示状态的自定义Jbutton。代码有点肮脏,并且仍在架构上进行工作,现在它只是使用带有委托JButton的JPanel的原型。当我将鼠标悬停在其他JFrame组件上时,我会遇到奇怪的绘画手工艺品,我不知道为什么。
这是带有测试程序的组件的代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class CustomStateButton extends JPanel {
JButton button;
boolean status;
public CustomStateButton(String text, ImageIcon icon, boolean status) {
super();
this.status = status;
this.button = new JButton(text,icon);
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add (Box.createHorizontalStrut(15));
add (this.button);
}
public CustomStateButton(String text, boolean status) {
super();
this.status = status;
this.button = new JButton(text);
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add (Box.createHorizontalStrut(15));
add(this.button);
}
public CustomStateButton( ImageIcon icon, boolean status) {
super();
this.status = status;
this.button = new JButton(icon);
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add (Box.createHorizontalStrut(15));
add (this.button);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.WHITE);
if (this.status)
{
g2.setPaint(new GradientPaint(new Point(0, (getHeight()-10)/2), Color.WHITE, new Point(0,
getHeight()-10), Color.GREEN.darker()));
}
else{
g2.setPaint(new GradientPaint(new Point(0, (getHeight()-10)/2), Color.WHITE, new Point(0,
getHeight()-10), Color.BLACK));
}
g2.fillOval(2, (getHeight()-10)/2, 10, 10);
g2.setPaint(Color.BLACK);
g2.drawOval(2, (getHeight()-10)/2, 10, 10);
}
/**
* @param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame("test");
frame.setSize(new Dimension(400,300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
final CustomStateButton button = new CustomStateButton("test", false);
final JRadioButton radioButton = new JRadioButton();
radioButton.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
button.status= radioButton.isSelected();
button.repaint();
}
});
frame.getContentPane().add (button);
frame.getContentPane().add (new JButton("blabla"));
frame.getContentPane().add (radioButton);
frame.setVisible(true);
}
}
任何建议都欢迎:-)
最佳答案
覆盖paintComponent
的JComponent
方法时,第一个调用通常应该是对super.paintComponent(g)
方法的调用:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
}
否则,可能会出现渲染伪像,这是由于屏幕上残留的东西在先前的渲染过程中已被绘制而导致的。
默认情况下,
super.paintComponent(g)
方法将通过最终委托ComponentUI
的该方法来使不透明的组件以其背景色清除:public void update(Graphics g, JComponent c) {
if (c.isOpaque()) {
g.setColor(c.getBackground());
g.fillRect(0, 0, c.getWidth(),c.getHeight());
}
paint(g, c);
}
关于java - 自定义组件的奇怪油漆问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23825492/