The monitoring of the package event;
Import java.awt.*;
Import java.awt.event.*;
public class Frame2 extends Frame implements actionlistener{
Private Button button1;
Public Frame2 ()
{
Super ("personal Information");
This.setsize (250,200);//dimensions of the design component
This.setlocation (800,600);//location of the design component display
This.setbackground (Color.Black);//Design background color
This.setlayout (New FlowLayout ());//design container for flow layout, center
This.add (New Label ("name"));
This.add (New TextField ("Autumn Pond", 20));
This.add (New Label ("gender"));
This.add (New TextField ("male", 20));
This.add (New Label ("Age"));
This.add (New TextField ("20", 20));
Button1=new button ("OK");
This.add (button1);
Button1.addactionlistener (this);
This.addwindowlistener (New Winclose ());
This.setvisible (TRUE);
}
public void actionperformed (ActionEvent ev)
{
if (Ev.getsource () ==button1)
{
System.out.print ("Welcome");
}
}
public static void Main (String arg[])
{
New Frame2 ();
}
Class Winclose implements WindowListener
{
public void windowclosing (WindowEvent ev)
{
System.exit (0);
}
public void windowopened (WindowEvent ev) {}
public void windowactivated (WindowEvent ev) {}
public void windowdeactivated (WindowEvent ev) {}
public void Windowclose (WindowEvent ev) {}
public void windowiconified (WindowEvent ev) {}
public void windowdeiconified (WindowEvent ev) {}
@Override
public void windowclosed (WindowEvent arg0) {
TODO auto-generated method stubs
}
}
}
2. Calculator interface
Package calculator;
Import Java.awt.BorderLayout;
Import Java.awt.Container;
Import Java.awt.GridLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.KeyAdapter;
Import java.awt.event.KeyEvent;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JTextField;
public class Jisuanqi extends JFrame {
public static void Main (string[] args) {
Jisuanqi frame = new Jisuanqi ();
Frame.settitle ("calculator");
Frame.setsize (300,400);
Frame.setresizable (FALSE);
Frame.setdefaultcloseoperation (Exit_on_close);
Frame.init ();
Frame.setvisible (TRUE);
}
private void Init ()
{
TextField = new JTextField ();
Textfield.seteditable (FALSE);
Textfield.sethorizontalalignment (Jtextfield.right);
JPanel panel = new JPanel ();
Panel.setlayout (New GridLayout (4, 4));
Container Container = Getcontentpane ();
Container.add (TextField, Borderlayout.north);
Container.add (panel, borderlayout.center);
Panel.add (Usebutton (' 7 '));
Panel.add (Usebutton (' 8 '));
Panel.add (Usebutton (' 9 '));
Panel.add (Usebutton (' + '));
Panel.add (Usebutton (' 4 '));
Panel.add (Usebutton (' 5 '));
Panel.add (Usebutton (' 6 '));
Panel.add (Usebutton ('-'));
Panel.add (Usebutton (' 1 '));
Panel.add (Usebutton (' 2 '));
Panel.add (Usebutton (' 3 '));
Panel.add (Usebutton (' * '));
Panel.add (Usebutton (' 0 '));
Panel.add (Usebutton ('. '));
Panel.add (Usebutton (' = '));
Panel.add (Usebutton ('/'));
}
Public JButton Usebutton (final char key)
{
JButton button = new JButton (string.valueof (key));
Button.addactionlistener (New ActionListener ()
{
public void actionperformed (ActionEvent event)
{
JButton btn = (JButton) event.getsource ();
Char Key2 = Btn.gettext (). charAt (0);
Action (Key2);
}
});
Button.addkeylistener (New Keyadapter ()
{
public void keyreleased (KeyEvent event)
{
Char Key2 = Event.getkeychar ();
Action (Key2);
}
});
return button;
}
private void Action (char key2)
{
if (REOP)
{
Textfield.settext ("");
Reop = false;
}
Switch (KEY2)
{
Case ' + ':
A1 = Double.parsedouble (Textfield.gettext ());
Textfield.settext ("");
operator = ' + ';
Break
Case '-':
A1 = Double.parsedouble (Textfield.gettext ());
Textfield.settext ("");
operator = '-';
Break
Case ' * ':
A1= double.parsedouble (Textfield.gettext ());
Textfield.settext ("");
operator = ' * ';
Break
Case '/':
A1= double.parsedouble (Textfield.gettext ());
Textfield.settext ("");
operator = '/';
Break
Case ' = ':
Reop = true;
A2 = double.parsedouble (Textfield.gettext ());
Switch (operator)
{
Case ' + ':
result = A1 + A2;
Break
Case '-':
result= a1-a2;
Break
Case ' * ':
result = A1 * A2;
Break
Case '/':
result = A1/A2;
Break
Default:;
}
Textfield.settext (string.valueof (result));
Break
Case ' 1 ':
Case ' 2 ':
Case ' 3 ':
Case ' 4 ':
Case ' 5 ':
Case ' 6 ':
Case ' 7 ':
Case ' 8 ':
Case ' 9 ':
Case ' 0 ':
Case '. ':
String text = Textfield.gettext () + Key2;
Textfield.settext (text);
Break
Default:;
}
}
Private JTextField TextField;
Private double A1, a2, result;
private char operator;
Private Boolean reop = false;
}
Java Program (event Listener and computer interface)