package JCheckbox_GUI;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JCheckbox_gui extends JFrame implements ItemListener, ActionListener{
JTextField txtext;
JCheckBox jcbBolb, jcbItalic;
public JCheckbox_gui() {
// TODO Auto-generated constructor stub
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.add(txtext = new JTextField(20));
p2.add(jcbBolb = new JCheckBox("Bold"));
p2.add(jcbItalic = new JCheckBox("Italic"));
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
jcbBolb.addItemListener(this);
jcbItalic.addItemListener(this);
/***/
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(420, 100);
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void itemStateChanged(ItemEvent e) {
Font t = txtext.getFont();
JCheckBox cb = (JCheckBox) e.getItem();
if(e.getStateChange()==ItemEvent.SELECTED)
{
System.out.println("Selected");
}
else
{
System.out.println("not selected");
txtext.setFont(new Font(t.getName(), Font.PLAIN, t.getSize()));
}
if(jcbBolb.isSelected())
{
txtext.setFont(new Font(t.getName(), Font.BOLD, t.getSize()));
}
if(jcbItalic.isSelected())
{
txtext.setFont(new Font(t.getName(), Font.ITALIC, t.getSize()));
}
if(jcbItalic.isSelected()&&jcbBolb.isSelected())
{
System.out.println("Both");
txtext.setFont(new Font(t.getName(), Font.ITALIC+Font.BOLD, t.getSize()));
}
}
public static void main(String[] args) {
new JCheckbox_gui();
}
}
No comments: