gpt4 book ai didi

java - 指定顶级容器的 ContentPane 有优势吗?

转载 作者:行者123 更新时间:2023-12-02 11:04:00 25 4
gpt4 key购买 nike

我试图摆脱使用 NetBeans 创建简单的 Swing GUI,因此试图更好地理解整个容器/布局机制。我一直在阅读各种内联内容,尤其是 https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html .

在下面的示例代码中,我可以看到第二种形式 DialogJPanel() 的优点。仅举一个示例,可以为 JPanel 指定边框。我的理解是 JPanel 实际上被添加到 JDialog 的内容 Pane 中。

在我接受的唯一一次涉及 Java 的“正规教育”(3 年前)中,我们被教导使用第三种形式,D​​ialogBoth()。

这样做有什么好处吗?也许在某些情况下需要以某种方式操作内容 Pane ?如果是这样,那些情况是什么?

或者“两者”形式只是为了向代码读者表明 JPanel 确实进入了 JDialog 的内容 Pane ?

然后可以使用 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(我认为)对顶级容器上的 addsetLayout 的调用会自动路由到 contentPane,这就是为什么你可能仍然会看到使用 getContentPanesetContentPane

的代码

为了更好地了解正在发生的情况,您需要更好地了解 JFrame 的工作原理...

Root pane

JFrame 是复合组件,由一系列层组成。当 Swing 首次发布时,需要直接使用 contentPane 才能向其中添加组件。这最终被修复(?)以允许您直接通过框架向 contentPane 添加/删除组件。

请注意,removeAll 不会路由到 contentPane,并且会删除 JRootPane,这很困惑。

有些人更喜欢“旧”方式,因为所做的事情很明显,有些人(比如我,很懒)只是喜欢把事情做好

关于java - 指定顶级容器的 ContentPane 有优势吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51118487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com