简体   繁体   中英

Change Font color of textfield input via JCheckBox

I'm making an address book and I'd like for the user to be able to color code their contacts [such as all 'friends' are printed in blue font, all family are green, etc.] I added checkboxes and I'm adding action listeners. However, I am getting a compilation error.

    friend = new JCheckBox("Friend");
    coWorker = new JCheckBox("Business");
    family = new JCheckBox("Family");
    miscellaneous = new JCheckBox("Miscellaneous");


jPanel4.add(friend);
    jPanel4.add(coWorker);
    jPanel4.add(family);
    jPanel4.add(miscellaneous);

    HandlerClass handler = new HandlerClass();
    friend.addItemListener(handler);
    coWorker.addItemListener(handler);
    family.addItemListener(handler);
    miscellaneous.addItemListener(handler);


jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
            AddressBookMain.addEntry(new AddressBook(jTextField1.getText(), jTextField2.getText(), jTextField3.getText(), jTextField4.getText()));
        }
    });

`private class HandlerClass implements ItemListener {
    public void itemStateChanged(ItemEvent event) {
        jTextField1.setFont(Color.BLUE);

    }

}`

JTextField does not have a setFont method that takes a color. I think you are looking for jTextField1.setForeground(Color.BLUE);

jTextField1.setFont(new Font("Serif", Font.PLAIN, 14));

Works great!

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