在 JFrame 中绘制矩形不起作用 [英] Draw Rectangle in JFrame not working

查看:24
本文介绍了在 JFrame 中绘制矩形不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码应该在 JFrame 上绘制一个矩形,但是当我在 Eclipse 上运行该程序时,它只是打开了框架,但没有在其上绘制圆圈.

I have this bit of code that's supposed to draw a rectangle on a JFrame but when I run the program on Eclipse, it just opens the frame, but does not draw the circle on it.

代码如下:

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

public class Infout {
    Infout(){
        JFrame frame = new JFrame();

        frame.setSize(300, 400);
        frame.setTitle("An Empty Frame");
        frame.setDefaultCloseOperation(3);

        frame.setVisible(true);
    }

    public static void main(String[] args) {
         // TODO Auto-generated method stub
         Infout m = new Infout();
         m.paint(null); 
    }

    public void paint(Graphics g) 
    {
         g.drawRect(5, 5, 105, 105);
    }
}

谁能告诉我为什么它不能正常工作?

Can someone please tell me why it's not working properly?

推荐答案

我相信你想要做的是调用 frame.repaint(); 代替.但是,这仍然不能解决您的问题,因为您的 paint() 方法实际上并未覆盖 JFramepaint() 方法因为你的类没有扩展 JFrame,它只是在构造函数中创建了一个 JFrame.

I believe what you are looking to do is call frame.repaint(); instead. This still doesn't sort your problem however, because your paint() method isn't actually Overriding the JFrame's paint() method because your class does not extend JFrame, it is just creating a JFrame in the constructor.

因此,您可以要么进行最后一分钟的覆盖并将您的绘制方法移至该方法中(根据 polypiel 的回答),或者(我个人认为更雄辩)您可以让您的类扩展 JFrame,像这样;

So, you can either do a last minute override and move your paint method into that (as per polypiel's answer), or (I personally think more eloquently) you can have your class extend JFrame, like so;

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

public class Infout extends JFrame{
    Infout(){
        setSize(300, 400);
        setTitle("An Empty Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
         Infout m = new Infout();
         m.repaint(); 
    }

    @Override
    public void paint(Graphics g) 
    {
         g.drawRect(5, 5, 105, 105);
    }

}

正如其他人所指出的,paintComponent() 是更好的覆盖选择,您应该记住在开始时对 super() 进行适当的调用新的覆盖方法.然后你必须创建一个新的 JPanel 来放入你的 JFrame 因为 JFrame 没有 paintComponent()> 要覆盖的方法.

As other's have pointed out however paintComponent() is a better choice of override, and you should remember to make an appropriate call to super() at the beginning of the new overridden method. You will have to then create a new JPanel to put inside your JFrame because JFrame does not have a paintComponent() method to override.

为此,您可以从类中完全删除 paint() 方法,并将以下最后一分钟覆盖添加到您的构造函数中:

To do this you could remove your paint() method from your class entirely and add the following last minute override into your constructor:

    setLayout(new BorderLayout());
    add(new JPanel(){
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawRect(5, 5, 105, 105);
        }
    }, BorderLayout.CENTER);

然而,为了可扩展性和良好的面向对象设计,从长远来看,您最好定义自己的 JPanel 子类并覆盖那里的 paintComponent(Graphics) 方法.抱歉胡说八道,希望能帮到你.

For extensibility and good object orientated design however, you may be better off in the long term defining your own JPanel subclass and overriding the paintComponent(Graphics) method there. Sorry for waffling, I hope this helps.

这篇关于在 JFrame 中绘制矩形不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆