java课内趣味小实验--计算器calc
任何剽窃行为都是可耻的,你可以学习交流,但不好把别人的代码拿过来,说这是你自己写的。
愿祖国加速富强……
实验四 带事件的图形界面
一、实验目的
掌握编写图形界面的一般方法,包括文本输入框、按钮、标签、布局管理器、授权事件模型的使用。
二、实验环境
PC系列微机,Windows XP。
JDK150或更高版本。
三、实验内容
编写一个计算两个数相加的加法器,要求:
1、提供输入两个加数的文本输入框。
2、一个表示“+”的标签
3、一个“=”按钮
4、一个保存两数相加的和的文本框
当单击等号按钮时,能够计算出两数的和并显示到第三个文本框,同时能够判断两个加数是否是合法,即都是数字。
界面参考如下:
提示:
1、使用FlowLayout布局管理器
2、判断输入的是不是数字可以单独编写一个方法boolean isNumber(String
s),在输入框的FocusEvent事件的focusLost方法中调用isNumber。
在isNumber方法中应该循环取出s中的每一个字符,判断是否大于57或小于48(因为0的ascii码是48,9的ascii码是57),如果符合条件则可以说明不是数字,否则就是合法数字。
3、给等号按钮添加ActionListener事件,并实现actionPerformed方法,在该方法中取出前两个输入的值并转换成int或double型数据,相加后放入和的文本输入框中。
四、实验报告
写出合格的实验报告,至少包括实验目的、实验内容(程序代码)、出现的问题、学到的知识等。
code:
package javaExp4$;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CalcDemo extends Frame {
TextField a1,a2,ans;
Label lblE,lblInstruction,lblI2;
Button btnAdd,btnSub,btnMul,btnDiv;
Panel p;
CalcDemo(){
init();
}
private void init(){
this.setTitle("计算器[1.1.1](操作数必为整型)");
p=new Panel();//152,64,102苹果红;102,64,152亮紫
p.setLayout(new FlowLayout());
p.setBackground(Color.CYAN);
a1=new TextField(6);
a2=new TextField(6);
ans=new TextField(20);
lblE=new Label("=");
lblE.setBackground(new Color(152,64,102));
lblE.setForeground(Color.YELLOW);
lblInstruction=new Label("此计算器功能已初步完善,较上一版本改进了 主要功能缺失、结果框不应输入等问题");
lblI2=new Label(" [1.1.1]版较上一版本改进了背景色、前景色的设置");
//lblI2.setFont(Font.getFont("华文细黑"));
btnAdd=new Button("+");
btnAdd.setBackground(new Color(102,64,152));
btnAdd.setForeground(Color.YELLOW);
btnAdd.setFont(Font.getFont("华文细黑"));
btnSub=new Button("-");
btnSub.setBackground(new Color(102,64,152));
btnSub.setForeground(Color.YELLOW);
btnMul=new Button("*");
btnMul.setBackground(new Color(102,64,152));
btnMul.setForeground(Color.YELLOW);
btnDiv=new Button("/");
btnDiv.setBackground(new Color(102,64,152));
btnDiv.setForeground(Color.YELLOW);
ans.addFocusListener(new Control(this));
btnAdd.addActionListener(new Calc(this));
btnSub.addActionListener(new Calc(this));
btnMul.addActionListener(new Calc(this));
btnDiv.addActionListener(new Calc(this));
p.add(a1);
p.add(btnAdd);
p.add(btnSub);
p.add(btnMul);
p.add(btnDiv);
p.add(a2);
p.add(lblE);
p.add(ans);
p.add(lblInstruction);
p.add(lblI2);
this.add(p);
this.addWindowListener(new ShutDown());
this.setSize(800, 300);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CalcDemo cd1=new CalcDemo();
// FocusAdapter fa=new FocusAdapter();
// fa.focusGained(null);
}
}
class Calc implements ActionListener{
CalcDemo c;
String c1,c2;
Calc(CalcDemo c){
this.c=c;
}
public void actionPerformed(ActionEvent ae){
c1=c.a1.getText();
c2=c.a2.getText();
if(isNumber(c1)&&isNumber(c2)){
Integer i1=new Integer(Integer.parseInt(c1));
Integer i2=new Integer(Integer.parseInt(c2));
Button btnTemp=new Button();
btnTemp=(Button)ae.getSource();
if(btnTemp.getLabel().equals(c.btnAdd.getLabel()))//四则运算核心代码
c.ans.setText(new Integer(i1+i2).toString());
if(btnTemp.getLabel().equals(c.btnSub.getLabel()))
c.ans.setText(new Integer(i1-i2).toString());
if(btnTemp.getLabel().equals(c.btnMul.getLabel()))
c.ans.setText(new Integer(i1*i2).toString());
if(btnTemp.getLabel().equals(c.btnDiv.getLabel()))//注意除法要用实型
c.ans.setText(new Double(i1.doubleValue()/i2.doubleValue()).toString());
}
else c.ans.setText("输入有误,请注意标题栏");
}
static boolean isNumber(String c){
for(int i=0;i<c.length();i++){
if(c.charAt(i)<48||c.charAt(i)>57)return false;
}
return true;
}
}
class Control extends FocusAdapter{
CalcDemo c;
Control(CalcDemo c){
this.c=c;
}
public void focusGained(FocusEvent fe){
c.ans.setFocusable(false);
}
}
class ShutDown extends WindowAdapter{
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
PS:期待产品说明ing…………