任务一:简易计算器的设计
实训内容:模仿Windows自带的标准版计算器,设计并用Java语言实现简易的计算器(根据自己的能力,可以适当地增加或删除部分功能)。
最低要求:计算器运行界面如下图所示,包含二个文本框(分别显示算式和运算结果)、10个数字按钮(0~9)、4个运算按钮、一个等号按钮、一个清除按钮,要求将按键和结果显示在文本框中。
package 实训1;
import javax.swing.JFrame;
public class Calculatortest {
public static void main(String[] args) {
Calculator c=new Calculator();
c.setTitle("计算器"); //设置窗体标题
c.setBounds(700,150,450,540);//标题位置
c.setResizable(false);//生成的窗体大小是由程序员决定的,用户不可以自由改变窗体的大小
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体默认关闭事件
c.setSize(400,400); //设置窗体大小
c.setVisible(true); //显示窗体
}
}
package 实训1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener{
private JPanel Panel1=new JPanel(); //此面板上将放置两个文本框
private JPanel Panel2=new JPanel(); //此面板上放置按钮
private String x="",y=""; //x,y代表两个操作数
private String fh=""; //运算符
private double t; //计算结果
private JTextField t1=new JTextField(); //文本框对象
private JTextField t2=new JTextField(); //文本框对象
public Calculator(){
Panel1.setLayout(new GridLayout(2, 1)); //将面板1布局为两行一列
add(Panel1, BorderLayout.NORTH); //将面板放在窗体北部
Panel1.add(t1); //把文本框t1添加到面板1中
Panel1.add(t2);//把文本框t2添加到面板1中
t1.setHorizontalAlignment(JTextField.LEFT); //文本框1靠左水平对齐
t2.setHorizontalAlignment(JTextField.RIGHT); //文本框2靠右水平对齐
Panel2.setLayout(new GridLayout(4,4));//将面板设置成4*4的网格
t1.setBackground(Color.white); //给文本框设置背景色
t2.setBackground(Color.white);
//从左到右,从上到下将按钮添加到面板上
addButton("7",Color.BLACK);
addButton("8",Color.BLACK);
addButton("9",Color.BLACK);
addButton("/",Color.BLACK);
addButton("4",Color.BLACK);
addButton("5",Color.BLACK);
addButton("6",Color.BLACK);
addButton("*",Color.BLACK);
addButton("1",Color.BLACK);
addButton("2",Color.BLACK);
addButton("3",Color.BLACK);
addButton("-",Color.BLACK);
addButton("0",Color.BLACK);
addButton("CE",Color.BLACK);
addButton("+",Color.BLACK);
addButton("=",Color.BLACK);
add(Panel2,BorderLayout.CENTER);//将面板布局在窗体的中部
t1.setFont(new Font("黑体",30,30));//设置组件中字体
t2.setFont(new Font("黑体",30,30));
}
public void addButton(String string,Color color) {
JButton bt=new JButton(string);
bt.setBackground(Color.white);//将面板中每个按钮的背景色设置成白色
bt.setFont(new Font("黑体",20,20));
bt.addActionListener(this);
Panel2.add(bt);
}
//计算功能实现
public void result(String z) {
if(z.equals("+"))
t=Double.parseDouble(x)+Double.parseDouble(y);//将字符串转化成包装类
if(z.equals("-"))
t=Double.parseDouble(x)-Double.parseDouble(y);
if(z.equals("*"))
t=Double.parseDouble(x)*Double.parseDouble(y);
if(z.equals("/"))
t=Double.parseDouble(x)/Double.parseDouble(y);
x=Double.toString(t);
if(x.length()>6) {
t2.setText(x.substring(0,10)); //设定结果长度,并将结果显示在文本框t2中
}
else {
t2.setText(x);
}
y="";
fh="";
}
//实现接口ActionListener中的唯一方法
public void actionPerformed(ActionEvent e)throws IndexOutOfBoundsException{
//e.getActionCommand()得到的是组件对象上的字符串
if(e.getActionCommand().equals("0")||e.getActionCommand().equals("1")
||e.getActionCommand().equals("2")||e.getActionCommand().equals("3")
||e.getActionCommand().equals("4")||e.getActionCommand().equals("5")
||e.getActionCommand().equals("6")||e.getActionCommand().equals("7")
||e.getActionCommand().equals("8")||e.getActionCommand().equals("9")) {
if(fh.equals("")) {
x=x+e.getActionCommand();
if(x.startsWith("00")) {
x = x.substring(1);//判断首字符是否为00
}
t1.setText(x);
}else {
y=y+e.getActionCommand();
if(y.startsWith("00")) {
y=y.substring(1);
}
t1.setText(x+fh+y);
}
}
//一键清空数据
if(e.getActionCommand().equals("CE")) {
x="";
y="";
fh="";
t1.setText("0");
t1.setText("");
}
//含有小数点.的运算
if(e.getActionCommand().equals("+")) {
if(fh!="")
result(fh);
fh="+";
t1.setText(x+fh);
}
if(e.getActionCommand().equals("-")) {
if(fh!="")
result(fh);
fh="-";
t1.setText(x+fh);
}
if(e.getActionCommand().equals("*")) {
if(fh!="")
result(fh);
fh="*";
t1.setText(x+fh);
}
if(e.getActionCommand().equals("/")) {
if(fh!="")
result(fh);
fh="/";
t1.setText(x+fh);
}
if(e.getActionCommand().equals("=")) {
result(fh);
}
}
}