java - 在面板中居中 JLabel

标签 java swing jpanel jlabel layout-manager

出于某种原因,“Java 文本”JLabel 没有居中。我查阅了如何执行此操作并看到了各种示例,最有希望的是 http://www.java2s.com/Code/Java/Swing-JFC/AsimpledemonstrationoftextalignmentinJLabels.htm但它不工作。如果您希望通过一些代码内注释自己运行它,那么这里是完整代码,这样您就不必费心搜索整个代码:

            import java.awt.BorderLayout;
            import java.awt.Color;
            import java.awt.Font;
            import java.awt.GridLayout;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;

            import javax.swing.ButtonGroup;
            import javax.swing.JCheckBox;
            import javax.swing.JComboBox;
            import javax.swing.JFrame;
            import javax.swing.JLabel;
            import javax.swing.JPanel;
            import javax.swing.JRadioButton;
            import javax.swing.SwingConstants;

            public class FontViewer
            {
                static JCheckBox checkBoxBold;
                static JCheckBox checkBoxItalic;
                static JCheckBox checkBoxCenter;
                static JPanel textPanel;
                static JLabel textLabel;
                static JComboBox fontName;
                static JComboBox fontSize;
                static JRadioButton redButton;
                static JRadioButton whiteButton;
                static JRadioButton blueButton;


                static ActionListener listener;

                public static void main(String[] args)
                {
                    final int FRAME_SIZE_X = 250;
                    final int FRAME_SIZE_Y = 400;

                    JFrame frame = new JFrame();
                    frame.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);

                    JPanel face = new JPanel();
                    face.setLayout(new GridLayout(2, 1));

            // listener inner class
                    class FontListener implements ActionListener
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            int fontStyle = 0;
                            if (checkBoxBold.isSelected())
                                fontStyle = fontStyle + Font.BOLD;
                            if (checkBoxItalic.isSelected())
                                fontStyle = fontStyle + Font.ITALIC;

            // this if statement does not work
                            if (checkBoxCenter.isSelected())
                                textLabel.setHorizontalAlignment(SwingConstants.CENTER);

                            String textFont = (String) fontName.getSelectedItem();

                            int textSize = Integer.parseInt((String) fontSize.getSelectedItem());

                            textLabel.setFont(new Font(textFont, fontStyle, textSize));

                            if (redButton.isSelected())
                                textLabel.setForeground(Color.RED);
                            else if (whiteButton.isSelected())
                                textLabel.setForeground(Color.WHITE);
                            else if (blueButton.isSelected())
                                textLabel.setForeground(Color.BLUE);

                            textLabel.repaint();
                        }
                    }

                    listener = new FontListener();

                    JPanel bottomFace = new JPanel();
                    bottomFace.setLayout(new GridLayout(3, 1));

                    textPanel = createTextPanel();

                    JPanel checkBoxPanel = createCheckBoxPanel();

                    JPanel comboPanel = createComboPanel();

                    JPanel radioButtonsPanel = createButtonsPanel();

                    face.add(textPanel);

                    bottomFace.add(checkBoxPanel);
                    bottomFace.add(comboPanel);
                    bottomFace.add(radioButtonsPanel);

                    face.add(bottomFace);

                    frame.add(face);

                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }

                private static JPanel createTextPanel()
                {
                    final int DEFAULT_FONT_SIZE = 12;

                    textPanel = new JPanel();

                    textPanel.setLayout(new BorderLayout());
                    textLabel = new JLabel("Java Text");
                    textLabel.setFont(new Font("Times", 0, DEFAULT_FONT_SIZE));
                    textPanel.add(textLabel, BorderLayout.WEST);

                    return textPanel;
                }

            // check boxes created and programmed here
                private static JPanel createCheckBoxPanel()
                {
                    JPanel checkBoxPanel = new JPanel();

                    checkBoxBold = new JCheckBox("Bold");
                    checkBoxItalic = new JCheckBox("Italic");
                    checkBoxCenter = new JCheckBox("Center");

                    checkBoxBold.addActionListener(listener);
                    checkBoxItalic.addActionListener(listener);
                    checkBoxCenter.addActionListener(listener);

                    checkBoxPanel.add(checkBoxBold);
                    checkBoxPanel.add(checkBoxItalic);
                    checkBoxPanel.add(checkBoxCenter);

                    return checkBoxPanel;
                }

                private static JPanel createComboPanel()
                {
                    JPanel comboPanel = new JPanel();

                    fontName = new JComboBox();
                    fontName.addItem("Times");
                    fontName.addItem("Serif");
                    fontName.addItem("Courier");

                    fontSize = new JComboBox();
                    fontSize.addItem("12");
                    fontSize.addItem("24");
                    fontSize.addItem("36");

                    comboPanel.add(fontName);
                    comboPanel.add(fontSize);

                    fontName.addActionListener(listener);
                    fontSize.addActionListener(listener);

                    return comboPanel;
                }

                private static JPanel createButtonsPanel()
                {
                    JPanel radioButtonsPanel = new JPanel();

                    redButton = new JRadioButton("Red");
                    whiteButton = new JRadioButton("White");
                    blueButton = new JRadioButton("Blue");

                    redButton.addActionListener(listener);
                    whiteButton.addActionListener(listener);
                    blueButton.addActionListener(listener);

                    ButtonGroup colors = new ButtonGroup();
                    colors.add(redButton);
                    colors.add(whiteButton);
                    colors.add(blueButton);

                    radioButtonsPanel.add(redButton);
                    radioButtonsPanel.add(whiteButton);
                    radioButtonsPanel.add(blueButton);

                    return radioButtonsPanel;
                }
            }

我对这种异常现象感到非常困惑,非常感谢任何帮助或建议!非常感谢您。

最佳答案

textLabel 锚定到 textPanelBorderLayout.WEST 位置。将它移到中心:

textPanel.add(textLabel, BorderLayout.CENTER);

关于java - 在面板中居中 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13257502/

相关文章:

java - 数据库未在实体类上使用注释创建

Java 多次使用 Scanner

java - Jbutton 未执行预期功能

java - 使用 GUI 的基于文本的冒险游戏存在无法通过按钮继续操作的问题

java - 如何使用 ActionListener 在 JPanel 上绘制一些形状?

java - java中动态添加组件

java - 如何使用内存中的 H2 数据库测试 JDBI DAO?

java - 忽略 SensorEventListener( compass 应用程序)的微小变化

c# - 不使用 Visual Studio GUI 设计器(工具箱)构建 C# GUI

java - 某些脚本即使以 UTF-8 编码,也无法在 JPanel 中正确显示?