我试图将一个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);发布于 2017-11-30 14:56:05
在这里,您将您的JScrollPane添加到JFrame的现有的 contentPane中
JScrollPane newScroll = new JScrollPane(newButtonPanel);
newScroll.getVerticalScrollBar().setUnitIncrement(16);
add(newScroll, BorderLayout.CENTER);
newScroll.setVisible(true); 在这里,将desktopPane添加到现有的contentPane中,然后将替换为desktopPane,将contentPane替换为desktopPane。
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结构,这是您尚未告诉或展示给我们的。
https://stackoverflow.com/questions/47576269
复制相似问题