简体   繁体   English

将JFrame调整为JLayeredPane内部的JPanels

[英]Resize JFrame to JPanels inside JLayeredPane

I'm working on a Swing GUI using the JLayeredPane . 我正在使用JLayeredPane Swing GUI。 The JFrame has a JLayeredPane which contains two JPanel s. JFrame有一个JLayeredPane ,其中包含两个JPanel Later I want to display some components in the JPanel s, I cut this part of the code to make it shorter for you. 稍后,我想在JPanel显示一些组件,我剪切了部分代码以使其更短。

How do I resize the JFrame to the sizes of the JPanel s? 如何调整JFrame到的大小JPanel S' frame.pack() does not work and without the line setting the preferred size the GUI will show with minimal size. frame.pack()无法工作,并且没有设置行的首选大小,GUI将以最小大小显示。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class TestGUI {
    private JFrame frame;
    private JLayeredPane layeredPane;
    private JPanel panelBottom;
    private JPanel panelTop;
    private MainMenuBar menuBar;

    public TestGUI() {
        // panel bottom:
        panelBottom = new JPanel();
        panelBottom.setSize(1000, 500);
        panelBottom.setBackground(new Color(0, 100, 0, 100));
        panelBottom.setLayout(new GridBagLayout());
        // panel top:
        panelTop = new JPanel();
        panelTop.setSize(950, 450);
        panelTop.setBackground(new Color(0, 0, 100, 100));
        panelTop.setLayout(new GridBagLayout());
        // layered pane:
        layeredPane = new JLayeredPane();
        layeredPane.add(panelBottom, 1);
        layeredPane.add(panelTop, 0);
        // frame building:
        frame = new JFrame();
        menuBar = new MainMenuBar();
        frame.setJMenuBar(menuBar);
        frame.setLayout(new BorderLayout());
        frame.setPreferredSize(new Dimension(1100, 600)); // <-- Without this the GUI will be very small
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(layeredPane, BorderLayout.CENTER);
        frame.pack(); // <-- does not work!
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new TestGUI();
    }
}

EDIT: I could just change the preferred size line to fit the biggest JPanel, but i ask the question because I want the JFrame to resize depending on the size of the JPanel s dynamically. 编辑:我可以更改首选大小行以适合最大的JPanel,但是我问这个问题,因为我希望JFrame可以根据JPanel的大小动态调整大小。

As suggested in How to Use Layered Panes: Laying Out Components in a Layered Pane , "Although a layered pane has no layout manager by default, you can still assign a layout manager to the layered pane." 如“ 如何使用分层窗格:在分层窗格中布局组件”中的建议 ,“尽管默认情况下分层窗格没有布局管理器,但仍可以将布局管理器分配给分层窗格。” Use OverlayLayout , seen here , for overlapping panels. 使用OverlayLayout (在此处看到)来重叠面板。

Alternatively, use JInternalFrame , which does allow you to pack() the internal frames individually, as shown here and here . 或者,使用JInternalFrame ,它确实允许您单独pack()内部框架,如此此处所示。

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

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