import java.awt.*;
import java.awt.event.*;class Computer implements ActionListener
{
Frame frame = new Frame("计算器 <羊子 工作室>");
TextField tf = new TextField("0");
int state =0;
public Computer()
{
Panel panel = new Panel(); //也是一个容器,但不能单独存在,最终要加到frame
panel.setLayout(new GridLayout(4,5));
String label[] = {"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
for(int i=0;i<label.length;i++)
{
Button b = new Button(label[i]);
if(label[i].equals("/")||label[i].equals("+")||label[i].equals("-")||label[i].equals("*")||label[i].equals("="))
{
b.setForeground(Color.red);
}
else
{
b.setForeground(Color.blue);
}
b.addActionListener(this);
panel.add(b);
}
frame.setLocation(400,150);
frame.add(tf,"North");
frame.add(panel,"Center"); frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.setSize(400,250);
frame.setResizable(false);
frame.setVisible(true);
}
int a = 0;
int b = 0;
int c = 0;
String o = "";public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand(); //所按按钮对应的标签值
String data_tf = tf.getText(); //文本框里的数字 if(action.equals("0")||action.equals("1")||action.equals("2")||action.equals("3")||action.equals("4")||action.equals("5")
||action.equals("6")||action.equals("7")||action.equals("8")||action.equals("9") )
{
if(state==0)
{
tf.setText(action);
state = 1;
}
else if(state==1)
{
tf.setText(tf.getText()+action);
state = 2;
}
else if(state==2)
{
tf.setText(tf.getText()+action);
state = 2;
}
else if(state==3)
{
tf.setText(action);
state = 4;
}
else if(state==4)
{
tf.setText(tf.getText()+action);
state = 5;
}
else if(state==5)
{
tf.setText(tf.getText()+action);
state = 5;
}
else if(state==6)
{
tf.setText(action);
state = 4;
}
else
{
tf.setText(action);
a=0;b=0;c=0;o="";
state = 1;
}
}
else if(action.equals("+")||action.equals("-")||action.equals("*")||action.equals("/"))
{
if(state==0)
{
o=action;
a=Integer.parseInt(tf.getText());
state = 3;
}
else if(state==1)
{
o=action;
a=Integer.parseInt(tf.getText());
state = 3;
}
else if(state==2)
{
o=action;
a=Integer.parseInt(tf.getText());
state = 3;
}
else if(state==3)
{
o=action;
a=Integer.parseInt(tf.getText());
state = 3;
}
else if(state==4)
{
b=Integer.parseInt(tf.getText());
if(o.equals("+"))
{
tf.setText(a+b+"");
}
if(o.equals("-"))
{
tf.setText(a-b+"");
}
if(o.equals("*"))
{
tf.setText(a*b+"");
}
if(o.equals("/"))
{
tf.setText(a/b+"");
}
a=Integer.parseInt(tf.getText());
o=action;
state = 6;
}
else if(state==5)
{
b=Integer.parseInt(tf.getText());
if(o.equals("+"))
{
tf.setText(a+b+"");
}
if(o.equals("-"))
{
tf.setText(a-b+"");
}
if(o.equals("*"))
{
tf.setText(a*b+"");
}
if(o.equals("/"))
{
tf.setText(a/b+"");
}
a=Integer.parseInt(tf.getText());
o=action;
state = 6;
}
else if(state==6)
{
o=action;
state = 6;
}
else
{
o=action;
a=Integer.parseInt(tf.getText());
state = 3;
}
}
else if(action.equals("="))
{
if(state==0)
{
state = 0;
}
else if(state==1)
{
state = 1;
}
else if(state==2)
{
state = 2;
}
else if(state==3)
{
state = 3;
}
else if(state==4)
{
b=Integer.parseInt(tf.getText());
if(o.equals("+"))
{
tf.setText(a+b+"");
}
if(o.equals("-"))
{
tf.setText(a-b+"");
}
if(o.equals("*"))
{
tf.setText(a*b+"");
}
if(o.equals("/"))
{
tf.setText(a/b+"");
}
state = 7;
}
else if(state==5)
{
b=Integer.parseInt(tf.getText());
if(o.equals("+"))
{
tf.setText(a+b+"");
}
if(o.equals("-"))
{
tf.setText(a-b+"");
}
if(o.equals("*"))
{
tf.setText(a*b+"");
}
if(o.equals("/"))
{
tf.setText(a/b+"");
}
state = 7;
}
else if(state==6)
{
state = 7;
}
else
{
c=Integer.parseInt(tf.getText());
if(o.equals("+"))
{
tf.setText(c+b+"");
}
if(o.equals("-"))
{
tf.setText(c-b+"");
}
if(o.equals("*"))
{
tf.setText(c*b+"");
}
if(o.equals("/"))
{
tf.setText(c/b+"");
}
state = 7;
}
}
else
{
tf.setText("0");
a=0;b=0;c=0;o="";
state=0;
}}
public static void main(String[] args)
{
new Computer();
}
}
上面是我在网上找的计算器的代码.布局我是知道的,就是事件处理确实看不明白.才学JAVA.请大家帮我注释哈事件处理哪儿.帮助我理解一下.注释的越好,分越多,我心理越感谢.
当然,
顶者有分...
顶者有分...
顶者有分...
顶者有分...
不要让贴子沉了.....
import java.awt.event.*;class Computer implements ActionListener
{
Frame frame = new Frame("计算器 <羊子 工作室>");
TextField tf = new TextField("0");
int state =0;
public Computer()
{
Panel panel = new Panel(); //也是一个容器,但不能单独存在,最终要加到frame
panel.setLayout(new GridLayout(4,5));
String label[] = {"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
for(int i=0;i<label.length;i++)
{
Button b = new Button(label[i]);
if(label[i].equals("/")||label[i].equals("+")||label[i].equals("-")||label[i].equals("*")||label[i].equals("="))
{
b.setForeground(Color.red);
}
else
{
b.setForeground(Color.blue);
}
b.addActionListener(this);
panel.add(b);
}
frame.setLocation(400,150);
frame.add(tf,"North");
frame.add(panel,"Center"); frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.setSize(400,250);
frame.setResizable(false);
frame.setVisible(true);
}
int a = 0;
int b = 0;
int c = 0;
String o = "";public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand(); //所按按钮对应的标签值
String data_tf = tf.getText(); //文本框里的数字 if(action.equals("0")||action.equals("1")||action.equals("2")||action.equals("3")||action.equals("4")||action.equals("5")
||action.equals("6")||action.equals("7")||action.equals("8")||action.equals("9") )
{
if(state==0)
{
tf.setText(action);
state = 1;
}
else if(state==1)
{
tf.setText(tf.getText()+action);
state = 2;
}
else if(state==2)
{
tf.setText(tf.getText()+action);
state = 2;
}
else if(state==3)
{
tf.setText(action);
state = 4;
}
else if(state==4)
{
tf.setText(tf.getText()+action);
state = 5;
}
else if(state==5)
{
tf.setText(tf.getText()+action);
state = 5;
}
else if(state==6)
{
tf.setText(action);
state = 4;
}
else
{
tf.setText(action);
a=0;b=0;c=0;o="";
state = 1;
}
}
else if(action.equals("+")||action.equals("-")||action.equals("*")||action.equals("/"))
{
if(state==0)
{
o=action;
a=Integer.parseInt(tf.getText());
state = 3;
}
else if(state==1)
{
o=action;
a=Integer.parseInt(tf.getText());
state = 3;
}
else if(state==2)
{
o=action;
a=Integer.parseInt(tf.getText());
state = 3;
}
else if(state==3)
{
o=action;
a=Integer.parseInt(tf.getText());
state = 3;
}
else if(state==4)
{
b=Integer.parseInt(tf.getText());
if(o.equals("+"))
{
tf.setText(a+b+"");
}
if(o.equals("-"))
{
tf.setText(a-b+"");
}
if(o.equals("*"))
{
tf.setText(a*b+"");
}
if(o.equals("/"))
{
tf.setText(a/b+"");
}
a=Integer.parseInt(tf.getText());
o=action;
state = 6;
}
else if(state==5)
{
b=Integer.parseInt(tf.getText());
if(o.equals("+"))
{
tf.setText(a+b+"");
}
if(o.equals("-"))
{
tf.setText(a-b+"");
}
if(o.equals("*"))
{
tf.setText(a*b+"");
}
if(o.equals("/"))
{
tf.setText(a/b+"");
}
a=Integer.parseInt(tf.getText());
o=action;
state = 6;
}
else if(state==6)
{
o=action;
state = 6;
}
else
{
o=action;
a=Integer.parseInt(tf.getText());
state = 3;
}
}
else if(action.equals("="))
{
if(state==0)
{
state = 0;
}
else if(state==1)
{
state = 1;
}
else if(state==2)
{
state = 2;
}
else if(state==3)
{
state = 3;
}
else if(state==4)
{
b=Integer.parseInt(tf.getText());
if(o.equals("+"))
{
tf.setText(a+b+"");
}
if(o.equals("-"))
{
tf.setText(a-b+"");
}
if(o.equals("*"))
{
tf.setText(a*b+"");
}
if(o.equals("/"))
{
tf.setText(a/b+"");
}
state = 7;
}
else if(state==5)
{
b=Integer.parseInt(tf.getText());
if(o.equals("+"))
{
tf.setText(a+b+"");
}
if(o.equals("-"))
{
tf.setText(a-b+"");
}
if(o.equals("*"))
{
tf.setText(a*b+"");
}
if(o.equals("/"))
{
tf.setText(a/b+"");
}
state = 7;
}
else if(state==6)
{
state = 7;
}
else
{
c=Integer.parseInt(tf.getText());
if(o.equals("+"))
{
tf.setText(c+b+"");
}
if(o.equals("-"))
{
tf.setText(c-b+"");
}
if(o.equals("*"))
{
tf.setText(c*b+"");
}
if(o.equals("/"))
{
tf.setText(c/b+"");
}
state = 7;
}
}
else
{
tf.setText("0");
a=0;b=0;c=0;o="";
state=0;
}}
public static void main(String[] args)
{
new Computer();
}
}
上面是我在网上找的计算器的代码.布局我是知道的,就是事件处理确实看不明白.才学JAVA.请大家帮我注释哈事件处理哪儿.帮助我理解一下.注释的越好,分越多,我心理越感谢.
当然,
顶者有分...
顶者有分...
顶者有分...
顶者有分...
不要让贴子沉了.....
还没CE
后退也没有
记忆也没有
还XX工作室
................................
好土
必须要实现public void actionPerformed(ActionEvent e);
在actionPerformed里面
String action = e.getActionCommand(); //所按按钮对应的标签值
通过这个action来判断是哪个按键触发的事件。
然后下面就是些具体的逻辑了。
不过这个计算器做的太一般了,很多逻辑没考虑进去。
既然楼主说了顶者有分,那我就顶吧
import java.awt.event.*;class Computer implements ActionListener
{
Frame frame = new Frame("计算器 <羊子 工作室>");
TextField tf = new TextField("0"); //作为计算器的计算栏 ,默认值为0
int state =0;
Computer()
{
//做计算器的按钮面板
Panel panel = new Panel(); //也是一个容器,但不能单独存在,最终要加到frame
panel.setLayout(new GridLayout(4,5)); //这行是布局
//添加计算器面板上的按钮。先定义一个数组,然后根据这个数组的每个值初始化多个button,并且根据值,决定Button的前景色即字的颜色
String label[] = {"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
for(int i=0;i<label.length;i++)
{
Button b = new Button(label[i]);
if(label[i].equals("/")||label[i].equals("+")||label[i].equals("-")||label[i].equals("*")||label[i].equals("="))
{
b.setForeground(Color.red);
}
else
{
b.setForeground(Color.blue);
}
/**
* 下面这行是为每个按钮添加监听,这样这个按钮发生操作,比如按下操作的时候,程序就会自动调用监听方法,
* 具体监听的方法是下面的 “actionPerformed” 。至于为什么会自动调用这个方法呢,因为这个类
* ”Computer implements ActionListener“ 它继承了ActionListener接口,所以按照约定它必须实现ActionListener
*方法,当你给某个组件(比如Button)使用了addActionListener(this)后,你一点击了这个Button,程序就会自动根据约定来
*执行你写的ActionListener这个方法.这就叫做接口回调.
*另外一个值得注意的问题是,这个Computer类它作为一个"主类"直接继承ActionListener,所以你可以直接写
*addActionListener(this) ,而这个this,就是指Computer 这个类.其实一般情况下,都使用内部类来实现的比如:
*
*button.addActionListener(new ActionListener(){
* public void actionPerformed(ActionEvent e) {
* if (e.getActionCommand().equals("0")){
* System.out.println("你按的是0");
* }
* } }) ;
*
*上面的代码意思是说当button,被按下的时候的时候,就"触发"一个事件,这个事件是由内部类的actionPerformed方法来执行的
*这两种监听方式分别是事件监听和组件监听.
*具体的几句话说不清:具体涉及的名词有 ---事件处理 , 组件监听 ,事件监听 ,接口回调 ,内部类等
*你配合java关键字搜索这些名词可以再进一步了解一下
*/ b.addActionListener(this);
panel.add(b); //这步把button添加到按钮面板中
}
//设定整个程序也就是个frame大小,
frame.setLocation(400,150);
//依次把计算栏,和按钮面板添加,后面的“North”“Center”表示布局位置
frame.add(tf,"North");
frame.add(panel,"Center");
/**这里就是用内部类进行组件监听,也就是只为frame添加一个监听事件windowClosing,也就是当
*关闭frame的时候,系统退出.
*/
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.setSize(400,250); //设置frame具体大小
frame.setResizable(false); //不能调整大小
frame.setVisible(true); //可见性设成true
}
/**
* 下面的这段用来进行计算逻辑的,但是我运行了一下发现这个计算器算的不对,应该是这儿的逻辑算错了,所以我也没兴趣分析
* 这个算法了,总体思想来讲:是每当按下按钮,要判断按下的是什么键,如果数字键就把它继续显示到TextField计算栏中,
* 如果是计算符,就先计算前一个式子的结果,然后把新的运算符存起来.等待下次运算.
*/
int a = 0;
int b = 0;
int c = 0;
String o = "";
public void actionPerformed(ActionEvent e)
{
/**
* 这里要注意一下,上面在初始化的时候没有用button.setActionCommand(String command) 设置此按钮激发的操作事件
* 当你调用getActionCommand()就返回的和button的lable的内容一致,实际上这样不正确,因为如果你把数字“1”改成在面板上
* 显示“一”那么在这里取回的将是一。最好的方式是:
* 先在类的最上面定义常量 private static final String NUM_1 = "1"
* 然后在定义代表数字1的butoon是 butoon.setActionCommand(NUM_1)
* 最后在取值得时候 :String action = e.getActionCommand();
* if (action.equals(NUM_1)) {...}
*/
String action = e.getActionCommand(); //所按按钮对应的标签值
String data_tf = tf.getText(); //文本框里的数字 这东西没用过/
//具体分析过程自己在写吧 } public static void main(String[] args)
{
/**
* new 一个Computer实例,由于已经在构造函数Computer()把东西都写好了,所以界面会自动出来.
* 不推荐这样写,这样构造函数太复杂.建议再加一个show()之类的方法,利用这和方法把界面显示出来
*/
new Computer();
}
}