gpt4 book ai didi

java - JLayeredPane 未使用 JFrame 调整大小

转载 作者:行者123 更新时间:2023-12-02 05:40:57 24 4
gpt4 key购买 nike

我在 JFrame 中有一个 JLayeredPane,其中有两个 JPanel。一切正常,除了因为 JLayeredPane 要求我有一个空的 Layout,所以当JFrame 调整大小。

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

}

这就是它通常的样子(对于正常尺寸来说看起来很完美): normal

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

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

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

这就是现在的样子(仅调整粉色层的大小): changed

最佳答案

我对上面有阴影层的更复杂的容器也有同样的问题。我正在寻找答案,但只找到了这个(这对我没有帮助)。我所做的是这样的:

layers.addComponentListener(new ComponentAdapter() {
@Override public void componentResized(ComponentEvent e) {
Dimension size = layers.getSize(); // get size
backgroundPane.setSize(size); // push size through
moveablePane.setSize(size); // push size trhough
// otherChildOfLayers.setSize(size); // push size trhough
layers.revalidate(); // revalidate to see updates
layers.repaint(); // "Always invoke repaint after revalidate"
}
});

有关 repaint/revalidate 的更多信息

关于java - JLayeredPane 未使用 JFrame 调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24522151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com