[英]How to tell a JDesktopPane to fill the entire screen of JFrame
任何機構請建議代碼如何告訴JDesktopPane
在 Java Netbeans IDE
填充JFrame
的整個屏幕。
JFrame
的布局設置為BorderLayout
。 將JDesktopPane
添加到 JFrame 的CENTER
區域:
JFrame f = new JFrame(); f.setBounds(50, 50, 500, 400); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.setLayout(new BorderLayout()); JDesktopPane desktopPane = new JDesktopPane(); f.add(desktopPane, BorderLayout.CENTER); f.setVisible(true);
神奇之處在於BorderLayout
如何管理其子組件的布局。 添加到BorderLayout
的CENTER
區域的任何內容都將填充從其容器中獲得的盡可能多的區域。
如果要最大化JDesktopPane
內的JInternalFrame
,則應在將其添加到基礎JDesktopPane
后對其調用setMaximum(true)
:
public class JDesktop {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setBounds(50, 50, 500, 400);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
JInternalFrame internalFrame1 = new JInternalFrame("Internal Frame 1", true, true, true, true);
internalFrame1.setSize(150, 150);
internalFrame1.setVisible(true);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.add(internalFrame1);
try {
internalFrame1.setMaximum(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
f.add(desktopPane, BorderLayout.CENTER);
f.setVisible(true);
}
}
既然您有了這個想法,那么了解默認值也不錯。 JFrame
的默認布局管理器是BorderLayout
,當您向JFrame
添加任何內容而不指定該區域的約束時,它將被添加到CENTER
區域。 所以你可以在你的代碼中省略這些行:
f.setLayout(new BorderLayout());
您只需使用以下行即可添加desktopPane
:
f.add(desktopPane);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.