/* BRecord ======= Author Chris Pollett Course: CSCI 102 Clark University Date: Jan. 20 Purpose: Display three buttons in an applet record ======== the left right button presses into a cRecorder class. Then print out this list when the middle button is pressed How to Use: =========== javac BRecord.java cRecorder.java appletviewer BRecord Methods: ======== init() -- overrides Applet init() to initialise Buttons and TextArea as well as cRecorder class. actionPerformed(ActionEvent e) -- handles events generated by Buttons presses as described in Purpose. */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.Font; public class BRecord extends Applet implements ActionListener { private Font font; private Button left, right, pTextArea; private Label name; private cRecorder history; //used to store button presses private TextArea ta; private String l="left", r="right"; public void init() {//post: background colour set and GUI components //of BRecord initialised. cRecorder class history //instantiatiated setBackground(new Color(00,255,255)); //background is cyan font = new Font ("SansSerif",Font.BOLD,24); history = new cRecorder(); //instantiate history setLayout(new BorderLayout(5,5)); //use BorderLayout // //now we add the Button's and the TextArea // name= new Label("Button Recorder"); name.setFont(font); add(name, BorderLayout.NORTH); left = new Button("Left"); left.addActionListener(this); add(left, BorderLayout.WEST); right = new Button("Right"); right.addActionListener(this); add(right, BorderLayout.EAST); pTextArea = new Button("Print"); pTextArea.addActionListener(this); add(pTextArea, BorderLayout.CENTER); ta = new TextArea("Buttons Pressed",10,10, TextArea.SCROLLBARS_VERTICAL_ONLY); ta.setEditable( false); add(ta, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { //post: a left right button event is recorded into history // or a list of key presses is set in the TextArea // and the list cleared if(e.getSource() == left) //left button { history.add(l); } else if (e.getSource() == right) //right button { history.add(r); } else if(e.getSource() == pTextArea) //middle button { ta.setText("Buttons Pressed\n"+history.toString()); history.clear(); } } }