[英]Is there an advantage to specifying the ContentPane of a top-level container?
我試圖擺脫使用NetBeans創建簡單的Swing GUI的麻煩,因此試圖更好地了解整個容器/布局機制。 我一直在線閱讀各種內容,尤其是https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html 。
在下面的示例代碼中,我可以看到第二種形式DialogJPanel()的優點。 僅舉一個例子,JPanel可以被賦予邊框。 我的理解是,實際上已將JPanel添加到JDialog的內容窗格中。
在3年前我參與Java的唯一“形式教育”中,我們被教導使用第三種形式DialogBoth()。
這樣做有好處嗎? 也許在某些情況下需要以某種方式操縱內容窗格? 如果是這樣,那是什么情況?
還是“兩種”形式只是為了使代碼讀者清楚地知道JPanel實際上已經進入了JDialog的內容窗格?
然后有可能使用setContentPane(jPanelOuter)。 從實際意義上講,這是否具有某些特殊目的?
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class DialogTest {
public static void main(String[] args) {
DialogContentPane dlgC = new DialogContentPane();
display(dlgC, "ContentPane");
DialogJPanel dlgP = new DialogJPanel();
display(dlgP, "JPanel");
DialogBoth dlgB = new DialogBoth();
display(dlgB, "Both");
}
public static class DialogContentPane extends JDialog {
public DialogContentPane() {
Container contentPane = this.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JRadioButton jRadioButton1 = new JRadioButton("My Radio Button, which does nothing");
jRadioButton1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.add(jRadioButton1);
JCheckBox jCheckBox1 = new JCheckBox("My Check Box, which does nothing either");
jCheckBox1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.add(jCheckBox1);
}
}
public static class DialogJPanel extends JDialog {
public DialogJPanel() {
JPanel jPanelOuter = new JPanel();
jPanelOuter.setLayout(new BoxLayout(jPanelOuter, BoxLayout.Y_AXIS));
JRadioButton jRadioButton1 = new JRadioButton("My Radio Button, which does nothing");
jRadioButton1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
jPanelOuter.add(jRadioButton1);
JCheckBox jCheckBox1 = new JCheckBox("My Check Box, which does nothing either");
jCheckBox1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
jPanelOuter.add(jCheckBox1);
this.add(jPanelOuter);
}
}
public static class DialogBoth extends JDialog {
public DialogBoth() {
Container contentPane = this.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel jPanelOuter = new JPanel();
jPanelOuter.setLayout(new BoxLayout(jPanelOuter, BoxLayout.Y_AXIS));
JRadioButton jRadioButton1 = new JRadioButton("My Radio Button, which does nothing");
jRadioButton1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
jPanelOuter.add(jRadioButton1);
JCheckBox jCheckBox1 = new JCheckBox("My Check Box, which does nothing either");
jCheckBox1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
jPanelOuter.add(jCheckBox1);
contentPane.add(jPanelOuter);
}
}
public static void display(JDialog dlg, String title) {
Toolkit tk;
Dimension screenDims;
dlg.setTitle(title);
tk = Toolkit.getDefaultToolkit();
screenDims = tk.getScreenSize();
dlg.setLocation((screenDims.width - dlg.getWidth()) / 2, (screenDims.height - dlg.getHeight()) / 2);
dlg.pack();
dlg.setModalityType(JDialog.DEFAULT_MODALITY_TYPE);
dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dlg.setVisible(true);
}
}
除了需要鍵入的代碼量之外,沒有“直接”的優勢或劣勢。 由於Java 1.5(我認為)對頂級容器上的add
和setLayout
類的調用會自動路由到contentPane
,這就是為什么您仍然可以看到使用getContentPane
或setContentPane
代碼的原因
為了更好地了解正在發生的事情,您需要更好地了解JFrame
工作方式...
JFrame
是復合組件,由一系列層組成。 最初發布Swing時,必須直接使用contentPane
才能向其中添加組件。 最終已將其修復(?),以允許您直接通過框架將組件添加/刪除到contentPane
。
請注意, removeAll
不會路由到contentPane
,而是會刪除JRootPane
,這很麻煩。
有些人喜歡“舊的”方式,因為很明顯已經完成了工作,有些人(像我一樣,很懶惰)只喜歡把事情做好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.