自定义组件的奇怪涂料问题

时间:2014-05-23 09:23:51

标签: java swing paintcomponent

我正在制作一个自定义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);


}


}

欢迎任何建议: - )

1 个答案:

答案 0 :(得分:2)

当覆盖paintComponent的{​​{1}}方法时,第一次调用通常应该是对JComponent方法的调用:

super.paintComponent(g)

否则,可能会出现渲染图像,这是因为屏幕上剩下的东西是在之前的渲染过程中绘制的。

默认情况下,@Override protected void paintComponent(Graphics g) { super.paintComponent(g); ... } 方法会导致使用其背景颜色清除不透明组件,最终委托super.paintComponent(g)的此方法:

ComponentUI