簡體   English   中英

將JPanel添加到JPanel時,第一個JPanel的大小會變小

[英]When adding JPanel to JPanel, the size of the first JPanel gets lower

我真的對此感到困惑。 我制作了“ 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);
    }

}

MainMenu類:

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();

    }

}

當我運行它時,它僅在頂部中心顯示一個小矩形。 我做了以下實驗:

我忽略了mainmenu類,直接在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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM