简体   繁体   English

最小化 JFrame 后 JTextField 正在移动

[英]JTextField is moving after minimizing the JFrame

I have just finished a project and ran into a problem I could not find anywhere on the internet so far.我刚刚完成了一个项目,遇到了一个到目前为止在互联网上找不到的问题。

I have several JTextFields on a JPanel on a JFrame.我在 JFrame 上的 JPanel 上有几个 JTextField。 When I open the frame, all text fields are in the right place.当我打开框架时,所有文本字段都在正确的位置。 If I minimize the frame and open it again, all text fields are moved and smaller.如果我最小化框架并再次打开它,所有文本字段都会移动并变小。 These are photos of my frame before and after I minimize it.这些是我最小化之前和之后的相框照片。

Before:前:

前

After:后:

后

panel = new JPanel();
panel.setBounds(0, 0, 400, 175);
panel.setVisible(true);
frame.add(panel);
field0 = new JTextField();
field0.setBounds(10, 55, 120, 20);
field0.setVisible(true);
Frame.panel.add(field0);
        
field1 = new JTextField();
field1.setBounds(250, 55, 125, 20);
field1.setVisible(true);
Frame.panel.add(field1);
        
field2 = new JTextField();
field2.setBounds(10, 105, 365, 20);
field2.setVisible(true);
Frame.panel.add(field2);

Swing was designed to be used with layout managers. Swing 旨在与布局管理器一起使用。 The job of the layout manager is to set the "size" and "location" of each components based on the preferred size of each component and the rules of the layout manager.布局管理器的工作是根据每个组件的首选大小和布局管理器的规则来设置每个组件的“大小”和“位置”。

In your code the setBounds(...) code is only temporary.在您的代码中, setBounds(...) 代码只是临时的。 When the frame is resized, the layout manager is invoked and the proper size/location is assigned to all components.调整框架大小时,会调用布局管理器,并将正确的大小/位置分配给所有组件。

So the solution is to not attempt to set the bounds manually but to use layout managers effectively.所以解决方案是不要尝试手动设置边界,而是有效地使用布局管理器。

Read the section from the Swing tutorial on Layout Managers for more information and working examples to get you started.阅读有关布局管理器的 Swing 教程中的部分,以获取更多信息和工作示例以帮助您入门。

Based on your example picture I would suggest you can use the GridBagLayout .根据您的示例图片,我建议您可以使用GridBagLayout It will allow you to create a GUI using row and columns.它将允许您使用行和列创建 GUI。 You can also have components span several columns.您还可以让组件跨越多个列。 I see the you have 5 rows and 3 columns.我看到你有 5 行和 3 列。 Download the working demo code from the tutorial and modify it for your requirements.从教程下载工作演示代码并根据您的要求修改它。

all text fields are moved and smaller.所有文本字段都被移动和缩小。

Each Swing component should determine its own preferred size.每个 Swing 组件都应确定其自己的首选大小。

When you use:当您使用:

field0 = new JTextField();

the preferred size is what you see.首选尺寸是您所看到的。

The better way to create the text field is to use:创建文本字段的更好方法是使用:

field0 = new JTextField(10);

Now the "10" will allow the text field to determines its preferred size to hold 10 "W" characters.现在“10”将允许文本字段确定其首选大小以容纳 10 个“W”字符。

Also, Swing components are visible by default, so you need need to use setVisible(true) for every component.此外,Swing 组件默认是可见的,因此您需要为每个组件使用 setVisible(true)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM