这是我的代码:
public class InfoPanel extends JPanel{
... ... ...
... ... ...
public InfoPanel(){
... ... ...
... ... ...
MigLayout lManager = new MigLayout();
setLayout(lManager);
add(lblName,new CC().pos("50", "80"));
add(txtName, new CC().pos("90","80").width("170").wrap().gap("r"));
add(lblOccupation,"pos 20 100");
add(txtOccupation,"pos 91 100,w 170,wrap,gap r");
它有正确的输出。 但我想使用任何内置代码来计算txtName,txtOccupation的pos()。 任何人都可以帮助我做到这一点。
答案 0 :(得分:0)
从图像模板:
public class MigLayoutTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MigLayoutTest().start();
}
});
}
private void start() {
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new MigLayout("wrap 4, debug", "[][fill, grow 1][][fill, grow 4]", ""));
//Declaration
JLabel nameLabel, employmenLabel, ssnLabel, taxIDLabel;
JTextField nameField, ssnField, taxIDField;
JComboBox<String> employmentBox;
JCheckBox checkbox;
JButton save, cancel, upload;
JPanel imagePlaceholder;
//Initialization
nameLabel = new JLabel("Name");
employmenLabel = new JLabel("Employment");
ssnLabel = new JLabel("Social Security Number");
taxIDLabel = new JLabel("tax ID");
nameField = new JTextField();
ssnField = new JTextField();
taxIDField = new JTextField();
save = new JButton("Save");
cancel = new JButton("Cancel");
upload = new JButton("Upload");
checkbox = new JCheckBox("Checkbox");
imagePlaceholder = new JPanel();
employmentBox = new JComboBox<String>();
//Some editing
imagePlaceholder.setBackground(Color.GREEN);
employmentBox.addItem("Some");
employmentBox.addItem("items");
employmentBox.addItem("to");
employmentBox.addItem("fill");
employmentBox.addItem("this");
//Adding to contentPane
contentPane.add(nameLabel, "alignx right");
contentPane.add(nameField, "");
contentPane.add(ssnLabel, "alignx right");
contentPane.add(ssnField, "");
contentPane.add(employmenLabel, "alignx right");
contentPane.add(employmentBox, "");
contentPane.add(upload, "");
contentPane.add(imagePlaceholder, "spany 3, grow");
contentPane.add(checkbox, "split 2");
contentPane.add(taxIDLabel, "");
contentPane.add(taxIDField, "wrap");
contentPane.add(save, "");
contentPane.add(cancel, "growx 0, wrap");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}