Java: paintComponent() Oval 没有出现在 Netbeans 中

Java: paintComponent() Oval is not appearing in Netbeans

我正在尝试学习如何在 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);
}    

}

先看看 Painting in AWT and Swing and Performing Custom Painting

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

  1. 继承自基于 swing 的组件(如 JComponentJPanel
  2. 然后您必须重写它的 paintComponent 方法并在此方法中执行自定义绘画。
  3. 将此组件添加到可显示的内容(如 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 的 class 中访问它。您可以创建一个新的 class 来扩展 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);
        }
    }

}