所以我想在自己的業餘時間製作我的第一個小型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