【问题标题】:JLabel not showing on top orientationJLabel 未显示在顶部方向
【发布时间】:2015-01-24 05:41:06
【问题描述】:

我一直在为我的程序设计一个菜单,但我在使用 JLabels 时遇到了一些问题,因为我对它们不是很有经验。

我设置了 JFrame,按钮和 JEditorPanes/JTextField 的布局很好,但是当我尝试将 JLabels 添加到前 2 个 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("");
}

}

【问题讨论】:

标签: java layout jframe jlabel


【解决方案1】:

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

你可以使用TitledBorder...

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 了解更多

【讨论】:

    猜你喜欢
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    相关资源
    最近更新 更多