将 JPanel 添加到 JPanel 时,第一个 JPanel 的大小变小
When adding JPanel to JPanel, the size of the first JPanel gets lower
我真的很困惑。我在制作游戏 "Microtrip"。这是手机游戏,但我正在为 PC 制作它。我有一个扩展 JPanel 的背景 class,它只绘制一个从 0、0 开始并具有屏幕大小的矩形(我的游戏是全屏的)。我有一个主菜单 class,它也扩展了 JPanel。我想在那里添加主菜单的所有内容。然后我将所有内容添加到扩展 JFrame 的 GameFrame class 中。我有 main class,他刚刚调用了 The GameFrame class。这是我的代码:
背景 class:
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);
}
}
主菜单 class:
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 class:
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){
}
});
}
}
和我的主要 class:
public class Main {
public static void main(String[] args) {
new GameFrame();
}
}
当我 运行 它时,它只在顶部中心显示一个小矩形。
我做了以下实验:
我忽略了主菜单 class 并直接在 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);
}
我真的很困惑。我在制作游戏 "Microtrip"。这是手机游戏,但我正在为 PC 制作它。我有一个扩展 JPanel 的背景 class,它只绘制一个从 0、0 开始并具有屏幕大小的矩形(我的游戏是全屏的)。我有一个主菜单 class,它也扩展了 JPanel。我想在那里添加主菜单的所有内容。然后我将所有内容添加到扩展 JFrame 的 GameFrame class 中。我有 main class,他刚刚调用了 The GameFrame class。这是我的代码: 背景 class:
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);
}
}
主菜单 class:
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 class:
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){
}
});
}
}
和我的主要 class:
public class Main {
public static void main(String[] args) {
new GameFrame();
}
}
当我 运行 它时,它只在顶部中心显示一个小矩形。 我做了以下实验:
我忽略了主菜单 class 并直接在 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);
}