gpt4 book ai didi

java - 自定义组件出现奇怪的油漆问题

转载 作者:行者123 更新时间:2023-12-02 05:48:23 25 4
gpt4 key购买 nike

我正在开发一个自定义 Jbutton,它必须在其旁边显示状态。代码有点脏,架构上仍然有工作,现在它只是一个使用带有委托(delegate) JButton 的 JPanel 的原型(prototype)。当悬停其他 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);


}


}

欢迎任何建议:-)

最佳答案

当重写 JComponentpaintComponent 方法时,第一次调用通常应该是对 super.paintComponent(g) 方法的调用:

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
}

否则,可能会出现渲染伪影,这是由屏幕上残留的在先前渲染 channel 中绘制的内容造成的。

默认情况下,super.paintComponent(g) 方法将通过最终委托(delegate)给 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com