如何控制 Java Swing 中 JTextField 的宽度?
我试图在一行上有多个 JTextField,但我不希望它们具有相同的宽度。如何控制宽度并使其中一些比其他更宽?我希望它们一起占据总宽度的 100%,所以如果我可以使用某种加权就更好了。
我尝试过 .setColumns()
但它没有意义。
这是一个示例,其中我使用三行和三个字符串,这些字符串应显示在列中:
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); }};
}
}
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.
I have tried with .setColumns()
but it doesn't make sense.
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); }};
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你像这样启动它,它会将文本字段设置为空并有 20 列
if you start it like this it sets the textfield to be empty and to have 20 collumns
试试这个:
Try this:
所有 Swing 组件都有一个首选大小。文本组件的首选大小基于组件的文本。因此,通常组件会按照其首选尺寸进行绘制。
所以我没有看到你的代码片段有问题。每个文本字段应具有不同的首选大小。此外,当调整框架大小时,宽度也会随之调整,因为 BoxLayout 会尝试将每个组件的大小调整为其最大/最小尺寸。
如果您需要更多帮助,请发布您的 SSCCE,它实际上说明了您遇到的尺寸问题。
编辑:
根据最新要求,您可以使用相对布局。
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.
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.
If you need more help post your SSCCE that actually demonstrates the sizing problem you are experiencing.
Edit:
Based on the latest requirement you can use the Relative Layout.