java - 将 JPanel 置于全屏 JFrame 的中心

标签 java user-interface miglayout

我正在尝试用 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/

相关文章:

css - Material 设计: Bootstrap 3 Responsive Navbar

c++ - Qt - QProcess 不工作

java - 调用 setPreferredWidth 时,JTable 列不会调整大小

java - MigLayout,将所有组件对齐 JFrame 的中心

java - 如何使用java将电子邮件下载到本地系统?

java - 使用 fieldgroup 属性 vaadin 进行验证

java - 程序需要一直循环直到键入键 "Q"/"q"

c - Glade 中的 GUI — 适用于 Linux 和 Windows 的应用程序

java - 在单独的类 java 中操作组件

java - 为什么我的所有 BST 遍历都按顺序返回值?