Java 在JPanel中集中对齐JLabel中的文本时出现问题

Java 在JPanel中集中对齐JLabel中的文本时出现问题,java,swing,user-interface,jlabel,Java,Swing,User Interface,Jlabel,一般来说,我对Java和编程相当陌生,遇到了一个似乎无法解决的问题。在我的程序中,我有两个JPanel,分别包含一个JLabel和三个JButton,面板在框架内以网格布局排列 我的问题是,我无法让JLabel中的文本位于第一个JPanel的北面中央。最初我使用的是一个FlowLayout,它按照我想要的方向排列,但是当包含的字符串超过某个长度时,它就离开了屏幕。在当前设置中,文本会适当地换行,但开始时总是向左对齐 我在网站上搜索了很多答案——事实上,我认为我的问题可以通过使用HTML解决(这

一般来说,我对Java和编程相当陌生,遇到了一个似乎无法解决的问题。在我的程序中,我有两个JPanel,分别包含一个JLabel和三个JButton,面板在框架内以网格布局排列

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

我在网站上搜索了很多答案——事实上,我认为我的问题可以通过使用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();
    }
}
import java.awt.*;
导入javax.swing.*;
公共类UITests扩展了JFrame{
私有JLabel txtWindow=新JLabel();
私有JButton jb1=新JButton();
私有JButton jb2=新JButton();
私有JButton jb3=新JButton();
专用最终字体字体=新字体(“衬线”,Font.PLAIN,12);
公共UITests(){
//创建JLabel的顶部面板
JPanel panelA=新的JPanel(新的BorderLayout());
设置前景(颜色为白色);
setFont(新字体(“,Font.PLAIN,15));
txtWindow.setText(“+”梅隆“
+ "");
镶板A.立根背景(颜色:黑色);
panelA.add(txtWindow,BorderLayout.NORTH);
//为JButtons创建底部面板
JPanel panelB=新的JPanel(新的网格布局(3,1,5,5));
//设置按钮属性
jb1.立根背景(颜色:黑色);
jb2.立根背景(颜色:黑色);
jb3.立根背景(颜色:黑色);
jb1.设置前景(颜色.白色);
jb2.设置前景(颜色.白色);
jb3.设置前景(颜色.白色);
jb1.setFocusPainted(false);//停止突出显示按钮
jb2.SetFocuspaint(假);
jb3.setfocuspaint(假);
jb1.setFont(字体);
jb2.setFont(字体);
jb3.setFont(字体);
镶板B.立根背景(颜色:黑色);
b.增补(jb1);
b.增补(jb2);
b.增补(jb3);
setLayout(新的GridLayout(2,1,0,0));
加(a);
加(b);
设置标题(“屏幕”);
设置大小(500200);
setLocationRelativeTo(空);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(真);
}   
公共静态void main(字符串[]args){
新UITests();
}
}

如果有任何帮助,我们将不胜感激,如果有类似的问题,我们将表示歉意;我尽了最大的努力去找到它

只需使用
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();
    }
}
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();
    }
}
这将使文本在标签中居中。 然后您就不需要添加HTML,所以您还可以
setText(“甜瓜”)而不是使用HTML

编辑1 但是,对于短字符串和长字符串,请尝试将居中的HTMLdiv标记与
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();
    }
}
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();
    }
}
import java.awt.*;
导入javax.swing.*;
公共类主框架{
私有JLabel txtWindow=新JLabel();
私有JButton jb1=新JButton();
私有JButton jb2=新JButton();
私有JButton jb3=新JButton();
专用最终字体字体=新字体(“衬线”,Font.PLAIN,12);
公用干管(){
//创建JLabel的顶部面板
JPanel panelA=新的JPanel(新的BorderLayout());
设置前景(颜色为白色);
setFont(新字体(“,Font.PLAIN,15));
最终字符串文本=“梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅隆梅;
setText(“+text+”);
txtWindow.setHorizontalAlignment(JLabel.CENTER);
镶板A.立根背景(颜色:黑色);
panelA.add(txtWindow,BorderLayout.NORTH);
//为JButtons创建底部面板
JPanel panelB=新的JPanel(新的网格布局(3,1,5,5));
//设置按钮属性
jb1.立根背景(颜色:黑色);
jb2.立根背景(颜色:黑色);
jb3.立根背景(颜色:黑色);
jb1.设置前景(颜色.白色);
jb2.设置前景(颜色.白色);
jb3.设置前景(颜色.白色);
jb1.setFocusPainted(false);//停止突出显示按钮
jb2.SetFocuspaint(假);
jb3.setfocuspaint(假);
jb1.setFont(字体);
jb2.setFont(字体);
jb3.setFont(字体);
镶板B.立根背景(颜色:黑色);
b.增补(jb1);
b.增补(jb2);
b.增补(jb3);
setLayout(新的GridLayout(2,1,0,0));
加(a);
加(b);
设置标题(“屏幕”);
设置大小(500200);
setLocationRelativeTo(空);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(真);
}   
公共静态void main(字符串[]args){
新的Main();
}
}

希望这就是您想要的。

我尝试过这一方法,它适用于短字符串,但当它们的长度超过UI窗口的宽度时,屏幕上就会出现一个“…”符号添加标记会使文本正确换行,因此较长的字符串整体显示在屏幕上,但是第一行将居中对齐,而第二行将向左对齐-基本上与我最初遇到的问题相反。@Jameshorn我编辑了我的答案。使用setHorizontalAlignment和居中的HTMLdiv标记进行测试,它适用于短字符串和长字符串。