2

我不明白为什么这个 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 对象不会改变。非常感谢您的任何帮助,并提前非常感谢您。

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

4

2 回答 2

4

在您将侦听器添加到复选框时,值为listenerwas nulllistener直到 main 方法的最后才被初始化。

于 2012-11-05T21:01:50.207 回答
1

尝试在创建之前初始化侦听器JFrame。它对我有用。当然,FontListener类的定义必须相应地放在listener声明之前。

//...
listener = new FontListener();
JFrame frame = new JFrame();
//...
于 2012-11-05T21:07:36.703 回答