AWTandSwing

Additemlistener handler addwindowlistener new

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ItemListener( handler ); 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 italic.addItemListener( handler ); addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); setSize( 275, 100 ); show(); } public static void main( String { new CheckBoxTest(); } Because CheckBoxHandler implements ItemListener, it must define method args ) itemStateChanged private class CheckBoxHandler implements ItemListener { getStateChange returns private int valBold = Font.PLAIN; ItemEvent.SELECTED or private int valItalic = Font.PLAIN; public void itemStateChanged( ItemEvent e ) { if ( e.getSource() == bold ) if ( e.getStateChange() == ItemEvent.SELECTED ) valBold = Font.BOLD; else valBold = Font.PLAIN; ItemEvent.DESELECTED JRadioButton Radio buttons Have two states: selected and deselected Normally appear as a group Only one radio button in group selected at time Selecting one button forces the other buttons off Mutually exclusive options ButtonGroup - maintains logical relationship between radio buttons Class JRadioButton Constructor JRadioButton( "Label", selected ) If selected true, JRadioButton initially selected 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // Fig. 12.12: RadioButtonTest.java // Creating radio buttons using ButtonGroup and JRadioButton. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RadioButtonTest extends JFrame { private JTextField t; private Font plainFont, boldFont, italicFont, boldItalicFont; private JRadioButton plain, bold, italic, boldItalic; private ButtonGroup radioGroup; public RadioButtonTest() { super( "RadioButton Test" ); Container c = getContentPane(); c.setLayout( new FlowLayout() ); t = new JTextField( "Watch the font c.add( t ); 1. import Initialize radio buttons. Only styleone is initially ); change", 25 selected. // Create radio buttons plain = new JRadioButton( "P...
View Full Document

Ask a homework question - tutors are online