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 itemStateC开发者_StackOverflowhanged(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!
精彩评论