JPanel 在使用 cardLayout 时不会显示 paintComponent

JPanel won't show paintComponent while using cardLayout

我一直无法让绘图组件在第三个屏幕上绘制任何东西。我用这么简单的代码测试了它,发现了 1 件事。如果我将矩形的坐标设置为 0,0,那么屏幕中间顶部会出现一个小点,其大小是应有的一小部分。

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class hello extends JPanel {
    public int[][] room = new int[20][20];// Whether the room is solid or hollow

    public static Random r = new Random();
    boolean toggle, GameEnable;
    int i, j, px, py, temp, endx, endy;
    private static final long serialVersionUID = 1L;
    JFrame f = new JFrame("Maze Game");

    JPanel p0 = new JPanel();
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();

    JButton b1 = new JButton("Screen 2");
    JButton b2 = new JButton("Give Visuals");

    CardLayout cl = new CardLayout();
    public void Game() {
        p0.setLayout(cl);

        p1.add(b1);
        p2.add(b2);
        p3.add(new GameVisual());
        p1.setBackground(Color.RED);
        p2.setBackground(Color.BLACK);
        p4.setBackground(Color.PINK);

        p0.add(p1, "1");
        p0.add(b2, "2");
        p0.add(p3, "3");
        p0.add(p4, "4");
        cl.show(p0, "1");

        final Timer timer = new Timer(10 * 60 * 1000, new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                cl.show(p0, "4");
            }
        });

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(p0, "2");
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(p0, "3");
                timer.start();
            }
        });

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(816, 816);
        f.setVisible(true);
        f.setLocationRelativeTo(null);
        f.add(p0);
        timer.start();
    }

    class GameVisual extends JPanel { //This is a sub class not a separate class file
        private static final long serialVersionUID = 2L;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLUE);
            g.fillRect(10,10, 400, 400);
        }
    }

    public static void main(String[] args) {
        new hello().Game();
    }
}

所以我的问题是为什么我的 paintcomponent 没有出现在屏幕上(或者显示在错误位置的大小部分,我该如何解决这个问题。 P.S。如果可能的话,我希望将所有内容都保持在相同的 class 中,而不是将它们放入单独的 classes 中,因为我使用一个数组来完成我在此代码的较大版本中的大部分绘画。

首先,我想你的意思是将 p2 添加到 p0(不是 b2):

p0.add(p2, "2");

其次,如果你想看到完整的矩形,你可以这样做:

GameVisual gv = new GameVisual();
gv.setPreferredSize(new Dimension(500,500));
p3.add(gv);

但是,当您向 GameVisual 添加组件时,它会调整大小以适合这些组件。所以也许你不应该设置首选大小。