【问题标题】:Trouble aligning text in a JLabel centrally within a JPanel无法在 JPanel 中集中对齐 JLabel 中的文本
【发布时间】:2020-01-11 11:53:41
【问题描述】:

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

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

Example screenshot of UI

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

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】:

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

    希望这是您正在寻找的。​​p>

    【讨论】:

    • 我试过这个,它适用于短字符串,但是当它们的长度超过 UI 窗口的宽度时,它们会以 '. . 。添加 标签会导致文本正确换行,因此更长的字符串会完整显示在屏幕上,但是第一行将居中对齐,但第二行向左对齐 - 基本上与我遇到的问题相反最初有。
    • @JamesHearn 我编辑了我的答案。使用 setHorizo​​ntalAlignment 和居中的 HTML div 标签进行测试,它适用于短字符串和长字符串。
    猜你喜欢
    • 2019-05-09
    • 1970-01-01
    • 2014-07-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 2012-01-29
    相关资源
    最近更新 更多