java 创建一个动态调整其宽度的文本字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5555355/
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
Creating a text field that dynamically resizes its width
提问by Nate W.
I'm making what is basically a toolbar that contains a search field and some buttons. I'd like for the search field to grow in size (just the width) when the parent container gets wider, like when the user adjusts the split pane. Currently, the text field and the buttons will remain the same size and whitespace is added on either side as the container is widened. I can achieve this growing effect by using a BorderLayout
in the container and putting the buttons on LINE_END
and the text field in the CENTER
. The problem I have with this is that the text field now becomes taller than a standard text field and it looks ugly. This behavior makes sense as the BorderLayout
manager will give all the extra space (this includes vertical and hortizontal space) to the CENTER
text field. I've tried to restrict this vertical growth by placing a maximum size on the text field, but BorderLayout
will not honor it.
我正在制作一个基本上包含搜索字段和一些按钮的工具栏。当父容器变宽时,例如用户调整拆分窗格时,我希望搜索字段的大小(仅宽度)增加。目前,文本字段和按钮将保持相同的大小,并且随着容器的加宽,在任一侧添加空格。我可以通过使用一个实现这一增长的影响BorderLayout
在容器中,并把上的按钮LINE_END
在和文本字段CENTER
。我遇到的问题是文本字段现在变得比标准文本字段高,而且看起来很难看。这种行为是有道理的,因为BorderLayout
经理会将所有额外的空间(包括垂直和水平空间)分配给CENTER
文本域。我试图通过在文本字段上设置最大尺寸来限制这种垂直增长,但BorderLayout
不会兑现。
Here's what I've got:
这是我所拥有的:
final JTextField searchField = new JTextField("Enter your search terms");
searchField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
final JPanel controls = new JPanel(new BorderLayout());
controls.add(searchField, BorderLayout.CENTER);
controls.add(new JPanel(){{
add(new JButton("Search")); add(new JButton("I'm Feeling Lucky"));
}}, BorderLayout.LINE_END);
It would seem that this behavior is a commonly desired one and it should be easy to implement, but I've had no luck after looking through all the Oracle/Sun tutorials and Google search results.
看起来这种行为是普遍期望的,而且应该很容易实现,但在浏览了所有 Oracle/Sun 教程和 Google 搜索结果后,我没有走运。
Anybody have any solutions to this? I need to stick with standard Java Swing components - no third party libraries please.
有人对此有任何解决方案吗?我需要坚持使用标准的 Java Swing 组件 - 请不要使用第三方库。
Thank you!
谢谢!
采纳答案by Gursel Koca
I would suggest you to use GridBagLayout , it is complicated but it is the most powerful layout.. When you learn it, you would not have any layout issue.
我建议你使用 GridBagLayout ,它很复杂,但它是最强大的布局。当你学习它时,你不会有任何布局问题。
Here is the sample use of gridbaglayout for this question...
这是这个问题的 gridbaglayout 的示例使用......
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class GridBagLayoutExample extends JFrame {
GridBagLayoutExample() {
initUI();
}
private void initUI() {
final JTextField searchField = new JTextField("Enter your search terms");
final JPanel controls = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx=1.0;
c.fill=GridBagConstraints.HORIZONTAL;
controls.add(searchField,c);
controls.add(new JPanel(){
{
add(new JButton("Search")); add(new JButton("I'm Feeling Lucky")); }});
setLayout(new BorderLayout());
add(controls, BorderLayout.NORTH);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GridBagLayoutExample().setVisible(true);
}
});
}
}
回答by kleopatra
it's a two part problem:
这是一个两部分的问题:
a) all xxSize (xx = min/max/pref) are mere hintsto the LayoutManager. Each and every LayoutManager has its own behaviour if/how/when it respects those hints and if/how/when it violates one or more if the overall space is more or less than would fit the sum of pref for all components in the container. No way but learn which does what (documentation is ... ehem ... suboptimal)
a) 所有 xxSize (xx = min/max/pref) 都只是对 LayoutManager 的提示。每个 LayoutManager 都有自己的行为,如果/如何/何时尊重这些提示,如果/如何/何时违反一个或多个,如果整体空间大于或小于适合容器中所有组件的 pref 总和。没办法,只能学习哪个做什么(文档是......呃......次优)
b) JTextField is a bit crazy in allowing its height to grow indefinitely if space allows. Implying that even LayoutManagers which respect max (like f.i. BoxLayout) have no chance to do so, max is Integer.MAX_VALUE (or Short.MAX? forgot). To make it behave, subclass and override maxSize to return the pref height:
b) 如果空间允许,JTextField 允许其高度无限增长,这有点疯狂。这意味着即使是尊重 max(如 fi BoxLayout)的 LayoutManagers 也没有机会这样做,max 是 Integer.MAX_VALUE(或 Short.MAX?忘记了)。为了使其行为,子类化并覆盖 maxSize 以返回首选项高度:
@Override
Dimension getMaximumSize() {
Dimension max = super.getMaximumSize();
max.height = getPreferredSize().height;
return max;
}
回答by bEn
Using BoxLayout.
使用BoxLayout。
JTextField search_field = new JTextField("Enter search term");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(search_field);
This will dynamically change the size of textfield when resizing the frame.
这将在调整框架大小时动态更改文本字段的大小。
回答by camickr
You should be able to use a horizontal BoxLayout.
您应该能够使用水平 BoxLayout。
searchField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
You should not guess at the height of the text field.
您不应该猜测文本字段的高度。
Dimension d = searchField.getPreferredSize();
d.width = Integer.MAX_VALUE;
searchField.setMaximumSize(d);
I know you said you don't want to use 3rd party libraries, but as a side note you might want to look at Text Field Prompt. It allows you to display a message that will disapear when you start typing in the text field.
我知道你说过你不想使用 3rd 方库,但作为旁注,你可能想看看Text Field Prompt。它允许您显示一条消息,当您开始在文本字段中输入时,该消息将消失。