简体   繁体   English

如何使JTextField与窗口一样宽

[英]How to make JTextField as wide as the window

I want to make the text automatically as wide as the window. 我想使文本自动和窗口一样宽。 I tried using text.setSize(window.getWidth(),20) and text.setBounds(window.getWidth(),20) , (where text is JTextfield), but the only way that seems to work is: static JTextField text = new JTextField(int numberOfColumns); 我尝试使用text.setSize(window.getWidth(),20)text.setBounds(window.getWidth(),20) (其中文本为JTextfield),但是唯一可行的方法是: static JTextField text = new JTextField(int numberOfColumns); I'm using GridBag layout. 我正在使用GridBag布局。

EDIT : I have edited example according to GridBagLayout. 编辑 :我已经根据GridBagLayout编辑了示例。

Use layout manager. 使用布局管理器。 It will automatically expands component according to window. 它将根据窗口自动扩展组件。 For example; 例如;

Jpanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = GridBagConstraints.REMAINDER;
panel.add(textfield,c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM