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



在我的代码中,当我在paintComponent()中添加代码时,所有的JLabel、文本字段和按钮都消失了——当我在程序运行时点击它们时,文本字段和按键会重新出现,但我仍然看不到任何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()));

}
}

编辑:删除不必要的代码

在我单击draw和reset以将绘制的图像返回到白板之后,我需要重新绘制以使用run1和run2数组作为x或y值来绘制矩形。

绘制按钮-->绘制图形

重置按钮-->将删除图形,以便可以创建新的图形。

您不应在paintComponent()中使用g2dImg,而应使用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();

相关内容

  • 没有找到相关文章

最新更新