GridLayout 中的 JPanel 组件错误地填充网格
我试图阻止 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
阅读有关使用布局管理器的 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.
您是否尝试过设置 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!
GridLayout 的全部要点是将组件布局在规则的网格中。您的
setSize
调用将被布局管理器覆盖。setPreferredSize
或setMaximumSize
可能有效,但由布局管理器决定是否考虑它们。The whole point of GridLayout is to layout the components in a regular grid. Your
setSize
calls will be overridden by the layout manager. PossiblysetPreferredSize
orsetMaximumSize
may work, but it is up to the layout manager to decide to take them into account.BoxLayout
是一个在这种情况下,有用的替代方案,如本示例所示。BoxLayout
is a useful alternative in this context, as seen in this example.