任务一:简易计算器的设计
实训内容:模仿Windows自带的标准版计算器,设计并用Java语言实现简易的计算器(根据自己的能力,可以适当地增加或删除部分功能)。
最低要求:计算器运行界面如下图所示,包含二个文本框(分别显示算式和运算结果)、10个数字按钮(0~9)、4个运算按钮、一个等号按钮、一个清除按钮,要求将按键和结果显示在文本框中。
package Mycalculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener {
private JFrame frame;//组合原理
private ImageIcon icon;
private JTextField textField1;
private JTextField textField2;
private JButton[] button;
private JPanel panel1;
private JPanel panel2;
private JLabel label;
//第一个数
private String x = "";
//第二个数
private String y = "";
//运算符
private String fh = "";
//输出结果
private double answer;
//初始化
public void init(){
MyFrame();
MyIcon();
MyTestField();
MyButton();
MyLabel();
display();
}
private void display() {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void MyFrame() {
//设置frame的标题,坐标,大小。
frame =new JFrame("splendid‘s Calculator");
frame.setBounds(700,150,450,540);
frame.setResizable(false);
//绝对布局
frame.setLayout(null);
}
private void MyTestField() {
//设置文本框1 的大小位置,字体,颜色
textField1 =new JTextField();
textField1.setHorizontalAlignment(JTextField.LEFT);
textField1.setFont(new Font("黑体",Font.BOLD,35));
textField1.setBackground(new Color(195, 195, 232));
//设置文本框2 的大小位置,字体,颜色
textField2 =new JTextField();
textField2.setHorizontalAlignment(JTextField.RIGHT);
textField2.setFont(new Font("黑体",Font.BOLD,35));
textField2.setBackground(new Color(189, 189, 232));
//将文本框添加到面板上
panel1 = new JPanel();
panel1.add(textField1);
panel1.add(textField2);
panel1.setLayout(new GridLayout(2,1));
panel1.setBounds(20,15,400,60);
//将文本框放在容器上面
frame.add(panel1,BorderLayout.NORTH);
}
private void MyIcon() {
}
private void MyButton() {
// 按钮文本
String[] arr =
{
"7","8","9","*",
"4","5","6","/",
"1","2","3","-",
"0","CE","+","=", };
// 按钮
button = new JButton[arr.length];
panel2 =new JPanel();
//设置面板的布局方式
panel2.setBounds(20,90,400,350);
//表格布局
panel2.setLayout(new GridLayout(4,4,8,8));
for(int i =0;i<button.length;i++){
//创建按钮
button[i] =new JButton(arr[i]);
//设置按钮字体
button[i].setFont(new Font("黑体",Font.CENTER_BASELINE,20));
//设置按钮背景颜色
button[i].setBackground(new Color(242,240,235));
//添加监听事件
button[i].addActionListener( this);
panel2.add(button[i]);
}
frame.add(panel2,BorderLayout.SOUTH);
}
//计算器功能实现
public void calculate(String z) {
if (z.equals("+")) answer = Double.parseDouble(x) + Double.parseDouble(y);
else if (z.equals("-")) answer = Double.parseDouble(x) - Double.parseDouble(y);
else if (z.equals("*")) answer = Double.parseDouble(x) * Double.parseDouble(y);
else if (z.equals("/")) answer = Double.parseDouble(x) / Double.parseDouble(y);
else answer = Double.parseDouble(x);
//将答案显示
x = Double.toString(answer);
if(x.length()>6) textField2.setText(x.substring(0,10));
else textField2.setText(x);
y = "";
answer = 0;
fh = "";
}
@Override
public void actionPerformed(ActionEvent e) {
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.substring(1);
textField1.setText(x);
} else {
y = y + e.getActionCommand();
if (y.startsWith("00")) y.substring(1);
textField1.setText(x+fh+y);
}
}
//清空
if (e.getActionCommand().equals("CE")) {
x = "";
y = "";
fh = "";
textField1.setText("");
textField2.setText("");
}
if (e.getActionCommand().equals("+")) {
if (!fh.equals("")) calculate(fh);
fh = "+";
textField1.setText(x+fh);
}
if (e.getActionCommand().equals("-")) {
if (!fh.equals("")) calculate(fh);
fh = "-";
textField1.setText(x+fh);
}
if (e.getActionCommand().equals("*")) {
if (!fh.equals("")) calculate(fh);
fh = "*";
textField1.setText(x+fh);
}
if (e.getActionCommand().equals("/")) {
if (!fh.equals("")) calculate(fh);
fh = "/";
textField1.setText(x+fh);
}
if (e.getActionCommand().equals("=")) {
calculate(fh);
}
}
private void MyLabel() {
label = new JLabel();
label.setText("版权所有:splendid");
//设置标签的字体,大小及颜色
label.setFont(new Font("黑体",Font.CENTER_BASELINE,25));
label.setForeground(Color.RED);
label.setBounds(4, 470, 300, 40);
frame.add(label);
}
public static void main(String[] args) {
Calculator cal =new Calculator();
cal.init();
}
}