java - 将JPanel添加到JPanel时,第一个JPanel的大小变小

标签 java swing jpanel

我对此真的很困惑。我制作了游戏“Microtrip”。这是手机游戏,但我正在制作PC游戏。我有一个扩展 JPanel 的背景类,它只是绘制一个从 0, 0 开始并具有屏幕大小的矩形(我的游戏是全屏)。我有一个主菜单类也扩展了 JPanel。我想为主菜单添加所有内容。然后我将所有内容添加到扩展 JFrame 的 GameFrame 类中。我有一个主类,它只调用 GameFrame 类。这是我的代码: 背景类:

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

@SuppressWarnings("serial")

public class Background extends JPanel{

    int width, height;
    Color backgroundBlue;

    public Background(int width, int height){

        this.width = width;
        this.height = height;
        backgroundBlue = new Color(25, 159, 229);

    }

    @Override

    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(backgroundBlue);
        g.fillRect(0, 0, this.width, this.height);
    }

}

主菜单类:

import javax.swing.*;

@SuppressWarnings("serial")

public class MainMenu extends JPanel{

    Background background;

    public MainMenu(int WW, int WH){
        //WW = window width
        //WH = widow height
        this.setLocation(0, 0);
        this.setSize(WW, WH);
        background = new Background(WW, WH);
        this.add(background);
    }

}

GameFrame 类:

import main_menu.MainMenu;
import main_menu.*;

import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;

import javax.swing.JFrame;

import java.awt.event.*;

@SuppressWarnings("serial")

public class GameFrame extends JFrame{

    private GraphicsDevice vc;
    private MainMenu mainMenu;
    //private Background background;

    public GameFrame(){

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        mainMenu = new MainMenu((int)screenSize.getWidth(), (int) screenSize.getHeight());
        /*background = new Background((int)screenSize.getWidth(), (int) screenSize.getHeight());*/
        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
        vc= e.getDefaultScreenDevice();
        this.setTitle("Mictrotrip");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(0, 0, (int)screenSize.getWidth(), (int)screenSize.getHeight());
        this.add(mainMenu);
        //this.add(background);
        this.setLocationRelativeTo(null);
        this.setUndecorated(true);
        this.setResizable(false);
        vc.setFullScreenWindow(this);
        addKeyListener(new KeyListener(){

            public void keyPressed(KeyEvent e){

                if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
                    System.exit(0);
                }

            }

            public void keyReleased(KeyEvent e){

            }

            public void keyTyped(KeyEvent e){

            }

        });
    }

}

和我的主类:

public class Main {

    public static void main(String[] args) {

        new GameFrame();

    }

}

当我运行它时,它只在顶部中心显示一个小矩形。 我做了以下实验:

我忽略了主菜单类,直接在 JFrame 中添加背景(GameFrame 中的注释行),一切正常。为什么? 我阅读了所有类似的问题,但没有一个对我有帮助。

最佳答案

如果您希望背景填充主菜单,那么问题出在 MainMenu 上 - 您让 JPanel 使用默认的 FlowLayout,而 FlowLayouts 不会考虑组件大小(因此 Jamal 的解决方案将不起作用),而是考虑其大小首选尺寸。如果你想让背景充满主菜单,那么给MainMenu一个BorderLayout,然后添加你的背景组件BorderLayout.CENTER:

public MainMenu(int WW, int WH){
    // WW = window width
    // WH = widow height
    // this.setLocation(0, 0);
    // this.setSize(WW, WH);

    setLayout(new BorderLayout());
    background = new Background(WW, WH);
    this.add(background, BorderLayout.CENTER);
}

关于java - 将JPanel添加到JPanel时,第一个JPanel的大小变小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44848452/

相关文章:

java - 属性值历史模式

java - 如何在 Spring 中编写 mongodb 聚合减少查询?

java - getItem()、getSelectableItem() 和 getSource() 之间有什么区别?

java - 如何将jtable的内容复制到剪贴板

java - JPanel 中的 JPanel

Java fillOval不会填充窗口

java - 我如何反序列化字符串 Json Date?

java继承删除子对象

java - 客户端从服务器请求公共(public)

java - Java中的drawImage(),为什么图像没有加载?