为什么不能调用getFont()方法并给出NullPointerException。

时间:2015-02-12 22:01:37

标签: java swing user-interface fonts awt

/ 我的目标是为此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 个答案:

答案 0 :(得分:1)

首先,您尚未在Font中设置JFrame,因此当您致电Font时,它将无法返回getFont。< / p>

其次,这可以更清洁。您只需为按钮创建一个ActionListender soley,并在类的一部分上创建要使用的变量。在那里你可以简单地添加一个功能来改变字体。

我为你做了这件事因为我很无聊

import java.awt.Container;
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.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class QuestionTwo {

    public static void main(String[] args) {
        CheckRadio two = new CheckRadio("Font Chooser");
        two.init();

    }
}

@SuppressWarnings("serial")
class CheckRadio extends JFrame {

    JButton okButton;
    JCheckBox bold;
    JCheckBox italic;
    JRadioButton ti;
    JRadioButton he;
    JRadioButton co;
    ButtonGroup group;
    JTextField tf;

    public CheckRadio(String s) {
        super(s);
    }

    public void init() {

        setFont(new Font("Times", Font.PLAIN, 12));

        JPanel check = new JPanel();
        check.setLayout(new GridLayout(2, 1));

        bold = new JCheckBox("Bold");
        bold.setFont(getFont().deriveFont(Font.BOLD));
        italic = new JCheckBox("Italic");
        italic.setFont(getFont().deriveFont(Font.ITALIC));

        check.add(bold);
        check.add(italic);

        JPanel radio = new JPanel();
        radio.setLayout(new GridLayout(3, 1));
        group = new ButtonGroup();

        ti = new JRadioButton("Times");
        he = new JRadioButton("Heltivica");
        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));

        tf = new JTextField(10);

        textBox.add(new JLabel(""));
        textBox.add(tf);
        textBox.add(new JLabel(""));

        JLabel okLabel = new JLabel();
        okLabel.setLayout(new GridLayout(3, 1));

        okButton = new JButton("OK");

        okLabel.add(new JLabel(""));
        okLabel.add(okButton);
        okLabel.add(new JLabel(""));

        Container panel = this.getContentPane();
        panel.setLayout(new GridLayout(1, 4));
        panel.add(check);
        panel.add(radio);
        panel.add(textBox);
        panel.add(okLabel);

        okButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                updateFont();

            }
        });

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    public void updateFont() {
        int derivedFont = Font.TRUETYPE_FONT;
        String text = "";
        int font = 0;
        if (italic.isSelected())
            derivedFont += Font.ITALIC;
        if (bold.isSelected())
            derivedFont += Font.BOLD;
        if (ti.isSelected()){
            font = 15;
            text = "times";
        } else if (he.isSelected()){
            font = 16;
            text = "helvtica";
        } else if (co.isSelected()){
            font = 18;
            text = "courrier";
        }

        tf.setFont(getFont().deriveFont(derivedFont, font));
        tf.setText(text);

    }
}