【发布时间】:2020-01-17 22:07:11
【问题描述】:
我正在编写一个课堂程序,但无法获得一组单选按钮来更改 JLabel 的字体。我有适用于粗体和斜体的 JCheckBox,但是字体单选按钮不起作用。我必须调用重新加载或重新绘制方法来更新字体吗?我可以通过在 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));
}
}
}
【问题讨论】:
-
除非提供“逻辑字体”(
Font.SERIF、Font.MONOSPACED等),否则依赖 可用字体列表会更安全,如当前的 JRE。在“Helvetica”中可以看到一个很好的理由。我猜环境是 OS X。这里(Windows)没有 Helvetica。Arial 是 MS 的未修饰字体。例如,在运行时确定已安装字体的名称时间,见this answer。