/***********************************************************************
* 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("选项"),new jmenu("关于")};
jmenuitem[] items = {new jmenuitem("关于"),new jmenuitem("设置背景色") };
jbutton b1 = new jbutton("运算");
joptionpane b2 = new joptionpane("运算...");
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, "继续进行运算吗?", "确定",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, "作者:黑雪小组", "关于...",joptionpane.information_message);
}
};
actionlistener al3 = new actionlistener() {
public void actionperformed(actionevent e){
jcolorchooser.createdialog(null,"背景色...",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){
//设置背景色
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("计算器");
// to close the application:
//console.setupclosing(frame);
frame.getcontentpane().add(applet);
frame.setsize(260,140);
applet.init();
applet.start();
frame.setvisible(true);
}
} ///:~
闽公网安备 35060202000074号