Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Jframe上绘图_Java_Swing - Fatal编程技术网

Java 在Jframe上绘图

Java 在Jframe上绘图,java,swing,Java,Swing,我不能让这个椭圆形在JJ框架上画 static JFrame frame = new JFrame("New Frame"); public static void main(String[] args) { makeframe(); paint(10,10,30,30); } //make frame public static void makeframe(){ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JL

我不能让这个椭圆形在JJ框架上画

static JFrame frame = new JFrame("New Frame");
public static void main(String[] args) {
  makeframe();
  paint(10,10,30,30);
}

//make frame
public static void makeframe(){
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JLabel emptyLabel = new JLabel("");
  emptyLabel.setPreferredSize(new Dimension(375, 300));
  frame.getContentPane().add(emptyLabel , BorderLayout.CENTER);
  frame.pack();
  frame.setVisible(true); 
}

// draw oval 
public static void paint(int x,int y,int XSIZE,int YSIZE) {
  Graphics g = frame.getGraphics();
  g.setColor(Color.red);
  g.fillOval(x, y, XSIZE, YSIZE);
  g.dispose();
}

框架将显示,但其中未绘制任何内容。我做错了什么?

我想到了几点:

  • 永远不要覆盖paint(),而是执行paintComponent()
  • 你为什么直接在JFrame上画画?为什么不扩展JComponent(或JPanel)并利用它呢?它提供了更多的灵活性
  • 那张桌子的用途是什么?如果它位于JFrame的顶部并覆盖了整个内容,那么您的绘画将隐藏在标签后面
  • 绘制代码不应依赖于在paint()中传递的x、y值来确定绘制例程的起点。paint()用于绘制零部件的一部分。在画布上你想要的地方画一个椭圆形

  • 此外,您没有看到JLabel,因为paint()方法负责绘制组件本身以及子组件。重写paint()是邪恶的=)

    如果重写了错误的paint()方法,则应重写名为paintComponent的方法,如下所示:

    @Override 
    public void paintComponent(Graphics g)
    

    您已经创建了一个不重写绘制方法的静态方法。现在,其他人已经指出,您需要覆盖paintComponent等。但要快速修复,您需要这样做:

    public class MyFrame extends JFrame {  
       public MyFrame() {
            super("My Frame");
    
            // You can set the content pane of the frame to your custom class.
            setContentPane(new DrawPane());
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400, 400);
            setVisible(true); 
       }
    
       // Create a component that you can actually draw on.
       class DrawPane extends JPanel {
            public void paintComponent(Graphics g) {
                g.fillRect(20, 20, 100, 200); // Draw on g here e.g.
            }
       }
    
       public static void main(String args[]){
            new MyFrame();
       }
    }
    

    然而,正如其他人指出的那样……在JFrame上绘图是非常棘手的。最好在JPanel上绘制。

    您需要覆盖一个实际与您的帧最新的现有绘制方法。在本例中,您刚刚创建了自定义的新方法,默认情况下不会调用该方法

    因此,将您的方法更改为:

    public void paint(Graphics g){
    
    }
    

    这才是真正的问题。他的绘画方法从来没有被调用过。答案显然是错误的:JFrame!是一个JComponent,没有paintComponent。虽然可以实现该方法,但在正常绘制过程中从未调用该方法。与原件相比没有优势;-)@克利奥帕特拉。你完全正确。我已经改进了答案以反映你的观点。酷:-)只需注意:可以直接在JFrame上绘制,请参见[link](),但绝对不推荐,完全同意这一点。为什么这一点被否决?JFrame不是JComponent,因此没有paintComponent。