java - 我的文字没有改变。我不明白为什么

标签 java user-interface

我不明白为什么这段 Java 代码不起作用。这是我正在开发的一个 GUI 项目,我试图通过一些复选框将 JLabel 更改为粗体、斜体等:

import java.awt.BorderLayout;
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;

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

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

    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;
            if (checkBoxCenter.isSelected())
                textPanel.add(textLabel, BorderLayout.CENTER);

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

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

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

    listener = new FontListener();
}

private static JPanel createTextPanel() {
    textPanel = new JPanel();

    textPanel.setLayout(new BorderLayout());
    textLabel = new JLabel("Java Text");
    textPanel.add(textLabel, BorderLayout.WEST);

    return textPanel;
}

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("Serif");
    fontName.addItem("Courier");

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

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

    return comboPanel;
}

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

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

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

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

    return radioButtonsPanel;
}

}

当我按下任何复选框时,JLabel 对象不会更改。非常感谢任何帮助,并提前非常感谢您。

注意:到目前为止,我只想知道为什么这些复选框不起作用。这段代码不完整,我知道这一点。再次感谢您。

最佳答案

将监听器添加到复选框时,listener 的值为 nulllistener 直到 main 方法最后才初始化。

关于java - 我的文字没有改变。我不明白为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13240189/

相关文章:

java - XSnippet EmailBean 错误

java - 有没有办法在javafx中显示从meshview到平面绘图的uv?

java - 在 Java 中,如何在每次进入或退出给定对象的监视器时记录消息?

c# - 桌面应用程序爬虫

iphone - performSegueWithIdentifier 不工作

java - 在 libsvm 中使用预计算内核会导致卡住

java - 从数据库获取数据时出现 NullPointerException,使用注解在 Struts 2 中调用 Action

android - 为正在进行的操作提供用户反馈的建议 - Android 3.0?

c# - 在 Surface Pro 3 中开发 WinForm 应用程序时出现 GUI 预览大小问题

iOS : UI - Design login page and Tab view - Tab not coming after Login