所以我想在自己的业余时间制作我的第一个小型java游戏。我遇到了布局问题。Java swing布局,3个面板
我想要游戏是一个特定的大小(600高度,800宽度),我想在主框架内的3个“面板”。一个是主要的游戏框架,它的高度为500,宽度为600,右侧的库存/信息面板应该有500高度和200宽度,最后在底部有一个文本面板,用于保存高度为100的信息宽度为800.到目前为止,这里是我所拥有的。 (我没有发现面板高度,因为我没有发现任何变化)。
我会如何去制作一个框架,里面有3个面板。我看了一下如何使用GridBagLayout(),但看起来我已经变得更糟,并且不完全理解如何使用GridBagLayout(),即使是文档(是的,我也是愚蠢的)。
LMK,如果你不明白我的代码的部分或我的帖子。谢谢。
package Frame;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class frame {
//Sets variables such as height, width and title
private JFrame frame;
private Canvas canvas;
private JPanel mainWindow, infoWindow, textWindow;
//main constructor to create the frame of the game. Is called in Launcher
//Sets the parameters of the frame. User defined in main.
public frame(){
createDisplay();
}
//sets the properties of the display
private void createDisplay() {
frame = new JFrame();
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
frame.setTitle("Island Man");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
canvas = new Canvas();
canvas.setSize(new Dimension(800, 600));
canvas.setMaximumSize(new Dimension(800, 600));
canvas.setMinimumSize(new Dimension(800, 600));
mainWindow = new JPanel();
mainWindow.setBackground(Color.CYAN);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 2;
c.weighty = 2;
frame.add(mainWindow, c);
infoWindow = new JPanel();
infoWindow.setBackground(Color.GREEN);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 0;
c.weightx = 0;
c.weighty = 2;
frame.add(infoWindow, c);
textWindow = new JPanel();
textWindow.setBackground(Color.MAGENTA);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 0;
c.weightx = 0;
c.weighty = 3;
frame.add(textWindow, c);
frame.add(canvas);
frame.pack();
}
}
什么是你的问题? – user3437460
如何制作具有这些尺寸的这3个面板的框架?我看了一下,看到GridBagLayout()会很好用,但我遇到了一些麻烦。 –
由于FredK提到,使用setPreferredSize。不要在面板上调用setSize;布局经理负责这个大小,并且会消除你的设置。 – VGR