简体   繁体   中英

Checkbox is selected

I must change the font of the label when the user clicks one of the CheckBox es from the GUI . Right now the GUI works but when I click a checkbox I get a long error message in console and the text doesn't change. Here is my code so far hope you can help me:

package fontp;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;


public class FontP extends Frame{
    Label l1;
    Checkbox c1, c2, c3, c4;
    Panel p1, p2;
    CheckboxGroup cbg;

    FontP(String title) {
        super(title);
        ListenerB ab = new ListenerB();

        Panel p1 = new Panel();
        Panel p2 = new Panel();
        Label l1 = new Label("Some random text");
        CheckboxGroup cbg = new CheckboxGroup();
        Checkbox c1 = new Checkbox("Normal", cbg, false);
        c1.addItemListener(ab);
        Checkbox c2 = new Checkbox("Bold", cbg, false);
        c2.addItemListener(ab);
        Checkbox c3 = new Checkbox("Italic", cbg, false);
        c3.addItemListener(ab);
        Checkbox c4 = new Checkbox("Bold/Italic", cbg, false);
        c4.addItemListener(ab);

        p1.add(l1);
        p2.add(c1); p2.add(c2); p2.add(c3); p2.add(c4);

        add(p1,BorderLayout.NORTH);
        add(p2);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent ev) {
                    System.exit(0);
                }
        });       

    }

    class ListenerB implements ItemListener {
        public void itemStateChanged(ItemEvent ev) {
            if(c1.getState() == true) {
                Font f = new Font(l1.getText(), Font.PLAIN, 12);
                l1.setFont(f);                    
            }
            else if(c2.getState() == true) {
                Font f = new Font(l1.getText(), Font.BOLD, 12);
                l1.setFont(f);
            }
            else if(c3.getState() == true) {
                Font f = new Font(l1.getText(), Font.ITALIC, 12);
                l1.setFont(f);
            }
            else if(c4.getState() == true) {
                Font f = new Font(l1.getText(), Font.BOLD | Font.ITALIC, 12);
                l1.setFont(f);
            }      
        }
    }   
}

You don't initialize the fields c1, c2, ... in the constructor but define new variables. So the fields c1, c2, ... are still null and this causes the exception. Do this in your constructor:

    p1 = new Panel();
    p2 = new Panel();
    l1 = new Label("Some random text");
    cbg = new CheckboxGroup();
    c1 = new Checkbox("Normal", cbg, false);
    c1.addItemListener(ab);
    c2 = new Checkbox("Bold", cbg, false);
    c2.addItemListener(ab);
    c3 = new Checkbox("Italic", cbg, false);
    c3.addItemListener(ab);
    c4 = new Checkbox("Bold/Italic", cbg, false);
    c4.addItemListener(ab);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM