首页 > 解决方案 > 无法在 JPanel 中集中对齐 JLabel 中的文本

问题描述

我对 Java 和一般编程还很陌生,我遇到了一个我似乎无法解决的问题。在我的程序中,我有两个 JPanel,分别包含一个 JLabel 和三个 JButton,并且面板排列在框架内的 GridLayout 中。

我的问题是我无法将 JLabel 中的文本放在第一个 JPanel 以北的中央。最初我使用的是按照我想要的方向排列它的 FlowLayout,但是当包含的字符串变得超过一定长度时,它就离开了屏幕。在当前设置下,文本会适当地换行,但总是从左对齐开始。

UI 的示例屏幕截图

我已经搜索了该站点并查看了多个答案 - 实际上我认为我的问题可以通过使用 HTML 来解决(这确实解决了包装问题并将第二行居中对齐,如下所示) - 但我仍然想不通找出如何将第一行居中对齐。

使用更长字符串的示例

这是我当前的代码:

import java.awt.*;
import javax.swing.*;


public class UITests extends JFrame {

    private JLabel txtWindow = new JLabel();
    private JButton jb1 = new JButton();
    private JButton jb2 = new JButton();
    private JButton jb3 = new JButton();
    private final Font font = new Font("Serif", Font.PLAIN, 12);

    public UITests() {

        //Creating the top panel for the JLabel
        JPanel panelA = new JPanel(new BorderLayout());

        txtWindow.setForeground(Color.white);
        txtWindow.setFont(new Font("", Font.PLAIN, 15));
        txtWindow.setText("<html><div style='text-align: center;'>" + "Mellon"
                + "</div></html>");
        panelA.setBackground(Color.BLACK);
        panelA.add(txtWindow, BorderLayout.NORTH);


        //Creating the bottom panel for the JButtons
        JPanel panelB = new JPanel(new GridLayout(3, 1, 5, 5));

        //setting up button properties
        jb1.setBackground(Color.BLACK);
        jb2.setBackground(Color.BLACK);
        jb3.setBackground(Color.BLACK);

        jb1.setForeground(Color.white);
        jb2.setForeground(Color.white);
        jb3.setForeground(Color.white);

        jb1.setFocusPainted(false); //Stopping highlighting of button
        jb2.setFocusPainted(false);
        jb3.setFocusPainted(false);

        jb1.setFont(font);
        jb2.setFont(font);
        jb3.setFont(font);


        panelB.setBackground(Color.BLACK);
        panelB.add(jb1);
        panelB.add(jb2);
        panelB.add(jb3);

        setLayout(new GridLayout(2, 1, 0, 0));
        add(panelA);
        add(panelB);

        setTitle("Screen");
        setSize(500, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }   

    public static void main(String[] args) {
        new UITests();
    }
}

对此的任何帮助将不胜感激,如果存在非常相似的问题,我们深表歉意;我尽力找到了!

标签: javaswinguser-interfacejlabel

解决方案


只需使用txtWindow.setHorizontalAlignment(JLabel.CENTER);,就像这样:

import java.awt.*;
import javax.swing.*;

public class Main extends JFrame {

    private JLabel txtWindow = new JLabel();
    private JButton jb1 = new JButton();
    private JButton jb2 = new JButton();
    private JButton jb3 = new JButton();
    private final Font font = new Font("Serif", Font.PLAIN, 12);

    public Main() {

        //Creating the top panel for the JLabel
        JPanel panelA = new JPanel(new BorderLayout());

        txtWindow.setForeground(Color.white);
        txtWindow.setFont(new Font("", Font.PLAIN, 15));
        txtWindow.setText("Mellon");
        txtWindow.setHorizontalAlignment(JLabel.CENTER);
        panelA.setBackground(Color.BLACK);
        panelA.add(txtWindow, BorderLayout.NORTH);


        //Creating the bottom panel for the JButtons
        JPanel panelB = new JPanel(new GridLayout(3, 1, 5, 5));

        //setting up button properties
        jb1.setBackground(Color.BLACK);
        jb2.setBackground(Color.BLACK);
        jb3.setBackground(Color.BLACK);

        jb1.setForeground(Color.white);
        jb2.setForeground(Color.white);
        jb3.setForeground(Color.white);

        jb1.setFocusPainted(false); //Stopping highlighting of button
        jb2.setFocusPainted(false);
        jb3.setFocusPainted(false);

        jb1.setFont(font);
        jb2.setFont(font);
        jb3.setFont(font);


        panelB.setBackground(Color.BLACK);
        panelB.add(jb1);
        panelB.add(jb2);
        panelB.add(jb3);

        setLayout(new GridLayout(2, 1, 0, 0));
        add(panelA);
        add(panelB);

        setTitle("Screen");
        setSize(500, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }   

    public static void main(String[] args) {
        new Main();
    }
}

这将使标签中的文本居中。那么您就不需要添加 HTML,因此您也可以setText("Melon");直接添加,而不是使用 HTML。

编辑 1

对于短字符串和长字符串,尽管尝试将居中的 HTML div 标记和setHorizontalAlignment(JLabel.CENTER);调用结合起来,如下所示:

import java.awt.*;
import javax.swing.*;

public class Main extends JFrame {

    private JLabel txtWindow = new JLabel();
    private JButton jb1 = new JButton();
    private JButton jb2 = new JButton();
    private JButton jb3 = new JButton();
    private final Font font = new Font("Serif", Font.PLAIN, 12);

    public Main() {

        //Creating the top panel for the JLabel
        JPanel panelA = new JPanel(new BorderLayout());

        txtWindow.setForeground(Color.white);
        txtWindow.setFont(new Font("", Font.PLAIN, 15));
        final String text = "Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon";
        txtWindow.setText("<html><div style='text-align: center;'>" + text + "</div></html>");
        txtWindow.setHorizontalAlignment(JLabel.CENTER);
        panelA.setBackground(Color.BLACK);
        panelA.add(txtWindow, BorderLayout.NORTH);


        //Creating the bottom panel for the JButtons
        JPanel panelB = new JPanel(new GridLayout(3, 1, 5, 5));

        //setting up button properties
        jb1.setBackground(Color.BLACK);
        jb2.setBackground(Color.BLACK);
        jb3.setBackground(Color.BLACK);

        jb1.setForeground(Color.white);
        jb2.setForeground(Color.white);
        jb3.setForeground(Color.white);

        jb1.setFocusPainted(false); //Stopping highlighting of button
        jb2.setFocusPainted(false);
        jb3.setFocusPainted(false);

        jb1.setFont(font);
        jb2.setFont(font);
        jb3.setFont(font);


        panelB.setBackground(Color.BLACK);
        panelB.add(jb1);
        panelB.add(jb2);
        panelB.add(jb3);

        setLayout(new GridLayout(2, 1, 0, 0));
        add(panelA);
        add(panelB);

        setTitle("Screen");
        setSize(500, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }   

    public static void main(String[] args) {
        new Main();
    }
}

希望这是您正在寻找的。


推荐阅读