Applet version of number interpreter.java
Source code
/*
* AppInterp.java
*
* Created on February 28, 2007, 2:41 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author aharris
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class AppInterp extends JApplet
implements ActionListener{
/** applet-based version of interpeter */
//create buttons
JButton btnOne = new JButton("one");
JButton btnTwo = new JButton("two");
JButton btnThree = new JButton("three");
JButton btnFour = new JButton("four");
JLabel lblOutput = new JLabel("Press a button");
/** Creates a new instance of AppInterp */
public AppInterp() {
}
public void init(){
//set up main panel
Container pnlMain = this.getContentPane();
pnlMain.setLayout(new GridLayout(0,1));
//set up button panel
Panel pnlButtons = new Panel();
pnlButtons.setLayout(new GridLayout(0, 2));
pnlButtons.add(btnOne);
pnlButtons.add(btnTwo);
pnlButtons.add(btnThree);
pnlButtons.add(btnFour);
//place buttons and label
pnlMain.add(lblOutput);
pnlMain.add(pnlButtons);
//clean up label
lblOutput.setOpaque(true);
lblOutput.setBackground(Color.YELLOW);
lblOutput.setFont(new Font("SansSerif", Font.BOLD, 20));
lblOutput.setHorizontalAlignment(lblOutput.CENTER);
btnOne.addActionListener(this);
btnTwo.addActionListener(this);
btnThree.addActionListener(this);
btnFour.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String number = e.getActionCommand();
System.out.println(number);
if (number.equals("one")){
lblOutput.setText("uno");
} else if (number.equals("two")){
lblOutput.setText("dos");
} else if (number.equals("three")){
lblOutput.setText("tres");
} else if (number.equals("four")){
lblOutput.setText("quattro");
} else {
lblOutput.setText("something went wrong");
} // end if
} // end actionPerformed
}