Java设置JPanel的尺寸

发布于 2024-11-27 17:40:19 字数 1151 浏览 2 评论 0原文

我在 JPanel 中创建了一个 JPanel,我想将其宽度和高度属性设置为一些特定的数字。这是我到目前为止所拥有的。

  searchPanel.setLayout(new GridBagLayout());
     GridBagConstraints c = new GridBagConstraints();
     searchPanel.setBorder(BorderFactory.createCompoundBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), new EmptyBorder(12, 12, 12, 12)));

     c.insets = new Insets(2, 2, 2, 2);
     c.fill = GridBagConstraints.HORIZONTAL;

     c.weightx = 1;
     c.weighty = 1;
     c.gridx = 0;
     c.gridy = 0;
     searchPanel.add(typeLabel, c);

     c.weightx = .8;
     c.weighty = 1;
     c.gridx = 0;
     c.gridy = 1;
     searchPanel.add(typeComboBox, c); 
    ....

     yearComboBox.setModel(this.yearDefaultCombo);
     typeComboBox.setModel(this.typeDefaultCombo);

     updateYearComboBox();

     this.setLayout(new BorderLayout(10, 10));
     this.setBorder(new EmptyBorder(10, 10, 10, 10));

     searchPanel.setSize(200, 100);
     this.add(searchPanel, BorderLayout.NORTH);

在代码中,“this”指的是父 JPanel。现在,由于 BorderLayout.NORTH 约束,我的内部“searchPanel”沿着屏幕的整个顶部部分拉伸,但我只希望它作为左上角的一个小面板,但是 setSize 对我不起作用。

谢谢。

I have created a JPanel within a JPanel, and I would like to set its width and height attribute to some specific numbers. here is what I have so far.

  searchPanel.setLayout(new GridBagLayout());
     GridBagConstraints c = new GridBagConstraints();
     searchPanel.setBorder(BorderFactory.createCompoundBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), new EmptyBorder(12, 12, 12, 12)));

     c.insets = new Insets(2, 2, 2, 2);
     c.fill = GridBagConstraints.HORIZONTAL;

     c.weightx = 1;
     c.weighty = 1;
     c.gridx = 0;
     c.gridy = 0;
     searchPanel.add(typeLabel, c);

     c.weightx = .8;
     c.weighty = 1;
     c.gridx = 0;
     c.gridy = 1;
     searchPanel.add(typeComboBox, c); 
    ....

     yearComboBox.setModel(this.yearDefaultCombo);
     typeComboBox.setModel(this.typeDefaultCombo);

     updateYearComboBox();

     this.setLayout(new BorderLayout(10, 10));
     this.setBorder(new EmptyBorder(10, 10, 10, 10));

     searchPanel.setSize(200, 100);
     this.add(searchPanel, BorderLayout.NORTH);

In the code, "this" refers to the parent JPanel. Right now my inner "searchPanel" is stretched along the entire top section of the screen due to the BorderLayout.NORTH constraint, but I only want it as a little panel in the left top corner however setSize didn't work for me.

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千と千尋 2024-12-04 17:40:20

大多数布局管理器更重视首选尺寸而不是尺寸,因此您可以在 JPanel 上尝试 setPreferredSize(...) 。但无论您做什么,如果您添加它的 BorderLayout.NORTH,它都会拉伸以填充包含 JPanel 的整个北位置。您可能希望将 searchPanel 嵌套到另一个 JPanel 中(可能是使用 BoxLayout 的 JPanel),并添加该 JPanel BorderLayout.NORTH。

请阅读布局教程及其 API,因为其中大部分内容都有解释。

Most layout managers respect preferred size more than size, and so you may with to try setPreferredSize(...) on your JPanel. But no matter what you do, if you add it BorderLayout.NORTH, it will stretch to fill the entire north position of the containing JPanel. You may wish to nest your searchPanel into another JPanel (perhaps one using BoxLayout), and add that JPanel BorderLayout.NORTH.

Please read the layout tutorials and their API's as most of this is explained there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文