java 如何阻止 JTextField 在 GridLayout 中重新调整大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10953484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:13:07  来源:igfitidea点击:

How to stop a JTextField from getting re-sized in GridLayout

javaswinglayoutlayout-manager

提问by Lemon Juice

I am creating a GUI with grid layout. I have added the JTextFields, JButtons and everything to it DIRECTLY. For an example, like this,

我正在创建一个带有网格布局的 GUI。我已经直接向其中添加了 JTextFields、JButtons 和所有内容。例如,像这样,

JButton b1 = new JButton("Hello"):
JButton b2 = new JButton("Bye");
JTextField t1 = new JTextField(10);
JTextField t2 = new JTextField(10);

GridLayout grid = new GridLayout(2,2,2,2);

JPanel panel = new JPanel();
panel.setLayout(grid);
panel.add(b1);
panel.add(t1);
panel.add(b2);
panel.add(t2);

In here, if the user resizes the window everything appears very large. The only other way I know to prevent these from getting these is adding all of each and everything to seperate JPanel which has FlowLayout. But thats not practical because if there are 20 stuff, there will be 20 JPanels. I tried setting the maximum size, minimum size and all, and they didnt change any. Please help.

在这里,如果用户调整窗口大小,一切都会显得非常大。我知道的唯一另一种方法是将所有这些都添加到具有 FlowLayout 的单独 JPanel 中。但这并不实用,因为如果有 20 个东西,就会有 20 个 JPanel。我尝试设置最大尺寸、最小尺寸等,但它们没有任何改变。请帮忙。

回答by mKorbel

  1. not possible with plain GridLayout, because this is basic feature of this Layout Manager

  2. use proper Layout Manager GridBagLayoutor todays MigLayout

  3. very simple way is by using SpringLayout

  1. 普通GridLayout不可能,因为这是此布局管理器的基本功能

  2. 使用适当的布局管理器GridBagLayout或今天的MigLayout

  3. 非常简单的方法是使用SpringLayout

回答by george_h

@mKorbel and @Logan have a point in their answers, but if you for some reason really really REALLY need to use a GridLayout and you really need the JTextField to stay a certain size then you can do so by putting the JTextField inside a JPanel with a BorderLayout.

@mKorbel 和 @Logan 在他们的答案中有一点,但如果你出于某种原因真的真的真的需要使用 GridLayout 并且你真的需要 JTextField 保持一定的大小,那么你可以通过将 JTextField 放在 JPanel 中来做到这一点一个边框布局。

JTextField fixedWidthField = new JTextField();
JPanel fieldPanel = new JPanel(new BorderLayout());
fieldPanel.add(fixedWidthField, BorderLayout.WEST);

GridLayout grid = new GridLayout(2,2,2,2);

JPanel panel = new JPanel();
panel.setLayout(grid);
panel.add(b1);
panel.add(fieldPanel);
panel.add(b2);
panel.add(t2);

We all recommend you use a more advanced layout manager. I personally like TableLayout but you need to download an extra Jar file for that.

我们都建议您使用更高级的布局管理器。我个人喜欢 TableLayout 但你需要为此下载一个额外的 Jar 文件。

回答by Logan

That is the default behavior of gridLayout. I'm not sure its easy to stop it. I would use gridBagLayout instead. You can set the fill to none for each cell it has. It is more difficult at first, but it is all I use now.

这是 gridLayout 的默认行为。我不确定阻止它是否容易。我会改用 gridBagLayout 。您可以将每个单元格的填充设置为无。一开始比较困难,但我现在只用它。