import java.awt *;. import java.awt.event *;. import javax.swing *;. class Gui {JCheckBox right1 = new JCheckBox ( "1"); JCheckBox right2 = new JCheckBox ( "2"); JCheckBox right4 = new JCheckBox ( "4"); JCheckBox right8 = new JCheckBox ( "8"); StringBuffer choices; JFrame frame = new JFrame ( "CheckBoxDemo"); JLabel displayLabel; Gui () {right1.setMnemonic (KeyEvent.VK_4); right1.setSelected (true); right2.setMnemonic (KeyEvent.VK_3); right2.setSelected (true); right4.setMnemonic (KeyEvent.VK_2); right4.setSelected (true); right8.setMnemonic (KeyEvent.VK_1); right8. SetSelected (true); checkboxlistener mylistener = new checkboxlistener ();
right1.addItemListener (myListener); right2.addItemListener (myListener); right4.addItemListener (myListener); right8.addItemListener (myListener); choices = new StringBuffer ( "0000"); displayLabel = new JLabel (choices.toString ()); JPanel CheckPanel = new jPanel (); checkpanel.setLayout (new gridLayout (0,1)); checkpanel.add (right1); checkpanel.add (right2); checkpanel.add (right8); frame .getContentPane () setLayout (new BorderLayout ());.. frame.getContentPane () add (checkPanel, BorderLayout.WEST); frame.getContentPane () add (displayLabel, BorderLayout.CENTER);. frame.setSize (500,500); Frame.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent E) {system.exit (0);}}); // frame.pack (); This method is not used here. Frame.setVisible (TRUE); Class CheckBoxListener Implements ItemListener {Public Void ItemStateChanged (ItemEvent E) {INDEX = 0; CHAR C = '-'; Object Source = E.GETITEMSELECTABLE (); if (source == right8) {index = 0; c = '8';} else} (source == Right4) { Index = 1; c = '4';} else if (source == right2) {index = 2; c = '2';} else} (source == Right1) {index = 3; c = '1'; } IF (E.GetStateChange () == itemEvent.deselected) c = '-'; choices.setcharat (index, c); displaylabel.settext (choices.tostring ());