gpt4 book ai didi

java - 使用单选按钮更改 JLabel 的字体时遇到问题

转载 作者:行者123 更新时间:2023-11-30 05:28:54 25 4
gpt4 key购买 nike

我正在开发一个类(class)程序,但在获取一组单选按钮来更改 JLabel 的字体时遇到了困难。我有适用于粗体和斜体的 JCheckBoxes,但是字体单选按钮不起作用。我是否必须调用重新加载或重新绘制方法才能更新字体?我可以通过在 StyleListener 方法中使用类似的代码来更改字体,因此我感觉问题出在 FontListener 方法中。我也尝试过通过代码进行调试,似乎 FontListener 方法中的代码实际上没有执行任何操作。我错过了什么?

//********************************************************************
// StyleOptionsPanel.java adapted from Java Foundations
//
// Demonstrates the use of check boxes.
//********************************************************************

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class StyleOptionsPanel extends JPanel
{
private JLabel saying;
private JCheckBox bold, italic;
private JRadioButton courier, times, helv;
private ButtonGroup fonts;
private int style = Font.PLAIN;
private String typeFace = "Helvetica";

//-----------------------------------------------------------------
// Sets up a panel with a label and some check boxes that
// control the style of the label's font.
//-----------------------------------------------------------------
public StyleOptionsPanel()
{
saying = new JLabel ("Say it with style!");
saying.setFont (new Font (typeFace, style, 20));

bold = new JCheckBox ("Bold");
bold.setBackground (Color.cyan);
italic = new JCheckBox ("Italic");
italic.setBackground (Color.cyan);

JRadioButton courier = new JRadioButton("Courier");
JRadioButton times = new JRadioButton("Times New Roman");
JRadioButton helv = new JRadioButton("Helvetica");

ButtonGroup fonts = new ButtonGroup();
fonts.add(courier);
fonts.add(times);
fonts.add(helv);

FontListener listener1 = new FontListener();
courier.addActionListener(listener1);
times.addActionListener(listener1);
helv.addActionListener(listener1);

StyleListener listener = new StyleListener();
bold.addItemListener (listener);
italic.addItemListener (listener);

add(courier);
add(times);
add(helv);
add (saying);
add (bold);
add (italic);

setBackground (Color.cyan);
setPreferredSize (new Dimension(300, 100));
}

//*****************************************************************
// Represents the listener for both check boxes.
//*****************************************************************
private class StyleListener implements ItemListener
{
//--------------------------------------------------------------
// Updates the style of the label font style.
//--------------------------------------------------------------
public void itemStateChanged (ItemEvent event)
{
style = Font.PLAIN;

if (bold.isSelected())
style = Font.BOLD;

if (italic.isSelected())
style += Font.ITALIC;

saying.setFont (new Font (typeFace, style, 20));

}
}

private class FontListener implements ActionListener{

// Updates the font of the String variable saying
public void actionPerformed(ActionEvent event){

Object source = event.getSource();
typeFace = "Helvetica";

if(source == courier) typeFace = "Courier";

else if(source == times) typeFace = "Times New Roman";

else typeFace = "Helvetica";

saying.setFont(new Font(typeFace, style, 20));

}
}
}

最佳答案

您没有在类中初始化单选按钮,它们为 null,因此 source 永远不会等于其中之一,并且始终为“Helvetica”。

您应该更改代码

JRadioButton courier = new JRadioButton("Courier");
JRadioButton times = new JRadioButton("Times New Roman");
JRadioButton helv = new JRadioButton("Helvetica");

courier = new JRadioButton("Courier");
times = new JRadioButton("Times New Roman");
helv = new JRadioButton("Helvetica");

为了初始化类字段并在 FontListener 中找到源代码。

关于java - 使用单选按钮更改 JLabel 的字体时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57967534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com