dimanche 22 janvier 2017

Checkbox is selected

I must change the font of the label when the user clicks one of the checkboxes 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 dosen'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);
            }              

        }

    }   

}




Aucun commentaire:

Enregistrer un commentaire