Java JSpinner绘画两次

时间:2012-11-21 00:09:56

标签: java swing paint jcomponent jspinner

Quick picture to show what is happening

如上图所示,JSpinner出现了两次。第(0,0)点的第一次出现不应该是可选的,可编辑的或没有微调器按钮可用。

这里奇怪的是每个其他组件都没有问题。只有jspinner。我正在使用Java 7并在Netbeans中开发(不是gui开发人员工具包)。这是java 7的错误吗?如果没有,我可以尝试在我指定的区域中仅使我的JSpinner油漆一次?

用于说明问题的代码:

我将它添加到JPanel的子类中,如下所示:

public class MyCustomGUI extends JPanel {
private JSpinner entrySpinner;
public MyCustomGUI () {
 super(null);
 this.setDoubleBuffered(true);

 entrySpinner = new JSpinner(new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1));
 add(entrySpinner);
....

我有一个方法可以给它一个位置:

public void resize() {
     entrySpinner.setBounds((int) (this.getWidth() * .2), (int) (this.getHeight() * 0.38), (int) (this.getWidth() * 0.3), (int) (this.getHeight() * 0.1));
}

我在这里覆盖了paint方法:

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(
            RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    .. draw shapes..
    super.paintComponents(g);
    super.validate();
    Toolkit.getDefaultToolkit.sync();
    g.dispose();
}

1 个答案:

答案 0 :(得分:2)

  1. 您拨打paintComponent两次,一次拨打super.paint并手动拨打
  2. 您正在validate方法中调用paint,这只会导致组件重新开始重新绘制,反复...再次向您的CPU说再见..
  3. 您正在处理未创建的图形上下文,这就像关闭您未打开的文件一样。如果你没有创建它,就不应该关闭它。
  4. 我不相信你需要致电Toolkit.getDefaultToolkit.sync();,但我有理由相信你不应该在paint方法中做到这一点
  5. 除非你有充分的理由这样做,否则你不应该重写paint方法。执行自定义绘制的推荐方法是paintComponent方法(由paint调用)
  6. 您可能希望阅读

    使用示例更新

    为我修好了......

    enter image description here enter image description here

    左边是你的代码,右边是我的

    public class TestPaintSpinner {
    
        public static void main(String[] args) {
            new TestPaintSpinner();
        }
    
        public TestPaintSpinner() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setDoubleBuffered(true);
                setLayout(new GridBagLayout());
                add(new JSpinner(new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1)));
    
                JPanel panel = new JPanel();
                panel.add(new JLabel("Subpanel"));
                add(panel);
            }
    
    //        public void paint(Graphics g) {
    //            super.paint(g);
    //
    //            Graphics2D g2d = (Graphics2D) g;
    //            Point2D sPoint = new Point2D.Float(0, 0);
    //            Point2D ePoint = new Point2D.Float(this.getWidth(), this.getHeight());
    //
    //            g2d.setRenderingHint(
    //                    RenderingHints.KEY_TEXT_ANTIALIASING,
    //                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    //            super.paintComponents(g);
    //            super.validate();
    //            Toolkit.getDefaultToolkit().sync();
    //            g.dispose();
    //        }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
    
                Graphics2D g2d = (Graphics2D) g;
                Point2D sPoint = new Point2D.Float(0, 0);
                Point2D ePoint = new Point2D.Float(this.getWidth(), this.getHeight());
    
                // Note, this will effect every component painted after this one!!
                g2d.setRenderingHint(
                        RenderingHints.KEY_TEXT_ANTIALIASING,
                        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            }
        }
    }