我正在编写一个 Java 程序(下面的最小可编译示例),并且我试图通过排列一些元素来美化用户界面。如果你运行这个,你可以看到,如果我能把它们做成相同的大小d_ip
,d_pword
看起来会好很多。而且我希望它们即使在您调整窗口大小时也能动态保持相同的大小。
GroupLayout 有一种linkSize()
方法可以让您链接不相关对象的大小。这适用于按钮和标签。但是对于 JTextField,这是一场灾难。即使我将其中一个的最小尺寸设置为很大,它仍然坚持将尺寸设置为零。
我还尝试过诸如告诉 GroupLayout 水平最小和首选尺寸是否有效GroupLayout.DEFAULT_SIZE
或GroupLayout.PREFERRED_SIZE
无济于事的事情。谁能告诉我是否有任何方法可以强制 JTextField 与 linkSize 一起正常工作?
请注意,要打开 linkSize,有一行标有注释,您需要取消注释。当您这样做时,JTextField 的宽度会变得混乱。此外,尝试添加d_username
到链接对象列表。现在,即使存在“紧张”,您可能会说,要使用户名/密码行水平与窗口的宽度一致,字段的大小为零并且不会调整大小。
谢谢!
package textfield;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
public class TextField extends JFrame {
JTextField d_mount, d_ip, d_share, d_username, d_pword;
JRadioButton nfs, smb;
JButton save, revert;
TextField() {
Container pane = this.getContentPane();
JLabel lmount = new JLabel("Mount Point:");
JLabel ltype = new JLabel("Share Type:");
JLabel lip = new JLabel("IP Address:");
JLabel lshare = new JLabel("Remote Share:");
JLabel lusername = new JLabel("Username:");
JLabel lpassword = new JLabel("Password:");
d_mount = new JTextField();
nfs = new JRadioButton("NFS");
smb = new JRadioButton("SMB");
ButtonGroup group = new ButtonGroup();
group.add(nfs);
group.add(smb);
d_ip = new JTextField();
d_share = new JTextField();
d_username = new JTextField();
d_pword = new JTextField();
d_ip.setMinimumSize(new Dimension(200, 0));
save = new JButton("Save");
revert = new JButton("Revert");
GroupLayout layout = new GroupLayout(pane);
pane.setLayout(layout);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lmount)
.addComponent(d_mount)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(ltype)
.addComponent(nfs)
.addComponent(smb)
// )
//.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lip)
.addComponent(d_ip)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lshare)
.addComponent(d_share)
)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE, true)
.addComponent(lusername)
.addComponent(d_username)
.addComponent(lpassword)
.addComponent(d_pword)
)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(revert)
.addComponent(save)
)
);
layout.setHorizontalGroup(
layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(lmount)
.addComponent(ltype)
.addComponent(lshare)
.addComponent(lusername)
)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(d_mount)
.addGroup(layout.createSequentialGroup()
.addComponent(nfs)
.addComponent(smb)
.addGap(20, 20, Integer.MAX_VALUE)
.addComponent(lip)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(d_ip, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
)
.addComponent(d_share)
.addGroup(layout.createSequentialGroup()
.addComponent(d_username, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lpassword)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(d_pword, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
)
)
)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Integer.MAX_VALUE)
.addComponent(revert)
.addComponent(save)
)
);
// ****** ADD THIS LINE BACK IN ******
//layout.linkSize(SwingConstants.HORIZONTAL, d_pword, d_ip);
pack();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TextField().setVisible(true);
}
});
}
}