【问题标题】:How to align the bottom of 3 JLabels that are held in a JPanel如何对齐 JPanel 中的 3 个 JLabel 的底部
【发布时间】:2012-01-25 15:28:13
【问题描述】:

我正在尝试对齐包含图像的 3 个 JLabel 的底部。 3 个 JLabel 保存在一个大 JPanel 中。 I found a tutorial about GUI using Java Swing here. 但由于某种原因,如果我应用示例代码(为按钮提供),它不适用于 JLabels 或 JPanel。 这是来自 Oracle 网站的示例代码:

button1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
button2.setAlignmentY(Component.BOTTOM_ALIGNMENT);

知道出了什么问题吗?我可以发送我的代码,但我认为这可能会让它变得太混乱,因为对于这里的大多数人来说,这可能是一个简单的答案太简单的问题。

提前致谢。

编辑:

public class LayoutOef_01 extends JFrame{

    JPanel paneel;
    JLabel label1, label2, label3;
    ImageIcon pic1, pic2, pic3;
    Border panelBord, labelBord;


    public Layout_01(String titel){
        super(titel);

        paneel = new JPanel();

        pic1 = new ImageIcon("images/simon1.png");
        pic2 = new ImageIcon("images/simon2.png");
        pic3 = new ImageIcon("images/simon3.png");

        label1 = new JLabel(pic1);
        label2 = new JLabel(pic2);
        label3 = new JLabel(pic3);

        paneel.add(label1);
        paneel.add(label2);
        paneel.add(label3);

        panelBoord = BorderFactory.createLineBorder(Color.WHITE, 30);
        paneel.setBorder(panelBord);
        paneel.setBackground(Color.WHITE);

        labelBoord = BorderFactory.createLineBorder(Color.BLACK, 2);
        label1.setBorder(labelBord);
        label2.setBorder(labelBord);
        label3.setBorder(labelBord);

        this.getContentPane().add(paneel);
        this.pack();
    }

    public static void main(String[] args) {
        Layout_01 lay1 = new LayoutOef_01("Layout_01");
        lay1.setVisible(true);
    }

}

所以我尝试将以下代码 - 在不同的地方 - 放在上面的代码中,但没有任何变化:

label1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
label2.setAlignmentY(Component.BOTTOM_ALIGNMENT);
label3.setAlignmentY(Component.BOTTOM_ALIGNMENT);

【问题讨论】:

  • 添加您的代码。这可能是Glue 的问题,可能是BoxLayout 被配置为垂直而不是水平堆叠所有内容。
  • 你的代码怎么会太混乱?测试它应该是大约 20 行代码。如果它适用于按钮,它应该适用于标签。发布您的 SSCCE 来证明问题。

标签: java swing user-interface alignment


【解决方案1】:

查看此示例:http://www.java2s.com/Code/JavaAPI/java.awt/ComponentBOTTOMALIGNMENT.htm

记住: - 在面板上设置布局。 - 设置按钮上的对齐方式 - 将按钮添加到面板。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 2019-05-09
  • 2015-07-06
  • 1970-01-01
  • 2014-01-02
  • 2016-09-13
相关资源
最近更新 更多