有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    首先,您没有在JFrame中设置Font,因此当您调用getFont时,它不会返回Font

    第二,这可以更干净。只需为按钮创建一个ActionListendersoley,并在类的一部分中使用变量。在那里,你可以简单地添加一个函数来改变字体

    我这么做是因为我很无聊

    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);
    
        }
    }