在vc6、eclipse(java)中完成加法运算,对比难度。你会喜欢上java的!
(一)、用vc6完成:
1) 新建工程
2) 基于对话框的程序
3) 拖拽输入框1(加数1),符号1(“+”),输入框2(加数2),符号2(“=”),符号3(结果),按钮(“计算”)
4) 映射控件和变量之间的关系
5) 给按钮添加事件函数,填进下面的代码:
UpdateData();
int a = m_a;
int b = m_a;
int c = a + b;
char c1[128]="";
itoa(c,c1 ,10);
m_result = c1;
UpdateData(FALSE);
(二)、Java的加法器代码:
import java.awt.*;
import java.awt.event.*;
class Calculate extends Frame implements ActionListener
{
TextField t1=new TextField(5);//第一个操作数文本框
Label LOP=new Label("+");
TextField t2=new TextField(5);//第二个操作数文本框
TextField t3=new TextField(5);//结果文本框
Label L1=new Label("=");
Button btn=new Button("计算");
public Calculate()
{
setLayout(new FlowLayout());
add(t1);add(LOP);add(t2);
add(L1);add(t3);add(btn);
btn.addActionListener(this);//注册动作事件监听者为当前对象
addWindowListener(new WindowAdapter() {//关闭窗口事件
public void windowClosing(WindowEvent e) {
dispose();//释放窗口
System.exit(0);//退出程序
}
});
}
public void actionPerformed(ActionEvent e){
float x,y; //操作数变量
double result=0;//结果变量
String op;
try{ //异常捕获机制
x=Float.parseFloat(t1.getText());//将字符串数据转换成浮点型数据
y=Float.parseFloat(t2.getText());
result=x+y;
t4.setText(Double.toString(result));
}catch(Exception ee){t3.setText("数据错误");}//捕获异常,数据错误时,显示信息
}
public static void main(String args[]) {
Calculate mainFrame = new Calculate();
mainFrame.setSize(400, 400);
mainFrame.setTitle("两个数的加法计算程序");
mainFrame.setVisible(true);
}
}
引入事件处理机制后的编程基本方法如下:
①对java.awt中组件实现事件处理必须使用java.awt.event包,所以在程序开始应加入import java.awt.event.*语句。
②用如下语句设置事件监听者:
事件源.addXXListener(XXListener代表某种事件监听者)。
③ 事件监听者所对应的类实现事件所对应的接口XXListener,并重写接口中的全部方法。
这样就可以处理图形用户界面中的对应事件了。要删除事件监听者可以使用语句:
事件源.removeXXListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*摘自网络
*/
class AnonymousEvent extends JFrame{
JButton btn;
public AnonymousEvent()
{
super("Java事件监听机制");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn=new JButton("点击");
/*匿名内部类******************************/
btn.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Container c=getContentPane();
c.setBackground(Color.red);
}
}
);
/***************************************/
getContentPane().add(btn);
setBounds(200,200,300,160);
setVisible(true);
}
public static void main(String args[])
{
new AnonymousEvent();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*摘自网络
*/
class InnerClassEvent extends JFrame{
JButton btn;
public InnerClassEvent(){
super("Java事件监听机制");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn=new JButton("点击");
btn.addActionListener(new InnerClass());
getContentPane().add(btn);
setBounds(200,200,300,160);
setVisible(true);
}
/*内部类*********************************/
class InnerClass implements ActionListener{
public void actionPerformed (ActionEvent e){
Container c=getContentPane();
c.setBackground(Color.red);
}
}
/**************************************/
public static void main(String args[]){
new InnerClassEvent();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class exec implements MouseMotionListener , MouseListener , WindowListener
{
private JFrame f;
JTextField tf;
public static void main(String args[])
{
exec demo = new exec();
demo.go();
}
public void go()
{
f = new JFrame("三个监听器的实例");
JPanel container = new JPanel();
f.setContentPane(container);
container.setLayout(new BorderLayout());
tf = new JTextField(30);
container.add(new JLabel("单击并拖动鼠标") , BorderLayout.NORTH);
container.add(tf , BorderLayout.SOUTH);
f.addMouseListener(this);
f.addMouseMotionListener(this);
f.addWindowListener(this);
f.setSize(240 , 150);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e){ }
public void mouseEntered(MouseEvent e)
{
String s = "鼠标进来了";
tf.setText(s);
}
public void mouseExited(MouseEvent e)
{
String s = "鼠标退出了";
tf.setText(s);
}
public void mousePressed(MouseEvent e){ }
public void mouseReleased(MouseEvent e){ }
public void mouseMoved(MouseEvent e){ }
public void mouseDragged(MouseEvent e)
{
String s = "鼠标拖动 :X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}
public void windowActivated(WindowEvent e){ }
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
public void windowClosed(WindowEvent e){ }
public void windowDeactivated(WindowEvent e){ }
public void windowDeiconified(WindowEvent e){ }
public void windowIconified(WindowEvent e){ }
public void windowOpened(WindowEvent e){ }
}