JLayeredPane 不使用 JFrame 调整大小 [英] JLayeredPane not resizing with JFrame

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

问题描述

我在 JFrame 中有一个 JLayeredPane,其中有两个 JPanel.一切正常,除了因为 JLayeredPane 要求我有一个空的 Layout,它不会正确调整两个 JPanel 的大小,当JFrame 调整大小.

I have a JLayeredPane within a JFrame, with two JPanels in it. It all works fine, except since the JLayeredPane requires me to have a null Layout, it doesn't resize the two JPanels correctly when the JFrame resizes.

public class CreatorUI extends JFrame{

    private static final long serialVersionUID = 1L;

    private JPanel moveablePane = new JPanel();
    private Container backgroundPane;
    private JLayeredPane layers = new JLayeredPane();
    private ComponentMover componentMover = new ComponentMover();
    private ComponentResizer componentResizer = new ComponentResizer();

    public CreatorUI(){
        backgroundPane = new BackgroundUI().initComponents();

        layers.add(backgroundPane, 1);
        moveablePane.setLayout(null);
        componentMover.setAutoLayout(true);
        moveablePane.setOpaque(false);
        layers.add(moveablePane, 2);
        moveablePane.setSize(backgroundPane.getSize());
        add(layers, BorderLayout.CENTER);
        layers.setPreferredSize(backgroundPane.getPreferredSize());

        pack();
    }

    public void addWindow(WindowComponent window){
        this.componentMover.registerComponent(window);
        this.componentResizer.registerComponent(window);

        this.componentMover.setDragInsets(this.componentResizer.getDragInsets());

        this.moveablePane.add(window);
    }


    public static void main(String[] args){
        CreatorUI frame = new CreatorUI();

        frame.addWindow(new MapComponent());
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

这是正常情况下的样子(看起来很适合正常尺寸):

This is what it looks like normally (looks perfect for the normal size):

这是我尝试调整 JFrame 大小后的样子:

This is what it looks like once I've tried to resize the JFrame:

我发现如果我将代码更改为这样,我可以调整顶层的大小:

I figured out that I can make the top layer resizable if I change my code to this:

public CreatorUI(){

    componentMover.setAutoLayout(true);

    backgroundPane = new BackgroundUI().initComponents();
    moveablePane.setLayout(null);
    moveablePane.setOpaque(false);
    moveablePane.setSize(backgroundPane.getSize());

    layers.setLayout(new BorderLayout());
    layers.add(backgroundPane, BorderLayout.CENTER);
    layers.setLayer(backgroundPane, new Integer(1));
    layers.add(moveablePane, BorderLayout.CENTER);
    layers.setLayer(moveablePane, new Integer(2));

    add(layers, BorderLayout.CENTER);
    layers.setPreferredSize(backgroundPane.getPreferredSize());

    pack();
}

这是现在的样子(只有粉红色层会调整大小):

This is what it look like now (only the pink layer resizes):

推荐答案

回答我自己的问题

有一个非常 hack-y 的修复,它(出于您的目的)是始终将叠加层保持在最大尺寸.

There is a very hack-y fix, which (for your purposes) is to keep the overlay at max size at all times.

public CreatorUI(){

    componentMover.setAutoLayout(true);

    backgroundPane = new BackgroundUI().initComponents();
    moveablePane.setLayout(null);
    moveablePane.setOpaque(false);
    moveablePane.setSize(Toolkit.getDefaultToolkit().getScreenSize());

    layers.setLayout(new BorderLayout());


    layers.setLayer(moveablePane, JLayeredPane.DRAG_LAYER);
    layers.add(moveablePane, BorderLayout.CENTER);

    layers.setLayer(backgroundPane, JLayeredPane.DEFAULT_LAYER);
    layers.add(backgroundPane, BorderLayout.CENTER);


    add(layers, BorderLayout.CENTER);
    layers.setPreferredSize(backgroundPane.getPreferredSize());

    pack();
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆