我对 Java 和一般编程相当陌生,我遇到了一个我似乎无法解决的问题。在我的程序中,我有两个 JPanel,分别包含一个 JLabel 和三个 JButton,这些面板排列在框架内的 GridLayout 中。
我的问题是我无法让 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();
}
}
对此的任何帮助将不胜感激,如果存在非常类似的问题,我们深表歉意;我已经尽力找到它了!
最佳答案
只需使用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();
}
}
希望这就是您正在寻找的内容。
关于java - 在 JPanel 中居中对齐 JLabel 中的文本时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59694154/