//Purpose: This applet has buttons and checkboxes that // are monitored with event handling. import java.awt.*; import java.applet.*; import java.awt.event.*; import java.awt.event.ActionListener; public class test13b extends Applet implements ActionListener { Checkbox black = new Checkbox("Black", true); Checkbox red = new Checkbox("Red"); Checkbox green = new Checkbox("Green"); Checkbox blue = new Checkbox("Blue"); Button b1,b2,b3; String s = new String(" ");; //Set background to yellow, public void init() { Panel main_panel = new Panel(); Panel south_panel = new Panel(); setBackground(Color.yellow); setBackground(Color.yellow); setFont(new Font("Times Roman", Font.BOLD, 12)); setLayout( new BorderLayout()); add("North", new Label("Selection Box", Label.CENTER)); //Set up main panel to be added to center of applet BorderLayout main_panel.setLayout(new GridLayout(1,4)); main_panel.add(black); main_panel.add(red); main_panel.add(green); main_panel.add(blue); //Set up south panel to add to south of applet BorderLayout south_panel.setLayout(new GridLayout(1,3)); south_panel.add(b1 = new Button("Clear")); south_panel.add(b2 = new Button("Defaults")); south_panel.add(b3 = new Button("Submit")); add("Center", main_panel); add("South", south_panel); //register this applet as an ActionListener for each button b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); } //Paint the current key at the current position public void paint(Graphics g) { //set drawing color g.setColor(Color.red); } //Method to handle GUI events Only respond to the buttons. public void actionPerformed(ActionEvent e) { //test if a button // note to test others must do e.getSource() == TextField, etc. if (e.getSource() instanceof Button) { Button b = (Button) e.getSource(); if( b.getLabel().equals("Clear") ) //set all boxes to off { black.setState(false); red.setState(false); green.setState(false); blue.setState(false); } else if(b.getLabel().equals("Defaults") ) //set boxes to default { black.setState(true); red.setState(false); green.setState(false); blue.setState(false); } else if(b.getLabel().equals("Submit") ) //reprot boxes { s = new String("Black= " + black.getState() + "Red = " + red.getState() + "Green = " + green.getState() + "Blue = " + blue.getState()); System.out.println(s); } } } }