Implementation of the Java window---calculator (add)

Source: Internet
Author: User
/***********************************************************************
* Module:Caculater.java
* Author:administrator
* Created:2004-12-7 23:48:21
* Purpose:
***********************************************************************/
Package org.heixue.test.applet;

/**
* @author Administrator
*/
Import javax.swing.*;
Import java.awt.event.*;
Import java.awt.*;
public class Calculater extends JApplet {
jmenu[] Menus = {new JMenu ("Options"), New JMenu ("About")};
jmenuitem[] items = {New JMenuItem ("about"), new JMenuItem ("Set Background Color")};
JButton B1 = new JButton ("Operation");
Joptionpane b2 = new Joptionpane ("Operation ...");
JTextField txt1 = new JTextField ("100", 10);
JTextField txt2 = new JTextField ("200", 10);
JTextField txt3 = new JTextField (20);
jlabel[] Label = {New JLabel ("+"), new JLabel ("=")};
JColorChooser JCC = new JColorChooser ();

ActionListener al = new ActionListener () {
public void actionperformed (ActionEvent e) {
String name = ((JButton) E.getsource ()). GetText ();
int m = Joptionpane.showconfirmdialog (null, "Continue operation?", "OK", joptionpane.yes_no_option);
if (m==0) {
String OP1 = Txt1.gettext ();
String OP2 = Txt2.gettext ();
Txt3.settext (bussiness (OP1,OP2));
Txt3.settext (m+ "");
}
}
};
ActionListener Al2 = new ActionListener () {
public void actionperformed (ActionEvent e) {
Joptionpane.showmessagedialog (NULL, "Author: Black Snow Group", "About ...", joptionpane.information_message);
}
};
ActionListener Al3 = new ActionListener () {
public void actionperformed (ActionEvent e) {

Jcolorchooser.createdialog (NULL, "Background color ...", true,jcc,al4,null). Show ();
}
};
ActionListener Al4 = new ActionListener () {
public void actionperformed (ActionEvent e) {
Changeface (0,jcc.getcolor ());
}
};
public void init () {
Initialize layout.
Container CP = Getcontentpane ();
Cp.setlayout (New FlowLayout ());
Set menu bar.
Menus[0].add (Items[1]);
Items[1].addactionlistener (AL3);
Menus[1].add (Items[0]);
Items[0].addactionlistener (AL2);
JMenuBar MB = new JMenuBar ();
for (int i = 0; i < menus.length; i++)
Mb.add (Menus[i]);
Setjmenubar (MB);

Add 3 JTextField.
Cp.add (TXT1);
Cp.add (Label[0]);
Cp.add (TXT2);
Cp.add (label[1]);
Cp.add (TXT3);

Add 1 button with Joptionpane
Cp.add (B1);
B1.addactionlistener (AL);
}
void changeface (int i,object o) {
if (i==0) {
Set Background color
This.getcontentpane (). SetBackground ((Color) o);
SetBackground ((Color) o);
}
}
String bussiness (String op1,string op2) {
int result=0;
int op11=0;
int op21=0;
try{
Op11=integer.parseint (OP1);
}catch (Exception E1) {

}
try{
Op21=integer.parseint (OP2);
}catch (Exception E1) {

}
RESULT=OP11+OP21;
Return "" +result;
}
public static void Main (string[] args) {
JApplet applet = new Calculater ();
JFrame frame = new JFrame ("Calculator");
To close the application:
Console.setupclosing (frame);
Frame.getcontentpane (). Add (applet);
Frame.setsize (260,140);
Applet.init ();
Applet.start ();
Frame.setvisible (TRUE);
}
} ///:~




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.