JTextField 固定高度

发布于 2024-10-29 02:46:40 字数 807 浏览 0 评论 0原文

当框架最大化时,如何使 JTextField 具有固定高度?我希望它看起来有点类似于 Ubuntu 上的 Skype 应用程序。

private JTextField username;
private JPasswordField password;
private JLabel usernamelabel;
private JLabel passwordlabel;
public LoginPanel(){


    setSize(200,200);
    setLayout(new GridLayout(4,4));
    setBackground(new Color(85,153,187));
    setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
    username = new JTextField();
    password = new JPasswordField();
    usernamelabel= new JLabel("Username");
    passwordlabel= new JLabel("Password");
    username.setBounds(5, 5, 100, 100);
    username.setPreferredSize(new Dimension(80,20));
    password.setPreferredSize(new Dimension(80,20));
    add(usernamelabel);
    add(username);
    add(passwordlabel);
    add(password);

How do I get the JTextField to have a fixed height when the Frame is maximized? I want it to look sort of similar to the Skype application on Ubuntu.

private JTextField username;
private JPasswordField password;
private JLabel usernamelabel;
private JLabel passwordlabel;
public LoginPanel(){


    setSize(200,200);
    setLayout(new GridLayout(4,4));
    setBackground(new Color(85,153,187));
    setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
    username = new JTextField();
    password = new JPasswordField();
    usernamelabel= new JLabel("Username");
    passwordlabel= new JLabel("Password");
    username.setBounds(5, 5, 100, 100);
    username.setPreferredSize(new Dimension(80,20));
    password.setPreferredSize(new Dimension(80,20));
    add(usernamelabel);
    add(username);
    add(passwordlabel);
    add(password);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

原野 2024-11-05 02:46:40

不要使用 GridLayout 以外的布局,也不要将文本字段放在 GridLayout 内部具有 FlowLayout 的另一个面板中。

Don't use a layout other than GridLayout or put the text field in another panel that has a FlowLayout that sits inside of your GridLayout.

听,心雨的声音 2024-11-05 02:46:40

我认为这可以解决你的问题。

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main { 

/**
 * @param args
 */
public static void main(String[] args) {
    JFrame failFrame = new JFrame();
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(0, 4));

    JPanel leftTextFieldPanel = new JPanel();
    leftTextFieldPanel.setLayout(new FlowLayout());        
    JPanel rightTextFieldPanel = new JPanel();
    rightTextFieldPanel.setLayout(new FlowLayout());

    JTextField leftTextField = new JTextField();
    leftTextField.setPreferredSize(new Dimension(150,20));              
    JTextField rightTextField = new JTextField();
    rightTextField.setPreferredSize(new Dimension(150,20));

    leftTextFieldPanel.add(leftTextField);
    mainPanel.add(leftTextFieldPanel);

    rightTextFieldPanel.add(rightTextField);
    mainPanel.add(rightTextFieldPanel);

    mainPanel.add(new JPanel());
    mainPanel.add(rightTextFieldPanel);
    mainPanel.add(new JPanel());

    failFrame.add(mainPanel);
    failFrame.setSize(600, 400);
    failFrame.setLocationRelativeTo(null);
    failFrame.setVisible(true);
}
}

我使用了嵌套布局。顶部是 GridLayout,文本字段位于 FlowLayout 中,并添加到 gridLayout 面板上。所以文本字段不再被拉伸。

I think this solves your problem.

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main { 

/**
 * @param args
 */
public static void main(String[] args) {
    JFrame failFrame = new JFrame();
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(0, 4));

    JPanel leftTextFieldPanel = new JPanel();
    leftTextFieldPanel.setLayout(new FlowLayout());        
    JPanel rightTextFieldPanel = new JPanel();
    rightTextFieldPanel.setLayout(new FlowLayout());

    JTextField leftTextField = new JTextField();
    leftTextField.setPreferredSize(new Dimension(150,20));              
    JTextField rightTextField = new JTextField();
    rightTextField.setPreferredSize(new Dimension(150,20));

    leftTextFieldPanel.add(leftTextField);
    mainPanel.add(leftTextFieldPanel);

    rightTextFieldPanel.add(rightTextField);
    mainPanel.add(rightTextFieldPanel);

    mainPanel.add(new JPanel());
    mainPanel.add(rightTextFieldPanel);
    mainPanel.add(new JPanel());

    failFrame.add(mainPanel);
    failFrame.setSize(600, 400);
    failFrame.setLocationRelativeTo(null);
    failFrame.setVisible(true);
}
}

I used nested Layouts. On the Top is your GridLayout and the textfields are in a FlowLayout which are added on the gridLayout panel. So the textfields are no longer stretched.

晨光如昨 2024-11-05 02:46:40

您可以参考这里

setMaximumSize 方法可以在 JComponentJTextField< /a> 从 JComponent 中使用此方法。

编辑:但是,对 JTextField 的大小进行硬编码似乎不是一个好习惯,特别是当您在多个平台上运行它时,因为您可能会遇到字体大小问题。

You can refer to here.

The setMaximumSize method can be found in JComponent and the JTextField gets to use this method from JComponent.

EDIT: However, it seems its not a good practice to hardcode the size of the JTextField particularly when you are running it on multiple platforms as you may face issues with the font-size.

月竹挽风 2024-11-05 02:46:40

感谢所有帮助的人,希望这对未来的其他人有所帮助。我得到了我正在寻找的视觉效果。

private JTextField username;
private JPasswordField password;
private JPanel [] login = {new JPanel(), new JPanel()};
private JLabel usernamelabel;
private JLabel passwordlabel;

public LoginPanel(){

    setSize(200,200);
    setBackground(new Color(85,153,187));
    setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
    username = new JTextField();
    password = new JPasswordField();
    usernamelabel= new JLabel("Username");
    passwordlabel= new JLabel("Password");
    login[0].add(usernamelabel);
    login[0].add(username);
    login[1].add(passwordlabel);
    login[1].add(password);
    for(JPanel p : login){
        p.setBackground(new Color(85,153,187));
        this.add(p);
    }
    username.setBounds(5, 5, 100, 100);
    username.setPreferredSize(new Dimension(120,20));
    password.setPreferredSize(new Dimension(120,20));

} 

Thanks for all the help guys, hope this helps out others in the future. I got the visual look that I was looking for.

private JTextField username;
private JPasswordField password;
private JPanel [] login = {new JPanel(), new JPanel()};
private JLabel usernamelabel;
private JLabel passwordlabel;

public LoginPanel(){

    setSize(200,200);
    setBackground(new Color(85,153,187));
    setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
    username = new JTextField();
    password = new JPasswordField();
    usernamelabel= new JLabel("Username");
    passwordlabel= new JLabel("Password");
    login[0].add(usernamelabel);
    login[0].add(username);
    login[1].add(passwordlabel);
    login[1].add(password);
    for(JPanel p : login){
        p.setBackground(new Color(85,153,187));
        this.add(p);
    }
    username.setBounds(5, 5, 100, 100);
    username.setPreferredSize(new Dimension(120,20));
    password.setPreferredSize(new Dimension(120,20));

} 
如日中天 2024-11-05 02:46:40

在紧要关头,这样做

tf.setMaximumSize(tf.getMinimumSize())

将使其保持恒定的规模和平台中立。

In a pinch, doing

tf.setMaximumSize(tf.getMinimumSize())

will keep it constant size and platform neutral.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文