GridLayout 中的 JPanel 组件错误地填充网格

发布于 2024-10-01 02:31:50 字数 953 浏览 1 评论 0原文

我试图阻止 JPanel 中的 GridLayout 完全填充单元格并忽略

我正在使用此代码的组件的任何 setSize:

import javax.swing.*;
import java.awt.*;

public class GrideComponents{
  public static void main(String[] args) {
    JFrame frame = new JFrame("Laying Out Components in a Grid");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(5,2,0,0));
    panel.add(new JLabel("Enter name"));
    JTextField a = new JTextField(5);
    a.setSize(10, 10);
    panel.add(a);
    panel.add(new JLabel("Enter Roll"));
    panel.add(new JTextField(3));
    panel.add(new JLabel("Enter Class"));
    panel.add(new JTextField(3));
    panel.add(new JLabel("Enter Total Marks"));
    panel.add(new JTextField(3));
    panel.add(new JButton("Ok"));
    panel.add(new JButton("Cancel"));
    frame.add(panel);
    frame.setSize(400,400);
    frame.setVisible(true);
  }
}

并且每个组件都填充 gris 单元格而不是指定的大小。

谢谢

I am trying to prevent the GridLayout in a JPanel from filling the cells entirely and ignoring any setSize of the components

i am using this code:

import javax.swing.*;
import java.awt.*;

public class GrideComponents{
  public static void main(String[] args) {
    JFrame frame = new JFrame("Laying Out Components in a Grid");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(5,2,0,0));
    panel.add(new JLabel("Enter name"));
    JTextField a = new JTextField(5);
    a.setSize(10, 10);
    panel.add(a);
    panel.add(new JLabel("Enter Roll"));
    panel.add(new JTextField(3));
    panel.add(new JLabel("Enter Class"));
    panel.add(new JTextField(3));
    panel.add(new JLabel("Enter Total Marks"));
    panel.add(new JTextField(3));
    panel.add(new JButton("Ok"));
    panel.add(new JButton("Cancel"));
    frame.add(panel);
    frame.setSize(400,400);
    frame.setVisible(true);
  }
}

And every component is filling the gris cell instead of being in the size specified.

Thanks

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

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

发布评论

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

评论(4

微暖i 2024-10-08 02:31:50

阅读有关使用布局管理器的 Swing 教程,了解基础知识布局管理器如何工作。

GridLayout 将所有组件的大小调整为相同的大小。

创建表单时,您通常会使用不同的布局管理器嵌套不同的面板,以获得您想要的效果。

在这种情况下,您可以使用 FlowLayout 作为按钮。然后,您可以将 GridBagLayout 或 SpringLayout 用于其他组件。

Read the Swing tutorial on Using Layout Managers so you understand the basics of how layout managers work.

A GridLayout resizes all components to the same size.

When creating a form you will generally nest different panels using different layout managers to get the effect you desire.

In this case you might use a FlowLayout for the buttons. Then you might use a GridBagLayout or SpringLayout for the other components.

迷雾森÷林ヴ 2024-10-08 02:31:50

您是否尝试过设置 setMaximumSize 和 setPreferredSize ?

无论如何,我建议您尝试 MiGLayout 来满足您的所有布局需求。你会惊讶于它的简单性!

Have you tried setting setMaximumSize and setPreferredSize?

In any case, I recommend you try MiGLayout for all your layout needs. You'll be surprised by it's simplicity!

暗恋未遂 2024-10-08 02:31:50

GridLayout 的全部要点是将组件布局在规则的网格中。您的 setSize 调用将被布局管理器覆盖。 setPreferredSizesetMaximumSize 可能有效,但由布局管理器决定是否考虑它们。

The whole point of GridLayout is to layout the components in a regular grid. Your setSize calls will be overridden by the layout manager. Possibly setPreferredSize or setMaximumSize may work, but it is up to the layout manager to decide to take them into account.

渡你暖光 2024-10-08 02:31:50

BoxLayout 是一个在这种情况下,有用的替代方案,如本示例所示。

BoxLayout is a useful alternative in this context, as seen in this example.

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