我对java swing有点新手,我正在尝试用手练习。在下面的示例中,我有一个设置,我有一个GridLayout以我想要的方式显示字段。但我需要重新调整文本字段的大小,因为我不希望它们都是那么恒定的大小。我需要一些更大或更小。有没有办法做到这一点?还有一种方法可以删除标签后的一些填充吗?
public void setupFrame() {
pnlTop.setLayout(new GridLayout(3, 4, 10, 10));
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);
getContentPane().add(pnlTop);
setTitle("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(300, 300);
pack();
}
答案 0 :(得分:2)
在GridLayout中重新调整文本字段的大小
GridLayout
的基本属性是在两个方向上调整其子级的大小,fills
/ schrinks
所有可用区域都来自其父级
还有办法在标签后删除一些填充吗?
pading由pnlTop.setLayout(new GridLayout(3, 4, 10, 10));
.... 10, 10)
定义
我需要一些更大或更小。有没有办法做到这一点?
答案 1 :(得分:0)
对于任何有兴趣的人,这是我在使用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();
}