JTextField 固定高度
当框架最大化时,如何使 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要使用
GridLayout
以外的布局,也不要将文本字段放在GridLayout
内部具有FlowLayout
的另一个面板中。Don't use a layout other than
GridLayout
or put the text field in another panel that has aFlowLayout
that sits inside of yourGridLayout
.我认为这可以解决你的问题。
我使用了嵌套布局。顶部是 GridLayout,文本字段位于 FlowLayout 中,并添加到 gridLayout 面板上。所以文本字段不再被拉伸。
I think this solves your problem.
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.
您可以参考这里。
setMaximumSize 方法可以在 JComponent 和 JTextField< /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.
感谢所有帮助的人,希望这对未来的其他人有所帮助。我得到了我正在寻找的视觉效果。
Thanks for all the help guys, hope this helps out others in the future. I got the visual look that I was looking for.
在紧要关头,这样做
将使其保持恒定的规模和平台中立。
In a pinch, doing
will keep it constant size and platform neutral.