簡體   English   中英

我的文字沒有改變。 我不明白為什么

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

我不明白為什么這個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的值為null 直到main方法的最后,才初始化listener器。

創建JFrame之前,請嘗試初始化偵聽器。 它為我工作。 當然,必須將FontListener類的定義相應地放在listener聲明之前。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM