Java:paintComponent()椭圆未出现在Netbeans中

Java:paintComponent()椭圆未出现在Netbeans中,java,swing,paintcomponent,Java,Swing,Paintcomponent,我试图学习如何在java中绘制椭圆,但我制作的paintComponent没有被任何东西调用,尝试调用它只会导致更多问题 程序运行成功,但我想要显示的图像未显示 import java.awt.*; import javax.swing.*; public class TEST2{ public void paintComponent(Graphics g){ g.drawOval(70, 70, 100, 100); } public static void main(String

我试图学习如何在java中绘制椭圆,但我制作的paintComponent没有被任何东西调用,尝试调用它只会导致更多问题

程序运行成功,但我想要显示的图像未显示

import java.awt.*;
import javax.swing.*;


public class TEST2{
public void paintComponent(Graphics g){
    g.drawOval(70, 70, 100, 100);
}
public static void main(String[] args) {
    TEST2 gui = new TEST2();
    gui.setUpFrame();
}   
public void setUpFrame(){
    JFrame frame = new JFrame();
    frame.setTitle("Images should be in this program");
    frame.setSize(600,300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}    

}
首先看一看,然后

为了能够在Swing中执行自定义绘制,您必须

  • 从基于swing的组件继承(如
    JComponent
    JPanel
  • 然后必须重写它的
    paintComponent
    方法,并在此方法中执行自定义绘制
  • 将此组件添加到可显示的内容(如
    JFrame
  • 在进行任何自定义绘制之前,应确保调用
    super.paintComponent

    为确保您没有犯任何(常见)错误,应使用
    @Override
    注释

    例如

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test2 extends JPanel {
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawOval(70, 70, 100, 100);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame();
                    frame.setTitle("Images should be in this program");
                    frame.add(new Test2());
                    frame.pack();
                    frame.setVisible(true);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
            });
        }
    
    }
    
    paintComponent()方法是一个可以重写的方法,应该在扩展JPanel的类中访问它。您可以创建一个扩展JPanel的新类,并重写paintComponent()方法来绘制椭圆。您还必须将新的JPanel添加到JFrame中才能显示它。我修改了下面的代码,现在应该显示椭圆形了。正如Madprogrammer所指出的,您可能应该在edt的上下文中构造GUI以避免并发问题,但为了简单起见,我将省略它

    import java.awt.*;
    import javax.swing.*;
    
    public class Test {
    
        public static void main(String[] args) {
            Test gui = new Test();
            gui.setUpFrame();
        }
    
        public void setUpFrame() {
            JFrame frame = new JFrame();
            frame.setTitle("Images should be in this program");
            frame.setSize(600, 300);
            JPanel oval = new oval();
            frame.setContentPane(oval);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        public class oval extends JPanel{
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawOval(70, 70, 100, 100);
            }
        }
    
    }
    

    1-在它开始在你的组件上绘制各种奇怪和奇妙的工件之前,调用
    super.paintComponent
    ;2 -考虑在重写方法时使用<代码> @重写< /代码>注释;3-
    paintComponent
    不应是
    公共的
    ;4-你真的应该在EDT的上下文中创建你的UI