最近刚学到javagui编程,自己写了一个java计算器但是还差按钮的事件处理没有搞好!我试了很多次都还是不会弄!我的源代码是这样的,就还差按钮事件处理,求高手帮忙写一下!
/**
* @(#)Calculator.java
*
*
* @author
* @version 1.00 2012/4/17
*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class Calculatorframe extends Frame implements ActionListener
{
JTextField jtf;
MenuBar menubar;//菜单条
Menu m1,m2,m3;
MenuItem mi1,mi2,mi3,mi4,mi5,mi6;
//计算器中的按钮
Button MC,MR,MS,MJIA,MJIAN;
Button Back,CE,C,zheng_fu,sqrt;
Button qi,ba,jiu,chu,quyu;
Button si,wu,liu,cheng,yix;
Button yi,er,san,jian;
Button ling,point,jia;
Button deng;
StringBuffer str;//显示屏所显示的字符串
double x,y;//定义的两个计算操作数 //String s1,s2;//
final int slength=30;//设置结果显示的有效长度
int z;//开关语句表达式用于确定加减乘除运算
Calculatorframe()
{
super("简单计算器");
this.setLayout(null);
menubar=new MenuBar();
m1=new Menu("查看(V)");
m2=new Menu("编辑(E)");
m3=new Menu("帮助(H)");
mi1=new MenuItem("标准型");
mi2=new MenuItem("科学型");
mi3=new MenuItem("程序员");
mi4=new MenuItem("复制");
mi5=new MenuItem("粘贴");
mi6=new MenuItem("关于计算器");
mi6.addActionListener(this);
//计算结果的显示框
jtf=new JTextField(35);
jtf.setBounds(10,55,195,46);
jtf.setEditable(true);
//jtf.setText();
jtf.setHorizontalAlignment(JTextField.RIGHT);
//按钮位置的设定以及为按钮注册监听器
MC=new Button("MC");
MC.setBounds(10,110,35,30);
MC.addActionListener(this);
MR=new Button("MR");
MR.setBounds(50,110,35,30);
MR.addActionListener(this);
MS=new Button("MS");
MS.setBounds(90,110,35,30);
MS.addActionListener(this);
MJIA=new Button("M+");
MJIA.setBounds(130,110,35,30);
MJIA.addActionListener(this);
MJIAN=new Button("M-");
MJIAN.setBounds(170,110,35,30);
MJIAN.addActionListener(this);
Back=new Button("Back");
Back.setBounds(10,145,35,30);
Back.addActionListener(this);
CE=new Button("CE");
CE.setBounds(50,145,35,30);
CE.addActionListener(this);
C=new Button("C");
C.setBounds(90,145,35,30);
C.addActionListener(this);
zheng_fu=new Button("+/-");
zheng_fu.setBounds(130,145,35,30);
zheng_fu.addActionListener(this);
sqrt=new Button("sqrt");
sqrt.setBounds(170,145,35,30);
sqrt.addActionListener(this);
qi=new Button("7");
qi.setBounds(10,180,35,30);
qi.addActionListener(this);
ba=new Button("8");
ba.setBounds(50,180,35,30);
ba.addActionListener(this);
jiu=new Button("9");
jiu.setBounds(90,180,35,30);
jiu.addActionListener(this);
chu=new Button("/");
chu.setBounds(130,180,35,30);
chu.addActionListener(this);
quyu=new Button("%");
quyu.setBounds(170,180,35,30);
quyu.addActionListener(this);
si=new Button("4");
si.setBounds(10,215,35,30);
si.addActionListener(this);
wu=new Button("5");
wu.setBounds(50,215,35,30);
wu.addActionListener(this);
liu=new Button("6");
liu.setBounds(90,215,35,30);
liu.addActionListener(this);
cheng=new Button("*");
cheng.setBounds(130,215,35,30);
cheng.addActionListener(this);
yix=new Button("1/x");
yix.setBounds(170,215,35,30);
yix.addActionListener(this);
yi=new Button("1");
yi.setBounds(10,250,35,30);
yi.addActionListener(this);
er=new Button("2");
er.setBounds(50,250,35,30);
er.addActionListener(this);
san=new Button("3");
san.setBounds(90,250,35,30);
san.addActionListener(this);
jian=new Button("-");
jian.setBounds(130,250,35,30);
jian.addActionListener(this);
deng=new Button("=");
deng.setBounds(170,250,35,65);
deng.addActionListener(this);
ling=new Button("0");
ling.setBounds(10,285,75,30);
ling.addActionListener(this);
point=new Button(".");
point.setBounds(90,285,35,30);
point.addActionListener(this);
jia=new Button("+");
jia.setBounds(130,285,35,30);
jia.addActionListener(this);
//添加到窗口中
this.add(jtf);this.add(MC);
this.add(MS);this.add(MJIA);
this.add(MR);this.add(MJIAN);
this.add(Back);this.add(CE);
this.add(C);this.add(chu);
this.add(quyu);this.add(si);
this.add(zheng_fu);this.add(wu);
this.add(sqrt);this.add(liu);
this.add(qi);this.add(cheng);
this.add(ba);this.add(yix);
this.add(jiu);this.add(yi);
this.add(er);this.add(jia);
this.add(san);this.add(point);
this.add(jian);this.add(ling);
this.add(deng);
//添加菜单条到窗口
m1.add(mi1);m1.add(mi2);m1.add(mi3);
m2.add(mi4);m2.add(mi5);
m3.add(mi6);
menubar.add(m1);menubar.add(m2);menubar.add(m3);
this.setMenuBar(menubar);
//注册窗口关闭的监听器
this.addWindowListener(new mywindow());
//对窗口的设置
setBounds(500,200,214,325);
setResizable(false);
setVisible(true);
setBackground(Color.GRAY);
}
//窗口关闭的事件处理
class mywindow extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
//单击按键时的事件处理
public void actionPerformed(ActionEvent e)
/*Button MC,MR,MS,MJIA,MJIAN;
Button Back,CE,C,zheng_fu,sqrt;
Button qi,ba,jiu,chu,quyu;
Button si,wu,liu,cheng,yix;
Button yi,er,san,jian;
Button ling,point,jia;
Button deng;*/
{
if(e.getSource().equals(qi) || e.getSource().equals(ba) || e.getSource().equals(jiu) || e.getSource().equals(si) ||
e.getSource().equals(wu) || e.getSource().equals(liu) || e.getSource().equals(yi) || e.getSource().equals(er) ||
e.getSource().equals(san) ||e.getSource().equals(ling)){
if(jtf.getText()==null){
jtf.setText(""+e.getActionCommand());
}else{
jtf.setText(null);
jtf.setText(jtf.getText()+e.getActionCommand());
}
//s1=e.getActionCommand();
//s2=e.getActionCommand();
}
try{
if(e.getSource().equals(jia)){
x=Double.parseDouble(jtf.getText());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
z=0;
y=0d;
}
else if(e.getSource().equals(jian)){
x=Double.parseDouble(jtf.getText());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
z=1;
y=0d;
}
else if(e.getSource().equals(cheng)){
x=Double.parseDouble(jtf.getText());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
z=2;
y=0d;
}
else if(e.getSource().equals(chu)){
x=Double.parseDouble(jtf.getText());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
z=3;
y=0d;
}
else if(e.getSource().equals(deng)){
//y=Double.parseDouble(jtf.getText());
switch(z)
{
case 0 : jtf.setText(""+(x+y));break;
case 1 : jtf.setText(""+(x-y));break;
case 2 : jtf.setText(""+(x*y));break;
case 3 : jtf.setText(""+(x/y));break;
}
}
else if(e.getSource().equals(CE)){ //选择"CE"清零
jtf.setText("0"); //把显示屏清零
//清空字符串缓冲区以准备接收新的输入运算数
}
else if(e.getSource().equals(point)){
if(jtf.getText().indexOf('.')!=-1){//判断字符串中是否已经包含了小数点
}
else{
if(jtf.getText()=="o"){
jtf.setText("0"+e.getActionCommand());
}
else if(jtf.getText().trim().equals(""))//如果初时显示为空则不做任何操作
{
}
else
{
jtf.setText(str.append(e.getActionCommand()).toString());
}
}
y=0d;
}
else if(e.getSource().equals(qi) || e.getSource().equals(ba) || e.getSource().equals(jiu) || e.getSource().equals(si) ||
e.getSource().equals(wu) || e.getSource().equals(liu) || e.getSource().equals(yi) || e.getSource().equals(er) ||
e.getSource().equals(san) ||e.getSource().equals(ling))//其他的数字键
{
jtf.setText(str.append(e.getActionCommand()).toString());
y=Double.parseDouble(jtf.getText().trim());
}
}catch(NumberFormatException e1){
}
}
}
public class Calculator {
public static void main(String[]args)
{
new Calculatorframe();
}
}
/**
* @(#)Calculator.java
*
*
* @author
* @version 1.00 2012/4/17
*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class Calculatorframe extends Frame implements ActionListener
{
JTextField jtf;
MenuBar menubar;//菜单条
Menu m1,m2,m3;
MenuItem mi1,mi2,mi3,mi4,mi5,mi6;
//计算器中的按钮
Button MC,MR,MS,MJIA,MJIAN;
Button Back,CE,C,zheng_fu,sqrt;
Button qi,ba,jiu,chu,quyu;
Button si,wu,liu,cheng,yix;
Button yi,er,san,jian;
Button ling,point,jia;
Button deng;
StringBuffer str;//显示屏所显示的字符串
double x,y;//定义的两个计算操作数 //String s1,s2;//
final int slength=30;//设置结果显示的有效长度
int z;//开关语句表达式用于确定加减乘除运算
Calculatorframe()
{
super("简单计算器");
this.setLayout(null);
menubar=new MenuBar();
m1=new Menu("查看(V)");
m2=new Menu("编辑(E)");
m3=new Menu("帮助(H)");
mi1=new MenuItem("标准型");
mi2=new MenuItem("科学型");
mi3=new MenuItem("程序员");
mi4=new MenuItem("复制");
mi5=new MenuItem("粘贴");
mi6=new MenuItem("关于计算器");
mi6.addActionListener(this);
//计算结果的显示框
jtf=new JTextField(35);
jtf.setBounds(10,55,195,46);
jtf.setEditable(true);
//jtf.setText();
jtf.setHorizontalAlignment(JTextField.RIGHT);
//按钮位置的设定以及为按钮注册监听器
MC=new Button("MC");
MC.setBounds(10,110,35,30);
MC.addActionListener(this);
MR=new Button("MR");
MR.setBounds(50,110,35,30);
MR.addActionListener(this);
MS=new Button("MS");
MS.setBounds(90,110,35,30);
MS.addActionListener(this);
MJIA=new Button("M+");
MJIA.setBounds(130,110,35,30);
MJIA.addActionListener(this);
MJIAN=new Button("M-");
MJIAN.setBounds(170,110,35,30);
MJIAN.addActionListener(this);
Back=new Button("Back");
Back.setBounds(10,145,35,30);
Back.addActionListener(this);
CE=new Button("CE");
CE.setBounds(50,145,35,30);
CE.addActionListener(this);
C=new Button("C");
C.setBounds(90,145,35,30);
C.addActionListener(this);
zheng_fu=new Button("+/-");
zheng_fu.setBounds(130,145,35,30);
zheng_fu.addActionListener(this);
sqrt=new Button("sqrt");
sqrt.setBounds(170,145,35,30);
sqrt.addActionListener(this);
qi=new Button("7");
qi.setBounds(10,180,35,30);
qi.addActionListener(this);
ba=new Button("8");
ba.setBounds(50,180,35,30);
ba.addActionListener(this);
jiu=new Button("9");
jiu.setBounds(90,180,35,30);
jiu.addActionListener(this);
chu=new Button("/");
chu.setBounds(130,180,35,30);
chu.addActionListener(this);
quyu=new Button("%");
quyu.setBounds(170,180,35,30);
quyu.addActionListener(this);
si=new Button("4");
si.setBounds(10,215,35,30);
si.addActionListener(this);
wu=new Button("5");
wu.setBounds(50,215,35,30);
wu.addActionListener(this);
liu=new Button("6");
liu.setBounds(90,215,35,30);
liu.addActionListener(this);
cheng=new Button("*");
cheng.setBounds(130,215,35,30);
cheng.addActionListener(this);
yix=new Button("1/x");
yix.setBounds(170,215,35,30);
yix.addActionListener(this);
yi=new Button("1");
yi.setBounds(10,250,35,30);
yi.addActionListener(this);
er=new Button("2");
er.setBounds(50,250,35,30);
er.addActionListener(this);
san=new Button("3");
san.setBounds(90,250,35,30);
san.addActionListener(this);
jian=new Button("-");
jian.setBounds(130,250,35,30);
jian.addActionListener(this);
deng=new Button("=");
deng.setBounds(170,250,35,65);
deng.addActionListener(this);
ling=new Button("0");
ling.setBounds(10,285,75,30);
ling.addActionListener(this);
point=new Button(".");
point.setBounds(90,285,35,30);
point.addActionListener(this);
jia=new Button("+");
jia.setBounds(130,285,35,30);
jia.addActionListener(this);
//添加到窗口中
this.add(jtf);this.add(MC);
this.add(MS);this.add(MJIA);
this.add(MR);this.add(MJIAN);
this.add(Back);this.add(CE);
this.add(C);this.add(chu);
this.add(quyu);this.add(si);
this.add(zheng_fu);this.add(wu);
this.add(sqrt);this.add(liu);
this.add(qi);this.add(cheng);
this.add(ba);this.add(yix);
this.add(jiu);this.add(yi);
this.add(er);this.add(jia);
this.add(san);this.add(point);
this.add(jian);this.add(ling);
this.add(deng);
//添加菜单条到窗口
m1.add(mi1);m1.add(mi2);m1.add(mi3);
m2.add(mi4);m2.add(mi5);
m3.add(mi6);
menubar.add(m1);menubar.add(m2);menubar.add(m3);
this.setMenuBar(menubar);
//注册窗口关闭的监听器
this.addWindowListener(new mywindow());
//对窗口的设置
setBounds(500,200,214,325);
setResizable(false);
setVisible(true);
setBackground(Color.GRAY);
}
//窗口关闭的事件处理
class mywindow extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
//单击按键时的事件处理
public void actionPerformed(ActionEvent e)
/*Button MC,MR,MS,MJIA,MJIAN;
Button Back,CE,C,zheng_fu,sqrt;
Button qi,ba,jiu,chu,quyu;
Button si,wu,liu,cheng,yix;
Button yi,er,san,jian;
Button ling,point,jia;
Button deng;*/
{
if(e.getSource().equals(qi) || e.getSource().equals(ba) || e.getSource().equals(jiu) || e.getSource().equals(si) ||
e.getSource().equals(wu) || e.getSource().equals(liu) || e.getSource().equals(yi) || e.getSource().equals(er) ||
e.getSource().equals(san) ||e.getSource().equals(ling)){
if(jtf.getText()==null){
jtf.setText(""+e.getActionCommand());
}else{
jtf.setText(null);
jtf.setText(jtf.getText()+e.getActionCommand());
}
//s1=e.getActionCommand();
//s2=e.getActionCommand();
}
try{
if(e.getSource().equals(jia)){
x=Double.parseDouble(jtf.getText());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
z=0;
y=0d;
}
else if(e.getSource().equals(jian)){
x=Double.parseDouble(jtf.getText());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
z=1;
y=0d;
}
else if(e.getSource().equals(cheng)){
x=Double.parseDouble(jtf.getText());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
z=2;
y=0d;
}
else if(e.getSource().equals(chu)){
x=Double.parseDouble(jtf.getText());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
z=3;
y=0d;
}
else if(e.getSource().equals(deng)){
//y=Double.parseDouble(jtf.getText());
switch(z)
{
case 0 : jtf.setText(""+(x+y));break;
case 1 : jtf.setText(""+(x-y));break;
case 2 : jtf.setText(""+(x*y));break;
case 3 : jtf.setText(""+(x/y));break;
}
}
else if(e.getSource().equals(CE)){ //选择"CE"清零
jtf.setText("0"); //把显示屏清零
//清空字符串缓冲区以准备接收新的输入运算数
}
else if(e.getSource().equals(point)){
if(jtf.getText().indexOf('.')!=-1){//判断字符串中是否已经包含了小数点
}
else{
if(jtf.getText()=="o"){
jtf.setText("0"+e.getActionCommand());
}
else if(jtf.getText().trim().equals(""))//如果初时显示为空则不做任何操作
{
}
else
{
jtf.setText(str.append(e.getActionCommand()).toString());
}
}
y=0d;
}
else if(e.getSource().equals(qi) || e.getSource().equals(ba) || e.getSource().equals(jiu) || e.getSource().equals(si) ||
e.getSource().equals(wu) || e.getSource().equals(liu) || e.getSource().equals(yi) || e.getSource().equals(er) ||
e.getSource().equals(san) ||e.getSource().equals(ling))//其他的数字键
{
jtf.setText(str.append(e.getActionCommand()).toString());
y=Double.parseDouble(jtf.getText().trim());
}
}catch(NumberFormatException e1){
}
}
}
public class Calculator {
public static void main(String[]args)
{
new Calculatorframe();
}
}
解决方案 »
- 一个关于Object类的问题。。
- 有没有用JAVA做的好看点的界面呢?
- StringBuffer()的问题,请大家看看.
- 帮我解释个名词,谢谢了!一定给分!
- 笔试遇到的问题,请大家帮我一下
- 怎样使自己做的java类库可以向标准API类库那样被我的电脑上所有java程序所访问
- 为什么abort.setEnable(false); 是错误的,编译不过去??
- 在应用程序中,一般用什么来显示数据查询的结果(Java初学者)?
- 初学者问题:Applet中怎么创建自己的类
- 在JBuilder里怎么把application打包成.exe文件,让用户双击直接可运行?
- 关于Java代码在内存中变化的书
- java变量作用域和生命周期
然后在下面实现即可