JFrame的大小调整为JLayeredPane中的JPanel

JFrame的大小调整为JLayeredPane中的JPanel

本文介绍了将JFrame的大小调整为JLayeredPane中的JPanels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JLayeredPane在Swing GUI上工作. JFrame有一个JLayeredPane,其中包含两个JPanel.稍后,我想在JPanel中显示一些组件,我剪切了代码的这一部分以使其更短.

I'm working on a Swing GUI using the JLayeredPane. The JFrame has a JLayeredPane which contains two JPanels. Later I want to display some components in the JPanels, I cut this part of the code to make it shorter for you.

如何将JFrame调整为JPanel的大小? frame.pack()不起作用,并且没有将行设置为首选大小,GUI将以最小大小显示.

How do I resize the JFrame to the sizes of the JPanels? frame.pack() does not work and without the line setting the preferred size the GUI will show with minimal size.

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

我可以更改首选大小行以适合最大的JPanel,但是我问这个问题,因为我希望JFrame根据JPanel的大小动态调整大小.

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 JPanels dynamically.

推荐答案

如何使用分层窗格:在分层窗格中布置组件 ,尽管默认情况下分层窗格没有布局管理器,但是您仍然可以分配布局管理器到分层窗格."使用OverlayLayout(见此处)来重叠面板.

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.

或者,使用 JInternalFrame ,其中允许您分别pack()内部框架,如此处和.

Alternatively, use JInternalFrame, which does allow you to pack() the internal frames individually, as shown here and here.

这篇关于将JFrame的大小调整为JLayeredPane中的JPanels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:30