当我向paintComponent()添加代码时,我的gui消失了

时间:2014-05-01 17:19:08

标签: java swing user-interface

在我将代码添加到paintComponent()后我的代码运行时,所有JLabel,textfields和按钮都消失了,当我在程序运行时单击它们时,文本域和按钮会重新出现,但我仍然可以&# 39;看到任何JLabel。 我希望这是愚蠢的,我已经注释了paintComponent()方法中的代码似乎导致了这个错误。

public class snowBoarding extends JFrame {
    private JButton getReset() {
        if (Reset == null) {
            Reset = new JButton();
            Reset.setBounds(new Rectangle(162, 411, 131, 39));
            Reset.setFont(new Font("Dialog", Font.BOLD, 18));
            Reset.setText("Reset");
            Reset.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e){
                    textField_1.setText("0");
                    textField_2.setText("0");
                    textField_3.setText("0");
                    textField_4.setText("0");
                    textField_5.setText("0");
                    textField_6.setText("0");
                    textField_7.setText("0");
                    textField_8.setText("0");
                    textField_9.setText("0");
                    textField_10.setText("0");
                    textField_11.setText("0");
                    textField.setText("0");
                    total_1.setText("0");
                    total_2.setText("0");
                    Overall.setText("0");
                    DrawPanel.clear(DrawPanel.getGraphics());                       
                    DrawPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));


                }
            });
        }
        return Reset;
    }


    public JButton getButton_calc_draw() {
        if (Button_calc_draw == null) {
            Button_calc_draw = new JButton();
            Button_calc_draw.setBounds(303, 411, 131, 39);
            Button_calc_draw.setFont(new Font ("Dialog", Font.BOLD, 18));
            Button_calc_draw.setText("Draw");
            Button_calc_draw.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    // Get values from the text fields
                    run_1[0] = Integer.parseInt(textField.getText());
                    run_1[1] = Integer.parseInt(textField_1.getText());
                    run_1[2] = Integer.parseInt(textField_2.getText());
                    run_1[3] = Integer.parseInt(textField_3.getText());
                    run_1[4] = Integer.parseInt(textField_4.getText());
                    run_1[5] = Integer.parseInt(textField_5.getText());

                    for (int i = 0; i < run_1.length; i++) {
                        temp[i] = run_1[i];
                    }
                    Arrays.sort(temp);

                    for (int i = 1; i < (temp.length -1) ; i++){
                        avg1+=temp[i];
                    }

                    avg1 = avg1/4;


                    run_2[0] = Integer.parseInt(textField_6.getText());
                    run_2[1] = Integer.parseInt(textField_7.getText());
                    run_2[2] = Integer.parseInt(textField_8.getText());
                    run_2[3] = Integer.parseInt(textField_9.getText());
                    run_2[4] = Integer.parseInt(textField_10.getText());
                    run_2[5] = Integer.parseInt(textField_11.getText());

                    for (int i = 0; i < run_2.length; i++) {
                        temp[i] = run_2[i];
                    }
                    Arrays.sort(temp);

                    for (int i = 1; i < (temp.length -1) ; i++){
                        avg2+=temp[i];
                    }

                    avg2 = avg2/4;


                    if (avg1 > avg2){
                        OverallScore = avg1;
                    }
                    else {
                        OverallScore = avg2;
                    }


                    total_1.setText(Integer.toString(avg1));
                    total_2.setText(Integer.toString(avg2));
                    Overall.setText(Integer.toString(OverallScore));
                    DrawPanel.repaint();


                }
                // Transfer the image from the BufferedImage to the JPanel to make it visible.
                 ;
            });

        }
        return Button_calc_draw;
    }



                }
            });
        }
        return Reset;
    }


    private myJPanel getDrawPanel() {
        if (DrawPanel == null) {
            DrawPanel = new myJPanel();
            DrawPanel.setLayout(new GridBagLayout());
            DrawPanel.setBounds(new Rectangle(258, 39, 326, 361));
            DrawPanel.setBackground(Color.white);
            DrawPanel.setEnabled(true);

            DrawPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

            //Instantiate the BufferedImage object and give it the same width 
            // and height as that of the drawing area JPanel
            img = new BufferedImage(DrawPanel.getWidth(), 
                                    DrawPanel.getHeight(), 
                                    BufferedImage.TYPE_INT_RGB);

            //Get its graphics context. A graphics context of a particular object allows us to draw on it.
            g2dImg = (Graphics2D)img.getGraphics();

            //Draw a filled white coloured rectangle on the entire area to clear it.
            g2dImg.setPaint(Color.WHITE);
            g2dImg.fill(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
        }
        return DrawPanel;
    }



    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                snowBoarding thisClass = new snowBoarding();
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                thisClass.setVisible(true);

            }
        });
    }

    public snowBoarding() { 
        super();        
        setResizable(false);
        getContentPane().setLayout(null);
        initialize();   
    }

    private void initialize() {
        this.setSize(600, 500);
        this.setContentPane(getJContentPane());
        this.setTitle("Snowboarding Score Calculator");
        this.setResizable(false);
        this.setVisible(true);

    }

}

class myJPanel extends JPanel {

BufferedImage img;
Graphics2D g2dImg;
private static final long serialVersionUID = 1L;
private Rectangle2D.Double rectangle;

public void paintComponent(Graphics g) {
    //Must be called to draw the JPanel control. 
     // As a side effect, it also clears it.
    super.paintComponent(g);

    Graphics2D g2D = (Graphics2D) g;
        rectangle = new Rectangle2D.Double(0, 260-score[0] * 2, 25, score[0] * 2);
        g2D.setPaint(Color.blue); 
        g2D.fill(rectangle);
        g2D.draw(rectangle); 

}

 protected void clear(Graphics g) {
     super.paintComponent(g);


     // Also clear the BufferedImage object by drawing a white coloured filled rectangle all over.
     g2dImg.setPaint(Color.WHITE);
     g2dImg.fill(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));


}

}

编辑:删除不必要的代码

我需要重绘以使用run_1和run_2数组绘制矩形作为x或y值,然后单击绘制并重置以将绘制的图像返回到白色平板。

绘制按钮 - &gt;绘制图表

重置按钮 - &gt;删除图表,以便可以创建新图表。

1 个答案:

答案 0 :(得分:4)

您不得在g2dImg使用paintComponent(),而应使用g(方法paintComponent()收到的参数)。更确切地说,((Grpahics2D)g)代替g2dImg

g2dImg似乎没有在您发布的代码中初始化,也许您已经在某处完成了...

更一般地说,您应始终使用在绘制方法中收到的Graphics实例(如果需要,将其转换为Graphics2D)。您不应尝试重用/共享/存储Graphics的实例。

同样适用于clear()方法。

以下是如何重写此paintComponent()方法的示例:

private boolean shallPaint = false;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (shallPaint) {
        Graphics2D g2D = (Graphics2D) g;
        rectangle = new Rectangle2D.Double(0, 260-score1 * 2, 25, score1 * 2);
        g2D.setPaint(Color.blue); 
        g2D.fill(rectangle);
        g2D.draw(rectangle);              
    }
}

public void setShallPaint(boolean pShallPaint) {
    shallPaint  = pShallPaint;
}

然后只需致电myJPanel.repaint()重绘它。

你应该在你的重置按钮中替换:

DrawPanel.clear(DrawPanel.getGraphics());

使用:

DrawPanel.setShallPaint(false);
DrawPanel.repaint();

Button_calc_draw

DrawPanel.setShallPaint(true);
DrawPanel.repaint();