caculatordemo.java

来自「JAVA程序设计课程中各章节的程序实例。」· Java 代码 · 共 77 行

JAVA
77
字号
/**How to control the layout of the applet??
*A simple caculator program
*2004.11.27. xhcprince
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class caculatorDemo extends Applet implements ActionListener
{
	Label L1,L2,L3;
	TextField t1,t2,t3;
	Button add_,sub_,mul_,div_;

	public void init()
	{
		L1 = new Label("First number");
		add(L1);
		L2 = new Label("Second number");
		add(L2);

		t1 = new TextField(10);
		add(t1);
		t2 = new TextField(10);
		add(t2);

		add_ = new Button("+");
		add(add_);
		add_.addActionListener(this);

		sub_ = new Button("-");
		add(sub_);
		sub_.addActionListener(this);

		mul_ = new Button("*");
		add(mul_);
		mul_.addActionListener(this);

		div_ = new Button("/");
		add(div_);
		div_.addActionListener(this);

		t3 = new TextField(10);
		add(t3);
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == add_)
		{
			double sum = Integer.parseInt(t1.getText()) + Integer.parseInt(t2.getText());
			t3.setText(String.valueOf(sum));
		}

		if(e.getSource() == sub_)
		{
			double sub = Integer.parseInt(t1.getText()) - Integer.parseInt(t2.getText());
			t3.setText(String.valueOf(sub));
		}

		if(e.getSource() == mul_)
		{
			double mul = Integer.parseInt(t1.getText()) * Integer.parseInt(t2.getText());
			t3.setText(String.valueOf(mul));
		}

		if(e.getSource() == div_)
		{
			double div = Integer.parseInt(t1.getText()) / Integer.parseInt(t2.getText());
			t3.setText(String.valueOf(div));
		}
	}
}
/*
<applet code = "caculatorDemo.class" width = 300 height = 160>
</applet>
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?