java swing 状态栏 文本宽度,如何在Java Swing中控制JTextField的宽度?

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); }};

}

}

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我们可以通过Java编写一个简单的签到程序,使用Swing实现用户界面。 首先,我们需要创建一个Java项目,并添加Swing库以便使用Swing组件。 然后,我们可以创建一个JFrame窗口来作为我们的用户界面。在JFrame,我们可以添加一些Swing组件,如JLabel、JTextField、JButton等。 下面是一个简单的签到程序示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Signin extends JFrame { private JLabel nameLabel, idLabel, statusLabel; private JTextField nameField, idField; private JButton signinButton; public Signin() { // 设置窗口标题 setTitle("签到"); // 创建组件 nameLabel = new JLabel("姓名:"); idLabel = new JLabel("学号:"); statusLabel = new JLabel("请填写信息并点击签到按钮"); nameField = new JTextField(20); idField = new JTextField(20); signinButton = new JButton("签到"); // 设置布局 setLayout(new GridLayout(3, 2)); // 添加组件 add(nameLabel); add(nameField); add(idLabel); add(idField); add(statusLabel); add(signinButton); // 注册事件监听器 signinButton.addActionListener(new SigninListener()); // 设置窗口大小和关闭方式 setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 签到按钮事件监听器 private class SigninListener implements ActionListener { public void actionPerformed(ActionEvent event) { String name = nameField.getText(); String id = idField.getText(); if (name.isEmpty() || id.isEmpty()) { statusLabel.setText("姓名和学号不能为空"); } else { statusLabel.setText(String.format("%s %s 签到成功", name, id)); } } } public static void main(String[] args) { Signin signin = new Signin(); signin.setVisible(true); } } ``` 这个示例,我们创建了一个JFrame窗口,并添加了三个JLabel标签、两个JTextField文本框和一个JButton按钮。我们还为按钮注册了一个事件监听器,在用户点击签到按钮时会触发该监听器,从而进行签到操作。 当用户点击签到按钮时,我们会获取输入的姓名和学号,如果输入为空,则提示用户姓名和学号不能为空;否则,我们会在状态栏显示签到成功的信息。 你可以根据自己的需求进行修改和扩展,比如添加数据库连接进行数据存储等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值