JLabel没有显示顶部方向

时间:2014-11-26 01:38:56

标签: java layout jframe jlabel

我一直在为我的程序制作一个菜单,但我在使用JLabel方面遇到了一些问题,因为我对它们没有太多经验。

我有一个JFrame设置,按钮和JEditorPanes / JTextField的布局很好但是当我尝试将JLabel添加到前面的两个JEditorPanes时,它们显示在左侧。

任何帮助将不胜感激,谢谢:)

http://imgur.com/WlXoaRw

public class WindowWin extends JFrame implements ActionListener {

JPanel[] row = new JPanel[4];
JButton[] button = new JButton[4];
String[] buttonString = { "Copy to Clipboard", "Go", "Back", "Info" };
JLabel[] label = new JLabel[4];
String[] labelString = {"Label1",
                        "Label2",
                        "Label3",
                        ""
};

int[] dimW = { 400, 200, 65 };
int[] dimH = { 40, 100 };

Dimension keyDim = new Dimension(dimW[0], dimH[0]);
Dimension displayDimension = new Dimension(dimW[0], dimH[1]);
Dimension butDim = new Dimension(dimW[1], dimH[0]);
Dimension infoDim = new Dimension(dimW[2], dimH[0]);

JEditorPane display = new JEditorPane();
Font font = new Font("Times new Roman", Font.PLAIN, 14);

JTextField keyIn = new JTextField(24);
JEditorPane msgIn = new JEditorPane();

JScrollPane scrollerD = new JScrollPane(display,
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane scrollerM = new JScrollPane(msgIn,
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

public static void main(String[] args) {
    WindowWin c = new WindowWin();
}

WindowWin() {
    super("Test");

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    GridBagLayout grid = new GridBagLayout();
    setLayout(grid);
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.fill = GridBagConstraints.HORIZONTAL;

    FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
    FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);

    for (int i = 0; i < 4; i++)
        row[i] = new JPanel();

    row[0].setLayout(f1);

    for (int i = 1; i < 4; i++) {
        row[i].setLayout(f2);
    }
    for (int i = 0; i < 4; i++) {
        button[i] = new JButton();
        button[i].setText(buttonString[i]);
        button[i].setFont(font);
        button[i].addActionListener(this);
    }

    for(int i = 0; i < 2; i++)
    {
        label[i] = new JLabel(labelString[i]);
        row[i].add(label[i]);
        label[i].setVerticalAlignment(JLabel.TOP);
        label[i].setHorizontalAlignment(JLabel.CENTER);
        label[i].setText(labelString[i]);
    }




    display.setFont(font);
    display.setEditable(false);
    display.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    display.setPreferredSize(displayDimension);

    keyIn.setFont(font);
    keyIn.setEditable(true);
    keyIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    keyIn.setPreferredSize(keyDim);

    msgIn.setFont(font);
    msgIn.setEditable(true);
    msgIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    msgIn.setPreferredSize(displayDimension);

    for (int i = 0; i < 2; i++)
        button[i].setPreferredSize(butDim);
    for (int i = 2; i < 4; i++)
        button[i].setPreferredSize(infoDim);

    row[0].add(scrollerD);
    add(row[0], gbc);

    row[1].add(scrollerM);
    add(row[1], gbc);

    row[2].add(button[0]);
    row[2].add(button[1]);
    add(row[2], gbc);

    row[3].add(button[2]);
    row[3].add(keyIn);
    row[3].add(button[3]);
    add(row[3], gbc);

    pack();
    setLocationRelativeTo(null);
    setVisible(true);

}

/*
 * public final void setDesign() { try { UIManager.setLookAndFeel(
 * "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch(Exception e)
 * { } }
 */
public void actionPerformed(ActionEvent ae) {

    if (ae.getSource() == button[0]) {

    }

    if (ae.getSource() == button[1]) {
        display.setText("Test");
    }
}

public void clear() {
    try {
        display.setText("");
    } catch (NullPointerException e) {
    }
}

public void outd() {
    display.setText("");
}

}

1 个答案:

答案 0 :(得分:0)

如前所述,请查看Laying Out Components Within a Container

您可以使用TitledBorder ...

enter image description here

scrollerD.setBorder(new CompoundBorder(
                new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Happy Bananas"),
                scrollerD.getBorder()));
scrollerM.setBorder(new CompoundBorder(
                new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Happy Strewberries"),
                scrollerM.getBorder()));

看一看 How to Use Borders  了解更多详情