gpt4 book ai didi

java - JPanel 和 JDesktopPane

转载 作者:行者123 更新时间:2023-11-30 06:24:04 25 4
gpt4 key购买 nike

我试图将 JPanel 添加到我的 JFrame,然后将 JFrame 的 contentPane 设置为 JDesktopPane,以便我可以创建新的 JInternalFrames 并将它们添加到 DesktopPane,但当我这样做时,第一个 JPanel 没有显示:

JScrollPane newScroll = new JScrollPane(newButtonPanel); //Create new scroll and add the JPanel
newScroll.getVerticalScrollBar().setUnitIncrement(16);
add(newScroll, BorderLayout.CENTER); //Add the scroll containing the JPanel to center
newScroll.setVisible(true);
add(desktopPane);
setContentPane(desktopPane);
desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

最佳答案

在这里,您将 JScrollPane 添加到 JFrame 的现有 contentPane

JScrollPane newScroll = new JScrollPane(newButtonPanel); 
newScroll.getVerticalScrollBar().setUnitIncrement(16);
add(newScroll, BorderLayout.CENTER);
newScroll.setVisible(true);

在这里,您将桌面 Pane 添加到现有的内容 Pane 中,然后用桌面 Pane 替换该内容 Pane 。

add(desktopPane);  // why do both? both add to contentPane
setContentPane(desktopPane); // and replace contentPane?
desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

您所得到的行为不应令您感到惊讶,因为您正在替换 contentPane 并想知道为什么之前添加到其中的组件看不到。解决方案:不要这样做!如果您想在 contentPane 中显示两个组件,则不要替换它。相反,将每个组件添加到不同的 BorderLayout 位置,因为 contentPanes 默认使用此布局。将它们放置在哪里将完全取决于您尝试构建的 GUI 结构,这是您尚未告诉或向我们展示的内容。

关于java - JPanel 和 JDesktopPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47576269/

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