java.awt.Frame是否支持内部窗体?能否通过它创建多个内部窗体?
Answers to Your AWT/Swing Internal Frame Questions
Hey there! Let's break down your questions about internal frames and java.awt.Frame clearly:
1. Does java.awt.Frame natively support internal windows?
Short answer: No. The java.awt.Frame class is part of the legacy AWT (Abstract Window Toolkit) library, which doesn't include built-in support for internal frames. Internal window functionality is exclusive to Swing—AWT only provides top-level containers like Frame, Dialog, and Window.
2. Is using JInternalFrame a valid way to create multiple internal windows with a java.awt.Frame?
Absolutely, this approach works perfectly! Here's the breakdown:
JInternalFrameis a Swing component built specifically for creating document-style internal windows. It relies on aJDesktopPane(another Swing component) to handle window layering, dragging, and other internal window interactions.- Even though you're starting with a
java.awt.Frame, you can embed Swing components likeJDesktopPanedirectly into it. Swing is built on top of AWT, so mixing the two is allowed (though for cleaner integration, usingjavax.swing.JFrameis recommended if you don't have a strict requirement to usejava.awt.Frame).
Here's a quick, runnable code example to demonstrate:
import java.awt.Frame; import javax.swing.JDesktopPane; import javax.swing.JInternalFrame; import javax.swing.SwingUtilities; public class InternalFrameDemo { public static void main(String[] args) { // Always run Swing code on the Event Dispatch Thread to avoid UI glitches SwingUtilities.invokeLater(() -> { Frame awtFrame = new Frame("AWT Frame with Internal Windows"); awtFrame.setSize(800, 600); awtFrame.setVisible(true); awtFrame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE); // Create a Swing desktop pane to manage internal frames JDesktopPane desktopPane = new JDesktopPane(); awtFrame.add(desktopPane); // Add 3 sample internal frames for (int i = 1; i <= 3; i++) { JInternalFrame internalFrame = new JInternalFrame( "Internal Window " + i, true, // resizable true, // closable true, // maximizable true // iconifiable ); internalFrame.setSize(250, 200); internalFrame.setLocation(50 * i, 50 * i); internalFrame.setVisible(true); desktopPane.add(internalFrame); } }); } }
内容的提问来源于stack exchange,提问作者Sounak Saha
相关产品推荐
相关产品推荐

