将JLabel置于组件之上

时间:2013-12-13 11:48:14

标签: java swing

我需要将JLabel对象添加到我的面板的特定组件。我使用setLabelFor方法,但是这个方法将标签nex添加到组件中。如何更改它以在组件顶部设置标签?

TextField sampleField = new TextField();
JLabel sampleLabel = new JLabel("sample text");
panel.add(sampleField);
sampleLabel.setLabelFor(sampleField);
panel.add(sampleLabel);

^这个将sampleLabel放在sampleField旁边。如何将sampleLabel放在sampleField的顶部?

先谢谢。

@Edit:

我使用这样的东西:

 public TabBody()
{
    setLayout(new BorderLayout());
    nameField = new TextField();
    //nameField.setSize(new Dimension(200, 200));
    //revalidate();
    nameLabel = new JLabel("name test");
    amountLabel = new JLabel("amount test");
    amountField = new TextField();
    unitsBox = new JComboBox(units);
    unitsBox.setSelectedIndex(3);

    nameLabel.setLabelFor(nameField);
    amountLabel.setLabelFor(amountField);
    add(nameLabel, BorderLayout.NORTH);
    add(amountLabel, BorderLayout.NORTH);

    add(nameField);
    add(amountField);
    add(unitsBox);
}

结果是:

enter image description here

我需要这样的事情:

enter image description here

2 个答案:

答案 0 :(得分:1)

默认情况下,JPanel使用FlowLayout,将该组件逐个放置。因为你有这种效果。

您需要使用正确的LayoutManager,例如使用BorderLayout

import java.awt.BorderLayout;
import java.awt.TextField;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Example {

    public static void main(String[] args){
        JFrame frame = new JFrame();

        JPanel panel = new JPanel(new BorderLayout());
        TextField sampleField = new TextField();
        JLabel sampleLabel = new JLabel("sample text");
        panel.add(sampleField);
        sampleLabel.setLabelFor(sampleField);
        panel.add(sampleLabel,BorderLayout.NORTH);

        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

enter image description here

如果您需要在网格中放置组件,例如逐个尝试使用GridBagLayout

    JPanel panel = new JPanel(new GridBagLayout());
    JTextField sampleField = new JTextField(5);
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.WEST;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(5, 5, 5, 5);

    JLabel sampleLabel = new JLabel("sample text");
    panel.add(sampleLabel,c);

    c.gridy++;
    panel.add(sampleField,c);

    c.gridy++;
    panel.add(new JLabel("sample text 2"),c);

    c.gridy++;
    panel.add(new JTextField(5),c);

    c.gridy++;
    panel.add(new JTextField(5),c);

使用gridx

gridyGridBagConstraints属性管理排名

enter image description here

答案 1 :(得分:0)

您必须使用布局来安排组件。对于谦虚的JPanel,FlowLayout会将您的组件排列在另一个旁边。您可以使用GridLayoutGridBagLayout

以下是示例代码:

TextField sampleField = new TextField();
JLabel sampleLabel = new JLabel("sample text");
panel.setLayout(new GridBagLayout());
sampleLabel.setLabelFor(sampleField);
GridBagConstraints = new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
panel.add(sampleLabel, gbc);
gbc.gridY=1;
panel.add(sampleField, gbc);