我想让文本自动与窗口一样宽。我尝试使用text.setSize(window.getWidth(),20)
和text.setBounds(window.getWidth(),20)
,(文本是JTextfield),但似乎工作的唯一方法是:static JTextField text = new JTextField(int numberOfColumns);
我正在使用GridBag布局。
编辑:我根据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);