java为什么无法调用getFont()方法并发出NullPointerException。
/我的目标是将侦听器对象添加到此GUI,以便无论何时选定的字体样式或字体系列发生更改,或按下“确定”按钮,都会在文本字段中显示字体的全名。先谢谢你/
import java.awt.*;
import java.awt.event.*;
import java.awt.Component.*;
import javax.swing.*;
public class QuestionTwo {
public static void main(String[] args) {
CheckRadio two = new CheckRadio("Font Chooser");
two.init();
}
}
@SuppressWarnings("serial")
class CheckRadio extends JFrame {
public CheckRadio(String s) {
super(s);
}
public void init() {
JPanel check = new JPanel();
check.setLayout(new GridLayout(2, 1));
check.add(new JCheckBox("Bold"));
check.setFont(getFont().deriveFont(Font.BOLD)); //getFont() method can be found through out the code`enter code here. Bottom down at the Listener method I also did create a getFont() method for those listener.
check.add(new JCheckBox("Italic"));
check.setFont(getFont().deriveFont(Font.ITALIC));
JPanel radio = new JPanel();
radio.setLayout(new GridLayout(3, 1));
ButtonGroup group = new ButtonGroup();
JRadioButton ti = new JRadioButton("Times");
JRadioButton he = new JRadioButton("Heltivica");
JRadioButton co = new JRadioButton("Courier");
ti.setFont(getFont().deriveFont(Font.TRUETYPE_FONT,15));
he.setFont(getFont().deriveFont(Font.TRUETYPE_FONT,16));
co.setFont(getFont().deriveFont(Font.TRUETYPE_FONT,17));
group.add(ti);
group.add(he);
group.add(co);
radio.add(ti);
radio.add(he);
radio.add(co);
JLabel textBox = new JLabel();
textBox.setLayout(new GridLayout(3, 1));
textBox.add(new JLabel(""));
textBox.add(new JTextField(10));
textBox.add(new JLabel(""));
JLabel okButton = new JLabel();
okButton.setLayout(new GridLayout(3, 1));
okButton.add(new JLabel(""));
okButton.add(new JButton("OK"));
okButton.add(new JLabel(""));
Container panel = this.getContentPane();
panel.setLayout(new GridLayout(1, 4));
panel.add(check);
panel.add(radio);
panel.add(textBox);
panel.add(okButton);
ti.addActionListener(new TimesListener(textBox));
he.addActionListener(new HelvticaListener(textBox));
co.addActionListener(new CourierListener(textBox));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
}
class TimesListener implements ActionListener {
// JLabel okButton;
JButton okButton;
JRadioButton ti;
JCheckBox bo, it;
JLabel textBox;
Font f;
public TimesListener(JLabel textBox) {
this.textBox = textBox;
}
public TimesListener(JButton okButton, JRadioButton ti, JCheckBox bo,
JCheckBox it, JLabel textBox) {
this.okButton = okButton;
this.ti = ti;
this.bo = bo;
this.it = it;
this.textBox = textBox;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
if (ti.isSelected()) {
if (bo.isSelected()) {
if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD + Font.ITALIC,
15));
textBox.setText("Times");
} else {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD, 15));
textBox.setText("Times");
}
} else if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.ITALIC, 15));
textBox.setText("Times");
} else {
textBox.setFont(getFont()
.deriveFont(Font.TRUETYPE_FONT, 15));
textBox.setText("Times");
}
}
}
}
private Font getFont() {
return null;
}
}
class HelvticaListener implements ActionListener {
JButton okButton;
JRadioButton he;
JCheckBox bo, it;
JLabel textBox;
Font f;
public HelvticaListener(JLabel textBox) {
this.textBox = textBox;
}
public HelvticaListener(JButton okButton, JRadioButton he, JCheckBox bo,
JCheckBox it, JLabel textBox) {
this.okButton = okButton;
this.he = he;
this.bo = bo;
this.it = it;
this.textBox = textBox;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
if (he.isSelected()) {
if (bo.isSelected()) {
if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD + Font.ITALIC,
16));
textBox.setText("Helvtica");
} else {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD, 16));
textBox.setText("Helvtica");
}
} else if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.ITALIC, 16));
textBox.setText("Helvtica");
} else {
textBox.setFont(getFont()
.deriveFont(Font.TRUETYPE_FONT, 16));
textBox.setText("Helvtica");
}
}
}
}
private Font getFont() {
return null;
}
}
class CourierListener implements ActionListener {
// JLabel okButton;
JButton okButton;
JRadioButton co;
JCheckBox bo, it;
JLabel textBox;
Font f;
public CourierListener(JLabel textBox) {
this.textBox = textBox;
}
public CourierListener(JButton okButton, JRadioButton co, JCheckBox bo,
JCheckBox it, JLabel textBox) {
this.okButton = okButton;
this.co = co;
this.bo = bo;
this.it = it;
this.textBox = textBox;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
if (co.isSelected()) {
if (bo.isSelected()) {
if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD + Font.ITALIC,
18));
textBox.setText("Courier");
} else {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD, 18));
textBox.setText("Courier");
}
} else if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.ITALIC, 18));
textBox.setText("Courier");
} else {
textBox.setFont(getFont()
.deriveFont(Font.TRUETYPE_FONT, 18));
textBox.setText("Courier");
}
}
}
}
private Font getFont() {
return null;
}
}
# 1 楼答案
首先,您没有在
JFrame
中设置Font
,因此当您调用getFont
时,它不会返回Font
第二,这可以更干净。只需为按钮创建一个
ActionListender
soley,并在类的一部分中使用变量。在那里,你可以简单地添加一个函数来改变字体我这么做是因为我很无聊