简体   繁体   中英

Can I add JInternalFrames to a JPanel?

It seems that JInternalFrames can only be added to a JDesktopPane and you have to set your JFrame's content pane to that JDesktopPane. Something like:

JFrame frame = new JFrame();
JDesktopPane desktopPane = new JDesktopPane();
JInternalFrame internalFrame = new JInternalFrame();

desktopPane.add(internalFrame);
frame.setContentPane(desktopPane);

The problem is that the JInternalFrames are allowed to move over anything that I add to the JFrame, like JPanels.

Is there a way for me to add the JInternalFrames/JDesktopPane to something else like a JPanel? That way I can restrict the JInternalFrames to be within that JPanel. If that is not possible, then what other options do I have?

You can add any object to a JPanel that extends JComponent. For example:

JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.setVisible(true);
window.setSize(600, 400);

JDesktopPane desktopPane = new JDesktopPane();
JInternalFrame internalFrame = new JInternalFrame();        
JPanel mainPanel = new JPanel();
mainPanel.add(desktopPane);
mainPanel.add(internalFrame);
window.add(mainPanel);

hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM