1

我对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();
}
4

2 回答 2

2

在 GridLayout 中重新调整文本字段的大小

的基本属性GridLayout是在两个方向上调整其子级的大小和fills/schrinks所有可用区域都来自其父级

还有一种方法可以去除标签后的一些填充物吗?

填充由中.... 10, 10)定义pnlTop.setLayout(new GridLayout(3, 4, 10, 10));

我需要一些更大或更小的。有没有办法做到这一点?

必须看GridBagLayoutSpringLayout

于 2012-10-15T13:31:15.763 回答
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();
}
于 2012-10-15T17:01:46.273 回答