简体   繁体   English

将JPanel置于全屏JFrame的中心

[英]Center JPanel in center of fullscreen JFrame

I am trying to make an application that is fullscreen in Java. 我正在尝试制作全屏Java应用程序。 I have three classes which extend JPanel that I want to add. 我有三个类可以扩展要添加的JPanel。 Each of these have their own layout and components. 这些每个都有自己的布局和组件。

I am trying to use MiG Layout for the first time. 我正在尝试第一次使用MiG Layout。 I have a class which extends JFrame as the main window. 我有一个扩展JFrame作为主窗口的类。 This class has a panel using MiG, and the three other classes are added to this panel. 此类有一个使用MiG的面板,其他三个类别已添加到该面板中。 Right now the main panel is appearing in the upper left and I want it to appear in the center. 现在,主面板显示在左上方,我希望它显示在中间。 I tried making a "wrapper" panel that I could just center using BorderLayout but this doesn't seem to be working. 我尝试制作一个“包装器”面板,可以仅使用BorderLayout居中,但这似乎不起作用。 I have tried a few other permutations but I feel like this should work and I don't understand why it isn't. 我尝试了其他一些排列,但我觉得这应该可行,但我不明白为什么不这样做。

Here is the relevant code: 以下是相关代码:

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);

Instead of a BorderLayout use a GridBagLayout : 代替BorderLayout使用GridBagLayout

this.setLayout(new GridBagLayout());
add(panel, new GridBagConstraints());

Avoid adding panels directly to a JFrame. 避免将面板直接添加到JFrame。 Use this.setContentPane(panel) instead. 请改用this.setContentPane(panel) This way you do not need any extra Layout. 这样,您不需要任何额外的布局。 Using MigLayout add the LayoutContraint fill for the mainPanel. 使用MigLayout为mainPanel添加LayoutContraint填充。


public class MigLayoutTests {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel mainPanel = new JPanel(new MigLayout("debug, fill", "", ""));
        mainPanel.add(new JLabel("Hello World"), "align left bottom");

        frame.setContentPane(mainPanel);
        frame.setVisible(true);

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM