我正在使用GridBagLayout设计一个窗口,但是当我运行它时,如果使用ScrollPane,则窗口的组件会很大。当我没有滚动窗口时,组件会重叠。
这是我的代码:
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{922, 0};
gridBagLayout.rowHeights = new int[]{148, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JPanel panel_2 = new JPanel();
GridBagConstraints gbc_panel_2 = new GridBagConstraints();
gbc_panel_2.fill = GridBagConstraints.BOTH;
gbc_panel_2.gridx = 0;
gbc_panel_2.gridy = 0;
add(panel_2, gbc_panel_2);
GridBagLayout gbl_panel_2 = new GridBagLayout();
gbl_panel_2.columnWidths = new int[] {0};
gbl_panel_2.rowHeights = new int[] {0, 0};
gbl_panel_2.columnWeights = new double[]{1.0, 1.0};
gbl_panel_2.rowWeights = new double[]{1.0, 1.0};
panel_2.setLayout(gbl_panel_2);
JPanel panel_11 = new JPanel();
GridBagConstraints gbc_panel_11 = new GridBagConstraints();
gbc_panel_11.weightx = 1.0;
gbc_panel_11.weighty = 1.0;
gbc_panel_11.gridwidth = 2;
gbc_panel_11.fill = GridBagConstraints.BOTH;
gbc_panel_11.gridx = 0;
gbc_panel_11.gridy = 1;
panel_2.add(panel_11, gbc_panel_11);
GridBagLayout gbl_panel_11 = new GridBagLayout();
gbl_panel_11.columnWidths = new int[] {0};
gbl_panel_11.rowHeights = new int[] {0};
gbl_panel_11.columnWeights = new double[]{1.0};
gbl_panel_11.rowWeights = new double[]{1.0};
panel_11.setLayout(gbl_panel_11);
JPanel panel_8 = new JPanel();
panel_8.setBorder(new LineBorder(new Color(0, 0, 0)));
GridBagConstraints gbc_panel_8 = new GridBagConstraints();
gbc_panel_8.weightx = 1.0;
gbc_panel_8.weighty = 1.0;
gbc_panel_8.fill = GridBagConstraints.BOTH;
gbc_panel_8.gridx = 0;
gbc_panel_8.gridy = 0;
panel_11.add(panel_8, gbc_panel_8);
GridBagLayout gbl_panel_8 = new GridBagLayout();
gbl_panel_8.columnWidths = new int[] {0};
gbl_panel_8.rowHeights = new int[] {0, 0};
gbl_panel_8.columnWeights = new double[]{1.0};
gbl_panel_8.rowWeights = new double[]{1.0, 1.0};
panel_8.setLayout(gbl_panel_8);
JPanel panel_14 = new JPanel();
GridBagConstraints gbc_panel_14 = new GridBagConstraints();
gbc_panel_14.weighty = 0.2;
gbc_panel_14.fill = GridBagConstraints.BOTH;
gbc_panel_14.gridx = 0;
gbc_panel_14.gridy = 0;
panel_8.add(panel_14, gbc_panel_14);
GridBagLayout gbl_panel_14 = new GridBagLayout();
gbl_panel_14.columnWidths = new int[] {0, 0, 0, 0, 0};
gbl_panel_14.rowHeights = new int[]{0, 0, 0};
gbl_panel_14.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_panel_14.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
panel_14.setLayout(gbl_panel_14);
Label label_7 = new Label("Family");
GridBagConstraints gbc_label_7 = new GridBagConstraints();
gbc_label_7.insets = new Insets(0, 40, 0, 5);
gbc_label_7.gridx = 0;
gbc_label_7.gridy = 1;
panel_14.add(label_7, gbc_label_7);
JPanel panel_16 = new JPanel();
GridBagConstraints gbc_panel_16 = new GridBagConstraints();
gbc_panel_16.weighty = 0.8;
gbc_panel_16.fill = GridBagConstraints.BOTH;
gbc_panel_16.gridx = 0;
gbc_panel_16.gridy = 1;
panel_8.add(panel_16, gbc_panel_16);
GridBagLayout gbl_panel_16 = new GridBagLayout();
gbl_panel_16.columnWidths = new int[] {0, 0, 0, 0};
gbl_panel_16.rowHeights = new int[] {0, 0};
gbl_panel_16.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_panel_16.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel_16.setLayout(gbl_panel_16);
TextArea textArea_2 = new TextArea();
GridBagConstraints gbc_textArea_2 = new GridBagConstraints();
gbc_textArea_2.weighty = 0.5;
gbc_textArea_2.weightx = 0.5;
gbc_textArea_2.fill = GridBagConstraints.BOTH;
gbc_textArea_2.insets = new Insets(0, 0, 0, 5);
gbc_textArea_2.gridx = 0;
gbc_textArea_2.gridy = 0;
panel_16.add(textArea_2, gbc_textArea_2);
textArea_2.setText("TEXT...");
Label label_9 = new Label("Language");
GridBagConstraints gbc_label_9 = new GridBagConstraints();
gbc_label_9.weightx = 0.25;
gbc_label_9.fill = GridBagConstraints.BOTH;
gbc_label_9.insets = new Insets(0, 0, 0, 5);
gbc_label_9.gridx = 1;
gbc_label_9.gridy = 0;
panel_16.add(label_9, gbc_label_9);
Choice choice_13 = new Choice();
GridBagConstraints gbc_choice_13 = new GridBagConstraints();
gbc_choice_13.weightx = 0.25;
gbc_choice_13.fill = GridBagConstraints.HORIZONTAL;
gbc_choice_13.gridx = 2;
gbc_choice_13.gridy = 0;
panel_16.add(choice_13, gbc_choice_13);
结果是: