// First example: -------------
/**
Here a simple example using the first element of swing
and actionListener. J maches javax.
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;//For J-Components
public class NumbersBoxes extends JApplet implements ActionListener
{
private final static long serialVersionUID = 42L;// For the compiler
//Declarations:
//---------------
JTextField text;
String Text;
// 2 Panels
JPanel ThisPanel = new JPanel();
JPanel TextPanel = new JPanel();
//4 bottons
JButton Fibona = new JButton("Fib");
JButton Factoria = new JButton("Fac");
JButton Somma = new JButton("Sum");
JButton Closa = new JButton("Quit");
//Initialisation:
//---------------
public void init()
{
// Once the frame is fired
this.setBackground(Color.white);
this.setLayout(new BorderLayout(0,0));
// Make the buttons listen
Fibona.addActionListener(this);
Factoria.addActionListener(this);
Somma.addActionListener(this);
Closa.addActionListener(this);
// Add the bottons in the panele ThisPanel
add(ThisPanel);
ThisPanel.add(Fibona);
ThisPanel.add(Factoria);
ThisPanel.add(Somma);
ThisPanel.add(Closa);
ThisPanel.add(TextPanel);
//Where to have the output ( textfield)
text = new JTextField("");
text.setForeground(Color.red);
text.setEditable(false);
TextPanel.add(text);
text.setText(" ");
}
//Performing:
//-----------
public void actionPerformed(ActionEvent evt )
{
JButton keyClicked = (JButton)(evt.getSource());
/* String ThisLabel = keyClicked.getLabel();
getLabel(). Deprecated. - Replaced by getText */
String ThisLabel = keyClicked.getText();
if (("Quit".equals(ThisLabel))){
System.exit(0);
}
else if (("Fib".equals(ThisLabel)) | ("Fac".equals(ThisLabel))
| ("Sum".equals(ThisLabel)))
{
text.setText(" ");// empty first
Text = ThisLabel;
text.setText(Text);
Object obj = evt.getSource();//Just for background colors
if (obj == Somma)
TextPanel.setBackground(Color.red);
if (obj == Factoria)
TextPanel.setBackground(Color.green);
}
}//End ActionPerformed
}
// Second example: -------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.util.*;
public class TheDays extends Applet implements ActionListener
{
//1. Declare variables:
//-----------------------
TextField TheResult ;
Color lightGray = new Color(200,200,200);
Color lightRed = new Color(248,200,200);
Font TheFont1 = new Font("Arial", Font.BOLD, 13);
Font TheFont2 = new Font("Courier New", Font.BOLD, 18);
Button [] TheKeys = {new Button ("0"), new Button ("1"),
new Button ("2"), new Button ("3"),
new Button ("4"),new Button ("5"),new Button ("6"),
new Button ("Today"),new Button ("OFF")};
//2. Initialisation:
//--------------------
public void init()
{
this.setBackground(lightRed );
this.setLayout(new BorderLayout());
setLayout(null);
setSize(600, 400);
setVisible(true);
TheResult = new TextField();
TheResult.setLocation(100, 100);
TheResult.setSize(400,35);
add (TheResult);
TheResult.setFont(TheFont2);
TheResult.setForeground(Color.red);
for(int i=0; i< 9 ; i++)
{
TheKeys[i].setBackground(lightGray);
TheKeys[i].setForeground(Color.blue);
TheKeys[i].setLocation(100 + 60*(i - 1), 200);
TheKeys[i].setSize(50, 30);
TheKeys[i].setFont(TheFont1);
add(TheKeys[i]);
TheKeys[i].addActionListener(this);
}
}
public void actionPerformed(ActionEvent evt)
{
Button keyClicked = (Button)(evt.getSource());
String name = keyClicked.getLabel();
runCalculations(name);
}
public void runCalculations (String name){
if(name =="0"){ TheResult.setText("Sunday");}
else if(name =="1"){ TheResult.setText("Monday");}
else if(name =="2"){ TheResult.setText("Tuesday");}
else if(name =="3"){ TheResult.setText("Wednesday");}
else if(name =="4"){ TheResult.setText("Thirsday");}
else if(name =="5"){ TheResult.setText("Friday");}
else if(name =="6"){ TheResult.setText("Saturday");}
else if(name =="Today"){ PerformThis (name);}
else {PerformThis();}
}
public void PerformThis(String TheString)
{
Date ThisDate = new Date();
TheResult.setText(TheString + " : " + ThisDate);
}
public void PerformThis()
{
System.exit(0);
}
}
|