Java 如何避免组件重叠并使其更小

Java 如何避免组件重叠并使其更小,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我正在设计一个带有GridBagLayout的窗口,但是当我运行它时,当我有一个滚动窗格时,窗口的组件非常大。当我没有滚动和窗口较小的组件重叠 这是我的代码: GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{922, 0}; gridBagLayout.rowHeights = new int[]{148, 0}; gridBagLayout.columnWeigh

我正在设计一个带有GridBagLayout的窗口,但是当我运行它时,当我有一个滚动窗格时,窗口的组件非常大。当我没有滚动和窗口较小的组件重叠

这是我的代码:

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);
结果是:

  • 我可以做些什么来阻止组件重叠
  • 如何使组件更小

您有窗口外观的草图吗?我举了一个工作示例()并用您的代码替换了“addComponentsToPane”方法的大部分内容。您的代码示例不包含滚动窗格,但我认为组件重叠没有任何问题。Swing layout manager是2D的,这意味着组件不会重叠(除非您在代码中做了一些非常奇怪的事情)。您可能尝试将多个组件放置在导致问题的同一gridx/y位置。张贴一个适当的说明问题的帖子。也就是说,创建一个包含2或3个组件的面板的框架,以演示所描述的问题。您发布的代码太多,我们无法查看。很抱歉,代码太大,它们只是一些组件,如textArea、panel、choice和label。我将试着举一个简短的例子。我添加了一个图像。您有窗口外观的草图吗?我使用了一个工作示例()并用您的代码替换了“addComponentsToPane”方法的大部分内容。您的代码示例不包含滚动窗格,但我认为组件重叠没有任何问题。Swing layout manager是2D的,这意味着组件不会重叠(除非您在代码中做了一些非常奇怪的事情)。您可能尝试将多个组件放置在导致问题的同一gridx/y位置。张贴一个适当的说明问题的帖子。也就是说,创建一个包含2或3个组件的面板的框架,以演示所描述的问题。您发布的代码太多,我们无法查看。很抱歉,代码太大,它们只是一些组件,如textArea、panel、choice和label。我将试着举一个简短的例子。我添加了一个图像。