任何有興趣,這是我得到了什麼之後我做SpringLayout的形式。如果我在某處出錯,請糾正我:
public void setupFrame() {
SpringLayout layout = new SpringLayout();
pnlTop.setLayout(layout);
lblClosetLoc.setText("Closet location:");
lblPhone1.setText("Phone 1:");
lblJackPaired.setText("Jack paired:");
lblPhone2.setText("Phone 2:");
lblCubicle.setText("Cubicle:");
lblJackType.setText("Jack type:");
txtClosetLoc.setEditable(false);
txtClosetLoc.setText("");
txtPhone1.setEditable(false);
txtPhone1.setText("");
txtJackPaired.setEditable(false);
txtJackPaired.setText("");
txtPhone2.setEditable(false);
txtPhone2.setText("");
txtCubicle.setEditable(false);
txtCubicle.setText("");
txtJackType.setEditable(false);
txtJackType.setText("");
pnlTop.add(lblClosetLoc);
pnlTop.add(txtClosetLoc);
pnlTop.add(lblPhone1);
pnlTop.add(txtPhone1);
pnlTop.add(lblJackPaired);
pnlTop.add(txtJackPaired);
pnlTop.add(lblPhone2);
pnlTop.add(txtPhone2);
pnlTop.add(lblCubicle);
pnlTop.add(txtCubicle);
pnlTop.add(lblJackType);
pnlTop.add(txtJackType);
layout.putConstraint(layout.WEST, lblClosetLoc, 5, layout.WEST, pnlTop);
layout.putConstraint(layout.NORTH, lblClosetLoc, 1, layout.NORTH, txtClosetLoc);
layout.putConstraint(layout.WEST, txtClosetLoc, 5, layout.EAST, lblClosetLoc);
layout.putConstraint(layout.NORTH, txtClosetLoc, 5, layout.NORTH, pnlTop);
layout.putConstraint(layout.WEST, lblPhone1, 10, layout.EAST, txtCubicle);
layout.putConstraint(layout.NORTH, lblPhone1, 1, layout.NORTH, txtPhone1);
layout.putConstraint(layout.WEST, txtPhone1, 5, layout.EAST, lblJackType);
layout.putConstraint(layout.NORTH, txtPhone1, 5, layout.NORTH, pnlTop);
layout.putConstraint(layout.WEST, lblJackPaired, 5, layout.WEST, pnlTop);
layout.putConstraint(layout.NORTH, lblJackPaired, 1, layout.NORTH, txtJackPaired);
layout.putConstraint(layout.WEST, txtJackPaired, 22, layout.EAST, lblJackPaired);
layout.putConstraint(layout.NORTH, txtJackPaired, 5, layout.SOUTH, txtClosetLoc);
layout.putConstraint(layout.WEST, lblPhone2, 10, layout.EAST, txtCubicle);
layout.putConstraint(layout.NORTH, lblPhone2, 1, layout.NORTH, txtPhone2);
layout.putConstraint(layout.WEST, txtPhone2, 5, layout.EAST, lblJackType);
layout.putConstraint(layout.NORTH, txtPhone2, 5, layout.SOUTH, txtPhone1);
layout.putConstraint(layout.WEST, lblCubicle, 5, layout.WEST, pnlTop);
layout.putConstraint(layout.NORTH, lblCubicle, 1, layout.NORTH, txtCubicle);
layout.putConstraint(layout.WEST, txtCubicle, 47, layout.EAST, lblCubicle);
layout.putConstraint(layout.NORTH, txtCubicle, 5, layout.SOUTH, txtJackPaired);
layout.putConstraint(layout.WEST, lblJackType, 10, layout.EAST, txtCubicle);
layout.putConstraint(layout.NORTH, lblJackType, 1, layout.NORTH, txtJackType);
layout.putConstraint(layout.WEST, txtJackType, 5, layout.EAST, lblJackType);
layout.putConstraint(layout.NORTH, txtJackType, 5, layout.SOUTH, txtPhone2);
layout.putConstraint(layout.EAST, pnlTop, 5, layout.EAST, txtPhone1);
layout.putConstraint(layout.SOUTH, pnlTop, 5, layout.SOUTH, txtJackType);
getContentPane().add(pnlTop, BorderLayout.CENTER);
setTitle("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocation(300,300);
pack();
}
使用'GridBagLayout'或'MigLayout'。 – Stephan