我正在尝试用 Java 制作一个全屏应用程序。我想添加三个扩展 JPanel 的类。每个都有自己的布局和组件。
我第一次尝试使用 MiG Layout。我有一个扩展 JFrame 作为主窗口的类。该类有一个使用米格的面板,其他三个类也添加到该面板中。现在主面板出现在左上角,我希望它出现在中间。我尝试制作一个“包装器”面板,我可以使用 BorderLayout 将其居中,但这似乎不起作用。我尝试了一些其他排列,但我觉得这应该可行,但我不明白为什么不行。
相关代码如下:
public class MainWindow extends JFrame {
private final int WINDOW_WIDTH = 800; //Width
private final int WINDOW_HEIGHT = 800; //Height
//three panel objects
private final IntroPanel header;
private final InputPanel input;
private final SubmitPanel submit;
//for fullscreen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public MainWindow() throws MalformedURLException {
//set things like size, close operation, etc
this.Build();
//Create a MiG layout
MigLayout layout = new MigLayout("wrap 3");
//panel which will hold three panels
JPanel panel = new JPanel(layout);
//initiate the three panels we need for user actions
header = new IntroPanel();
input = new InputPanel();
submit = new SubmitPanel();
panel.add(header, "span, center, gapbottom 15");
panel.add(input, "span, center, gapbottom 15");
panel.add(submit,"span, center, gapbottom 15");
this.setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
//set the windows position to the center of the screen
setLocationRelativeTo(null);
//Make the window visable
setVisible(true);
}
最佳答案
this.setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
使用GridBagLayout
代替BorderLayout:
this.setLayout(new GridBagLayout());
add(panel, new GridBagConstraints());
关于java - 将 JPanel 置于全屏 JFrame 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23850839/