【问题标题】:How to make JTextField as wide as the window如何使 JTextField 与窗口一样宽
【发布时间】:2019-07-30 12:05:24
【问题描述】:

我想让文本自动与窗口一样宽。我尝试使用 text.setSize(window.getWidth(),20)text.setBounds(window.getWidth(),20),(其中文本是 JTextfield),但似乎唯一可行的方法是:static JTextField text = new JTextField(int numberOfColumns); 我正在使用 GridBag 布局。

【问题讨论】:

标签: java swing user-interface jtextfield


【解决方案1】:

编辑:我根据GridBagLayout编辑了示例。

使用布局管理器。它会根据窗口自动展开组件。 例如;

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

【讨论】:

  • 我想我忘了提到,我正在使用 GridBag 布局。我不知道这是否有区别
  • 是的,它带来了巨大的不同!在您的字段的GridBagConstraints 中,将gridx 设置为0,将gridwidth 设置为GridBagConstraints.REMAINDER and fill` 设置为GridBagConstraints.HORIZONTALGridBagConstraints.BOTH
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 2012-02-28
相关资源
最近更新 更多