在JPanel中央居中对齐JLabel中的文本

问题描述 投票:0回答:1

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

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

Example screenshot of UI

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

Example using longer String

这是我当前的代码:

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();
    }
}

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

java swing user-interface jlabel
1个回答
0
投票

只需使用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();
    }
}

希望这是您想要的。

© www.soinside.com 2019 - 2024. All rights reserved.