自动调整GridBagLayout中组件的大小 [英] Automatically re-sizing a component within a GridBagLayout
问题描述
在这段代码中,我在GridBagLayout
中有一个面板,其中包含一个JLabel
和JTextField
.
In this code I have a panel in the GridBagLayout
which contains a JLabel
and a JTextField
.
import java.awt.*;
import javax.swing.*;
public class Simple
{
JFrame simpleWindow = new JFrame("Simple MCVE");
JPanel simplePanel = new JPanel();
JLabel lblSimple;
JTextField txtSimple;
public void numberConvertGUI()
{
simpleWindow.setBounds(10, 10, 420, 80);
simpleWindow.setMinimumSize(new Dimension(420, 80));
simpleWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
simpleWindow.setLayout(new GridLayout(1,1));
createSimplePanel();
simpleWindow.getContentPane().add(simplePanel);
simpleWindow.setVisible(true);
}
public void createSimplePanel()
{
simplePanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
lblSimple = new JLabel();
c.weightx = 0.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(0,2,0,2);
c.gridx = 0;
c.gridy = 0;
c.ipady = 0;
lblSimple.setText("Next to me is a JTextField: ");
lblSimple.setHorizontalAlignment(JLabel.RIGHT);
simplePanel.add(lblSimple, c);
txtSimple = new JTextField();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 5;
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(0,2,0,2);
simplePanel.add(txtSimple, c);
}
public static void main(String[] args)
{
Simple s = new Simple();
s.numberConvertGUI();
}
}
我希望能够根据输入的数据量自动调整文本字段的大小.例如,当字符串到达边缘时如何自动调整该组件的大小?"时,在JTextField中输入,看起来像这样.
I would like to be able to automatically re-size the text field dependent on the amount of data entered in it. For example when the string "How do I re-size this component automatically when the edge of it is reached?" is entered in the JTextField it looks like this.
但是,当我输入字符串时,我希望JTextBox
和JFrame
自动调整大小以产生看起来像这样的东西.
However as I enter the string I would like the JTextBox
and the JFrame
to automatically re-size to produce something which looks a bit like this.
唯一的问题是我不知道有什么可以做的.对于完成此任务的任何帮助,我将不胜感激.
The only problem is I am not aware of anything which allows me to do this. I would greatly appreciate any help with accomplishing this task.
修改
当组件自动调整大小时,我也希望该组件的最大大小.这样,随着输入更多数据,该组件将无法继续调整某人计算机显示器的大小
When the component is re-sized automatically I would also like there to be a maximum size for that component. This way as more data is entered the component will not keep re-sizing off of someone's computer monitor
推荐答案
Swing中没有内置功能可以做到这一点.
There is no built in functionality to do this provided within Swing.
您需要做的是在文本字段后面的文档中添加DocumentListener
,并在添加或删除文本时收到通知.
What you will need to do is add a DocumentListener
to the document behind the text field and be notified whenever text is added or removed from it.
然后,您将需要为文本字段计算所需的新大小(这可能很棘手-您可能需要使用FontMetrics
)并调整控件的大小以匹配.此时,只需查看要调整大小的尺寸与您希望允许的最大尺寸相比,即可轻松实现最大尺寸.
You will then need to calculate the new size you want for your text field (which can be tricky in of itself - you will probably need to use FontMetrics
) and re-size the control to match. The maximum size you can implement easily at this point just by looking at the size you are re-sizing to compared to the maximum you wish to allow.
有关DocumentListener
的信息,请参见此处:
https://docs.oracle.com/javase/tutorial/uiswing/events /documentlistener.html
See here for info on DocumentListener
:
https://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html
有关FontMetrics
的信息,请参见此处:
https://docs.oracle.com/javase/tutorial/2d/text /measuringtext.html
See here for info on FontMetrics
:
https://docs.oracle.com/javase/tutorial/2d/text/measuringtext.html
这篇关于自动调整GridBagLayout中组件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!