如何在 Java Swing 中控制 JTextFields 的宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2897506/
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
How can I control the width of JTextFields in Java Swing?
提问by Jonas
I am trying to have several JTextFields on a single row, but I don't want them to have the same width. How can I control the width and make some of them wider than others? I want that they together take up 100% of the total width, so it would be good if I could use some kind of weigthing.
我试图在一行上有几个 JTextFields,但我不希望它们具有相同的宽度。如何控制宽度并使其中一些比其他宽度更宽?我希望它们一起占据总宽度的 100%,所以如果我可以使用某种称重就好了。
I have tried with .setColumns()but it doesn't make sense.
我试过,.setColumns()但没有意义。
Here is an example, where I am using three rows with three strings that should appear as in columns:
这是一个示例,其中我使用三行和三个字符串,这些字符串应显示为列:
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RowTest extends JPanel {
class Row extends JComponent {
public Row(String str1, String str2, String str3) {
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
JTextField fld1 = new JTextField(str1);
JTextField fld2 = new JTextField(str2);
JTextField fld3 = new JTextField(str3);
fld1.setColumns(5); // makes no sense
this.add(fld1);
this.add(fld2);
this.add(fld3);
}
}
public RowTest() {
this.setLayout(new GridLayout(5,0));
this.add(new Row("Short", "A long text that takes up more space",
"Short again"));
this.add(new Row("Longer but short", "Another long string", "Short"));
this.add(new Row("Hello", "The long field again",
"Some info"));
}
public static void main(String[] args) {
new JFrame() {{ this.getContentPane().add(new RowTest());
this.pack(); this.setVisible(true); }};
}
}
采纳答案by camickr
All Swing components have a preferred size. The preferred size of a text component is based on the text of the component. So generally the component is painted at its preferred size.
所有 Swing 组件都有一个首选大小。文本组件的首选大小基于组件的文本。因此,通常组件以其首选尺寸进行绘制。
So I don't see the problem with your code snippet. Each text field should have a different preferred size. Also, as the frame is resized the width will be adjusted since the BoxLayout will try to resize each component up to its maximum/minimum size.
所以我看不出你的代码片段有什么问题。每个文本字段应具有不同的首选大小。此外,随着框架的大小调整,宽度也会随之调整,因为 BoxLayout 会尝试将每个组件的大小调整到其最大/最小尺寸。
If you need more help post your SSCCEthat actually demonstrates the sizing problem you are experiencing.
如果您需要更多帮助,请发布您的SSCCE,以实际展示您遇到的尺寸问题。
Edit:
编辑:
Based on the latest requirement you can use the Relative Layout.
根据最新要求,您可以使用相对布局。
回答by Xorty
// 1. create JTextField
yourTextField = new JTextField();
// 2. store default height
int defaultHeight = yourTextField.getSize().getHeight();
// 3. set custom width and default height
yourTextField.setSize(new Dimension(yourWidth, defaultHeight));
回答by Michael Pankhurst
yourTextField = new JTextField("", 20);
if you start it like this it sets the textfield to be empty and to have 20 collumns
如果你这样开始它会将文本字段设置为空并有 20 列
回答by Psychosis404
Try this:
尝试这个:
textfield.setPreferredSize(new Dimension(int width, int height));

