这是我要去的结果:
但这就是我得到的:
现在,我理解GridBagConstraints.HORIZONTAL;
允许组件水平占用任何空白空间,但我不确定如何让组件仅填充其网格空间的剩余量。这是我当前的GUI代码,包括GridBagConstraints:
public class LoginPanel extends JPanel {
private JTextField userfield;
private JPasswordField passfield;
private JButton login;
private JButton create;
public LoginPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = .2;
gbc.anchor = GridBagConstraints.EAST;
JLabel label = new JLabel("Username: ");
gbc.gridx = 1;
add(label, gbc);
label = new JLabel("Password: ");
gbc.gridy = 1;
add(label, gbc);
gbc.anchor = GridBagConstraints.WEST;
userfield = new JTextField(10);
gbc.gridx = 2;
gbc.gridy = 0;
add(userfield, gbc);
passfield = new JPasswordField(10);
gbc.gridy = 1;
add(passfield, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
login = new JButton("Login");
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 2;
add(login, gbc);
create = new JButton("Create Account");
gbc.gridy = 3;
add(create, gbc);
JTextPane textpane = new JTextPane();
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 10;
add(textpane, gbc);
}
}
有没有办法使用GridBagLayout实现这种格式化?如果没有,你建议我使用哪种布局/布局?
如果你仍然对我的目标感到困惑,我试图让按钮填充足够的空间,以便与标签和字段保持一致,但是底部的文本窗格填满了整个屏幕。
答案 0 :(得分:1)
摆脱界限
gbc.weightx = .2;
它正在弄乱fill
属性......
已更新,错过了一项要求
好的,你可以用余生来完成这项工作,或者你可以将布局分解为可管理的部分并将它们单独布局......
这个例子基本上将字段和按钮放在自己的容器中,并专注于它们的布局要求。然后,它会将此文本字段添加到LoginPane
并单独关注其要求...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class TestLayout100 {
public static void main(String[] args) {
new TestLayout100();
}
public TestLayout100() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new LoginPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LoginPanel extends JPanel {
private JTextField userfield;
private JPasswordField passfield;
private JButton login;
private JButton create;
public LoginPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JPanel fields = new JPanel(new GridBagLayout());
gbc.anchor = GridBagConstraints.EAST;
JLabel label = new JLabel("Username: ");
gbc.gridx = 1;
fields.add(label, gbc);
label = new JLabel("Password: ");
gbc.gridy = 1;
fields.add(label, gbc);
gbc.anchor = GridBagConstraints.WEST;
userfield = new JTextField(10);
gbc.gridx = 2;
gbc.gridy = 0;
fields.add(userfield, gbc);
passfield = new JPasswordField(10);
gbc.gridy = 1;
fields.add(passfield, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
login = new JButton("Login");
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 2;
fields.add(login, gbc);
create = new JButton("Create Account");
gbc.gridy = 3;
fields.add(create, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(fields, gbc);
JTextPane textpane = new JTextPane();
gbc.weightx = 1;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(textpane, gbc);
}
}
}