在下面的代码中,我有2个布局,GridBag&然而,边界,我只能使用BorderLayout推送所有内容。我也想把一切都推到西方,我不知道我该怎么做。任何想法?
我希望所有内容都出现在JPanel的左上角。
public class Opspanel extends JPanel{
private static final long serialVersionUID = -6393281054430179953L;
public Opspanel() {
JPanel gridlayout = new JPanel();
GridBagLayout layout = new GridBagLayout();
gridlayout.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,10,10);
c.anchor = GridBagConstraints.LINE_START;
gridlayout.add(new JLabel("Vendor ID"), c);
c.gridx = 1;
c.gridy = 0;
JTextField vendorid = new JTextField(7);
gridlayout.add(vendorid, c);
c.gridx = 0;
c.gridy = 1;
gridlayout.add(new JLabel("Email Date"), c);
c.gridx = 1;
JTextField emaildate = new JTextField(10);
gridlayout.add(emaildate, c);
c.gridx = 2;
gridlayout.add(new JLabel("Email Time"), c);
c.gridx = 3;
JTextField emailtime = new JTextField(10);
gridlayout.add(emailtime, c);
setLayout(new BorderLayout());
add(gridlayout, BorderLayout.NORTH);
}
}
非常感谢您使用MigLayout让我的生活更轻松。它对我来说非常有效。我有一个问题。你能帮忙吗?
我想要停靠在北方的“OPS协调员”,我想让它垂直和水平居中。但是,我只能在垂直中心。
public class Opspanel extends JPanel{
private static final long serialVersionUID = -6393281054430179953L;
public Opspanel() {
MigLayout layout = new MigLayout();
setLayout(layout);
add(new JLabel("Vendor ID"), "cell 0 0");
JTextField txtVendorId = new JTextField();
add(txtVendorId, "h 20, w 100, gapleft 5, cell 1 0");
add(new JLabel("Email Date"),"cell 2 0, gapleft 5");
JTextField txtEmailDate = new JTextField();
add(txtEmailDate, "cell 3 0, h 20, w 100, gapleft 5");
add(new JLabel("Email Time"), "cell 4 0, gapleft 5");
JTextField txtEmailTime = new JTextField();
add(txtEmailTime, "cell 5 0, h 20, w 100, gapleft 5, wrap 15px");
add(new JLabel("Subject Line"), "cell 0 1");
JTextField txtSubLine = new JTextField();
add(txtSubLine, "cell 1 1, h 20, w 600, span, gapleft 5, wrap 15px");
add(new JLabel("Assigned"), "cell 0 2");
JTextField txtAssigned = new JTextField();
add(txtAssigned, "cell 1 2, h 20, w 150, gapleft 5, span 2");
add(new JLabel("M-Manager"), "cell 3 2, gapleft 5, align center");
JComboBox<String> cmbMM = new JComboBox<>();
add(cmbMM, "cell 4 2, h 20!, w 150, gapleft 5, wrap 15px, span 2");
add(new JLabel("City Name"), "cell 0 3");
JTextField txtCity = new JTextField();
add(txtCity, "cell 1 3, span 2, w 150, gapleft 5");
add(new JLabel("Region"), "cell 3 3, gapleft 5, align center");
JComboBox<String> cmbRegion = new JComboBox<>();
cmbRegion.addItem("North");
cmbRegion.addItem("South");
cmbRegion.addItem("West");
cmbRegion.addItem("East");
cmbRegion.setSelectedIndex(-1);
add(cmbRegion, "cell 4 3, h 20!, w 100, gapleft 5, wrap 15px, span 2");
add(new JLabel("OPS Coordinator"),"dock north, align center center, span, h 70!, wrap");
}
}