简体   繁体   English

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

[英]My text does not change. I don't understand why

I don't understand why this Java code isn't working. 我不明白为什么这个Java代码无法正常工作。 It's a GUI project I'm working on and I'm trying to get the JLabel to change to bold, italic, etc. via some check boxes: 这是我正在处理的一个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;
}

} }

When I press any of the check boxes, the JLabel object does not change. 当我按下任何一个复选框时,JLabel对象都不会更改。 Any help is greatly appreciated and thank you so much in advance. 非常感谢您的帮助,在此先感谢您。

Note: As of now, I only wish to know why the check boxes are not working. 注意:到目前为止,我只想知道为什么复选框不起作用。 This code is incomplete, I'm aware of this. 这段代码不完整,我知道这一点。 Thank you once again. 再一次感谢你。

At the time you added the listener to the check boxes, the value of listener was null . 在将侦听器添加到复选框时, listener的值为null listener is not initialized until the very end of the main method. 直到main方法的最后,才初始化listener器。

Try to initialize the listeners before creating JFrame . 创建JFrame之前,请尝试初始化侦听器。 It worked for me. 它为我工作。 Of course the definition of the FontListener class has to be put before listener declaration accordingly. 当然,必须将FontListener类的定义相应地放在listener声明之前。

//...
listener = new FontListener();
JFrame frame = new JFrame();
//...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM