代码之家  ›  专栏  ›  技术社区  ›  Tamias

指定顶级容器的ContentPane是否有好处?

  •  0
  • Tamias  · 技术社区  · 6 年前

    https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html .

    或者“两者都是”表单只是为了向代码的读者清楚地表明jpanel确实要进入jdialog的内容窗格?

    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);
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   MadProgrammer    6 年前

    除了需要输入的代码量之外,没有“立即”优势或劣势。由于Java 1.5(我想)调用诸如“代码>添加< /代码>和<代码> SETFrase>代码>在顶级容器上被自动路由到<代码>内容窗格< /代码>,这就是为什么您仍然可以看到使用<代码> GETCractPANE< <代码>或<代码> SETCOUNTENPANE

    的代码。

    a jframe 是复合组件,由一系列层组成。当Swing首次发布时,需要直接使用 ContentPane 以向其添加组件。这最终是固定的(?)允许您直接通过框架向 contentpane via the frame添加/删除组件。

    add setLayout contentPane getContentPane setContentPane

    JFrame

    Root pane

    窗口 直接添加组件。这最终是固定的(?)允许您将组件添加/删除到

    removeAll 内容窗格 JRootPane