简体   繁体   English

为什么我不能绘制JPanel?

[英]Why can't I draw in a JPanel?

public void display() {

    pan.repaint();
    fen.add(pan);
    fen.addKeyListener(this);
    fen.setResizable(false);
    fen.setTitle("Le Jeu 2D");
    img.setText("Coucou");
    pan.add(img);
    pan.repaint();
    pan.setBackground(Color.yellow);
    fen.setVisible(true);
    fen.setSize(480, 272);
    pan.repaint();
    fen.revalidate();

}

public void paintComponent(Graphics g) {

    System.out.println("zzz");
    pan.paint(g);
    g.setColor(Color.red);
    g.drawRect(10, 10, 10, 10);
}

It doesn't draw anything. 它没有画任何东西。 Why? 为什么? I have defined the paint component method, so I don't understand why. 我已经定义了paint component方法,所以我不明白为什么。 Edit: I edited my code, please take a look 编辑:我编辑了我的代码,请看一看

You don't create an instance of your Game class and don't add it to the JFrame . 您不会创建Game类的实例,也不会将其添加到JFrame

Here is the Game panel which you are painting on: 这是您要绘画的“游戏”面板:

Game.java Game.java

public class Game extends JPanel {
    @Override
    public final void paintComponent(final Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.drawRect(10, 10, 10, 10);
    }

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(300, 300);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    @Override
    public Dimension getMaximumSize() {
        return new Dimension(300, 300);
    }
}

You then need to create an instance of this Game panel and add it to your JFrame: 然后,您需要创建此Game面板的实例并将其添加到您的JFrame中:

GameFrame.java GameFrame.java

public class GameFrame extends JFrame {
    public GameFrame() {
        setLocationRelativeTo(null);
        setResizable(false);
        setTitle("2D Game");
        Game game = new Game();
        setContentPane(game);
        pack();
        setVisible(true);
    }
}

Then when you create an instance of the JFrame: 然后,当您创建JFrame的实例时:

Example.java 范例.java

public class Example {
    public static void main(String[] args) {
        new GameFrame();
    }
}

The panel will be added, and painted: 面板将被添加并绘制:

在此处输入图片说明

You never create an instance of the Game class and you never add the Game class to the frame. 您永远不会创建Game类的实例,也永远不会将Game类添加到框架。 Even if you did create an instance the size would still be (0, 0) so there would be nothing to paint. 即使您确实创建了一个实例,其大小仍将是(0,0),因此将无需绘制任何内容。

Basically the whole structure of your code is wrong. 基本上,您的代码的整个结构是错误的。

I suggest you start over and start with the demo code found in the Swing tutorial on Custom Painting . 我建议您重新开始,并从“ 自定义绘画”的Swing教程中找到的演示代码开始。

The basic structure of your code seems weird. 您的代码的基本结构似乎很奇怪。 You could instantiate a JFrame in your main control class , ie. 您可以在主控件类中实例化JFrame,即。 it should be GAME class in this case. 在这种情况下,它应该是GAME类。 Then you can create a new JPanel class and add its object into the JPanel object. 然后,您可以创建一个新的JPanel类,并将其对象添加到JPanel对象中。 Within the JPanel class, you can create all the elements you need and set corresponding parameters. 在JPanel类中,您可以创建所需的所有元素并设置相应的参数。 You could also add the event listener in an inner class or a separate class. 您也可以在内部类或单独的类中添加事件侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM