使 JLabel 文本垂直居中对齐

Make a JLabel text vertically aligned to the center

如何使 JLabel 文本垂直和水平居中对齐?

我必须使用 setHorizontalTextPositionsetVerticalTextPosition。这可以通过使用这两个来实现吗?

我试过了,但文字本身仍位于顶部。

 import java.awt.FlowLayout;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.SwingConstants;

public class label extends JFrame
{
  private JLabel label;

    public label() //constructor
    {
      super("Simple GUI");
      setLayout(new FlowLayout());

      label=new JLabel("Centered JLabel");
      label.setHorizontalTextPosition(SwingConstants.CENTER);
      label.setVerticalTextPosition(SwingConstants.CENTER);
      add(label);
    }
 }

很抱歉不知道为什么setHorizo​​ntalTextPosition方法和setVerticalTextPosition方法不起作用。

但是,我会告诉您,有几种方法可以通过在创建标签时插入参数与字符串对齐来对标签中的文本进行排序。

首先,camickr 的回答是最好的回答,因为 Swing 被设计为与布局管理器一起使用!!

请记住,最推荐的方法是将GridBagLayout()应用于camickr的JFrame的Layout。

This answer is intended to inform you that This method is also possible, but not recommended in the normal case

Using this method is not recommended because changing the size of the frame keeps the components in place, but it could give you a little help when it is indicated that the size and location of components in the container are used in a way that can be used in special cases, such as when they need to be fixed without external impact.

为了更好地了解问题,我将简单地更改您上传给我的代码,以便它可以执行。

import java.awt.FlowLayout;

 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.SwingConstants;

 import java.awt.Color;

public class StackOver extends JFrame
{
  private JLabel label;

    public StackOver() //constructor
    {
      super("Simple GUI");
      setLayout(null);
      setSize(500,300);     

      label=new JLabel("Centered JLabel", JLabel.CENTER); 


      /*
      label.setHorizontalTextPosition(SwingConstants.CENTER);
      label.setVerticalTextPosition(SwingConstants.CENTER);*/

      add(label);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        new StackOver();
    }
 }

嗯,让我们在 JLabel 的背景中添加一些颜色以了解更多信息。

label.setOpaque(true);            //Transparency Settings
label.setBackground(Color.pink);      //Specify background color
//to use 'Color' class, We must import java.awt.Color;

JFrame 设置为 FlowLayout(),这使得组件的位置和大小固定,所以我们看不到它的工作!

所以现在我们要完成接下来的两个过程。 1) 将 JFrame 的布局更改为 null 以使用绝对布局 ("not recommended, just a case") 2) 改变 JLabel 的大小和位置 完成后我们可以看到文本对齐有效!

1) 改变 JFrame 的布局

following link: Layout Manager 表明 FlowLayout 之外还有很多布局。

Swing 被设计为使用布局管理器,所以你当然应该使用上面的布局之一link。

但是,要使用一种有趣的方式来绝对固定组件的位置和大小,我们将切换到 setLayout(null);

2) 更改 JLabel 的大小和位置

我们可以直接用setBounds(int startX, int startY, int Width, int Height)方法改变JLabel的大小和位置!

import java.awt.FlowLayout;
import java.awt.Rectangle;

 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.SwingConstants;

 import java.awt.Color;

public class StackOver extends JFrame
{
  private JLabel label;

    public StackOver() //constructor
    {
      super("Simple GUI");
      setLayout(null);
      setSize(500,300);


      label=new JLabel("Centered JLabel", JLabel.CENTER); 
      // Since it also sets the size of the JLabel, 
      //let's use the constructor to set the alignment of the text inside the JLabel.

      label.setOpaque(true);                //Transparency Settings
      label.setBackground(Color.pink);      //Specify background color
      //to use 'Color' class, We must import java.awt.Color;

      Rectangle r = this.getBounds();       //to get Frame Size
      label.setBounds(r.x+100, r.y+100, r.width-200, r.height-200);

      /*
      label.setHorizontalTextPosition(SwingConstants.CENTER);
      label.setVerticalTextPosition(SwingConstants.CENTER);*/

      add(label);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        new StackOver();
    }
 }

现在我们可以正常看到中间的 JLabel 文本了!

该方法可以直接固定所有组件(ex按钮)的大小和位置,但是即使程序运行后JFrame的window大小发生变化,组件的位置和大小也是固定的。

所以如果你真的想通过这种方式绝对设置组件,你宁愿通过在 JFrame 的代码上添加以下代码来设置 JFrame Resizable false

this.setResizable(false);

希望回答对您有所帮助,祝您生活愉快!

I have tried but the text remains at the top itself.

你有两个问题:

  1. Andrew 解决了第一个问题。你用错方法了。
  2. 接下来你使用了错误的布局。 FlowLayout 仅在一行中显示组件,因此组件将始终位于顶部。不要更改布局管理器。 JFrame 的默认布局管理器是 BorderLayout。当您将组件添加到 CENTER 时(这是您未指定约束时的默认设置),组件的大小将调整为填充整个框架。然后 "alignment" 属性将在分配给标签的大小内控制文本的位置。

或者另一种选择是使用 GridBagLayout。然后你不需要玩组件的对齐选项:

setLayout( new GridBagLayout() );
add(label, new GridBagConstraints());

尝试这两个选项,因为它们在不同情况下可能都有效。

阅读 Layout Managers 上的 Swing 教程,以更好地了解每个布局管理器的工作原理。