Please write a Java program named LeftMiddleRightjava and Le
Please write a Java program, named “LeftMiddleRight.java” and “LeftMiddleRightPanel.java”. This program will generate a window where there are three buttons, named left, middle and right respectively. The window also has a title named “Left Middle Right” and display a message “Click a Button” firstly. Then if the left button is clicked, the window will display “Left”; If the middle button is clicked, the window will display “Middle”; If the right button is clicked, the window will display “Right”;
Solution
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LeftMiddleRight extends Applet implements
ActionListener{
String actionMessage=\"\";
public void init(){
//create Buttons
Button Button1 = new Button(\"Left\");
Button Button2 = new Button(\"Middle\");
Button Button3 = new Button(\"Right\");
//add Buttons
add(Button1);
add(Button2);
add(Button3);
//set action listeners for buttons
Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
}
public void paint(Graphics g){
g.drawString(\"Click a Button\",0,50);
g.drawString(actionMessage,10,80);
}
public void actionPerformed(ActionEvent ae){
/*
* Get the action command using
* String getActionCommand() method.
*/
String action = ae.getActionCommand();
if(action.equals(\"Left\"))
actionMessage = \"Left Button Pressed\";
else if(action.equals(\"Right\"))
actionMessage = \"Right Button Pressed\";
else if(action.equals(\"Middle\"))
actionMessage = \"Middle Button Pressed\";
repaint();
}
}
<html>
<title>\"Left Middle Right\"</title>
<applet code=\"LeftMiddleRight\" width=200 height=200>
</applet>
</html>

