Version 2.0
功能实现:
1.基础计算机界面布局
2.实现简单的四则运算操作(加减乘除)和清除功能
更新日志:
1.重新布局计算器界面
2.增加平方(x^2),开方(sqrt),退格操作
运行效果:
Java实现计算器 Version 2.0
代码实现:
package pers.nico.windowtest;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CalculatorTest02 {
public static void main(String[] args) {
CalculatorTest02 test = new CalculatorTest02();
test.Calculator();
}
//创建面板对象
private JFrame frame;
private JPanel panelsouth;
//创建文本框对象
private JTextField text;
//设置按钮大小
private Dimension preferredsize;
//数字按钮0-9
JButton button0;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
//运算符符号
JButton button_point;//.
JButton button_equal;//=
JButton button_plus;//+
JButton button_minus;//-
JButton button_ride;//*
JButton button_divide;///
JButton button_sqrt;//sqrt
JButton button_square;//x^2
//退格和清零按钮
JButton button_back;//退格
JButton button_clear;//清除
public void Calculator()
{
frame = new JFrame();
// frame.setLayout(new GridLayout(2,1));//新建网格布局管理器,2行1列
text = new JTextField("0",40);//构造一个用指定文本和列初始化的新文本框text
text.setFont(new Font("黑体",Font.BOLD,20));
text.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式:居右对齐
preferredsize = new Dimension(80,50);//设置按钮大小
//实例化数字按钮0-9
button0 = new JButton("0");
button0.setPreferredSize(preferredsize);//设置按钮大小
// button0.setFont(new Font("黑体",Font.BOLD,18));
// button0.setBackground(Color.gray);
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
//实例化运算符符号
button_point = new JButton(".");
button_equal = new JButton("=");
// button_equal.setFont(new Font("黑体",Font.BOLD,25));
// button_equal.setBackground(Color.orange);
button_plus = new JButton("+");
button_minus = new JButton("-");
button_ride = new JButton("*");
button_divide = new JButton("/");
button_sqrt = new JButton("sqrt");
button_square = new JButton("x^2");
//实例化退格和清零按钮
button_back = new JButton("back");
button_clear = new JButton("C");
//南部面板,放置数字和运算符按钮
panelsouth = new JPanel();
//设置南部面板采用网格布局(4行*4列)
panelsouth.setLayout(new GridLayout(5,4));
//将文本框添加到面板
frame.add(text);
//将数字和符号添加到南部面板
//第1行
panelsouth.add(button_clear);
panelsouth.add(button_square);
panelsouth.add(button_sqrt);
panelsouth.add(button_divide);
//第2行
panelsouth.add(button7);
panelsouth.add(button8);
panelsouth.add(button9);
panelsouth.add(button_ride);
//第3行
panelsouth.add(button4);
panelsouth.add(button5);
panelsouth.add(button6);
panelsouth.add(button_plus);
//第4行
panelsouth.add(button1);
panelsouth.add(button2);
panelsouth.add(button3);
panelsouth.add(button_minus);
//第5行
panelsouth.add(button0);
panelsouth.add(button_point);
panelsouth.add(button_back);
panelsouth.add(button_equal);
//将南部面板添加到主面板
frame.add(panelsouth,BorderLayout.SOUTH);
//设置窗口名称
frame.setTitle("Nico的计算器 Version 0.1");
//设置窗口大小
frame.setSize(300, 400);
//设置窗口可见
frame.setVisible(true);
//设置窗口默认显示位置(null居中)
frame.setLocationRelativeTo(null);
//设置窗口默认的关闭按钮可用
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//按钮实现(方法调用)
method_input();
method_realize();
}
//按钮方法的实现
//数字&运算符实现(实现输入,未实现功能)
public void method_input()
{
//数字0的输入
button0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"0");//将文本内容后加上字符0
}
}
});
//数字1的输入
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("1");//将文本框内容置为 1
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"1");//将文本内容后加上字符1
}
}
});
//数字2的输入
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("2");//将文本框内容置为2
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"2"); //将文本内容后加上字符2
}
}
});
//数字3的输入
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("3");//将文本框内容置为3
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"3");//将文本内容后加上字符3
}
}
});
//数字4的输入
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("4");//将文本框内容置为4
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"4");//将文本内容后加上字符4
}
}
});
//数字5的输入
button5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("5");//将文本框内容置为5
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"5");//将文本内容后加上字符5
}
}
});
//数字6的输入
button6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("6");//将文本框内容置为6
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"6");//将文本内容后加上字符6
}
}
});
//数字7的输入
button7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("7");//将文本框内容置为7
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"7");//将文本内容后加上字符7
}
}
});
//数字8的输入
button8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("8");//将文本框内容置为8
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"8");//将文本内容后加上字符8
}
}
});
//数字9的输入
button9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("9");//将文本框内容置为9
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"9");//将文本内容后加上字符9
}
}
});
//点.的输入
button_point.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText(".");//将文本框内容置为.
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+".");//将文本内容后加上字符.
}
}
});
//加号+的输入
button_plus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("+");//将文本框内容置为+
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"+");//将文本内容后加上字符+
}
}
});
//减号-的输入
button_minus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("-");//将文本框内容置为-
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"-");//将文本内容后加上字符-
}
}
});
//乘号*的输入
button_ride.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("*");//将文本框内容置为*
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"*");//将文本内容后加上字符*
}
}
});
//除号/的输入
button_divide.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0)
{
if(text.getText().equals("0"))//将按钮值与0作比较
{
text.setText("");//将文本框初始化为空
text.setText("/");//将文本框内容置为/
text.requestFocus();//把光标放在文本框text里
}
else
{
String str = text.getText();//取得当前按钮的按钮值
text.setText(str+"/");//将文本内容后加上字符/
}
}
});
}
//运算符功能实现
public void method_realize()
{
//退格
button_back.addActionListener(new ActionListener(){//监听退格键
public void actionPerformed(ActionEvent arg0)
{
//处理退格键被按下的事件
String Text = text.getText();
int i = Text.length();
if(i>0)
{
Text = Text.substring(0,i-1);//去掉最后一个字符
if (Text.length() == 0)
{
// 如果文本没有了内容,则初始化计算器的各种值
text.setText("0");
}
else
{
//显示新的文本
text.setText(Text);
}
}
}
});
//清零
button_clear.addActionListener(new ActionListener(){//监听清零键
public void actionPerformed(ActionEvent e)
{
text.setText("0");//将文本框置为0(清零功能)
}
});
//平方功能
button_square.addActionListener(new ActionListener(){//监听root键
public void actionPerformed(ActionEvent e)
{
//root键被按事件
String i = text.getText();
Double j = Double.parseDouble(i);//将字符串i转换成对应的double类型的数值
double ans = j*j; //求平方
String answer =String.valueOf(ans);//将int型数据转换成String类型
text.setText(answer);//将文本框设置为平方后的结果
}
});
//开方(根号)
button_sqrt.addActionListener(new ActionListener(){//监听sqrt键
public void actionPerformed(ActionEvent e)
{
//sqrt键被按事件
String i = text.getText();
Double j = Double.parseDouble(i);//将字符串转换成对应的double类型的数值
double ans = (double)Math.sqrt(j);//求开方
String answer = String.valueOf(ans);//将double型数据转换成String类型
text.setText(answer);//将文本框设置为开方后的结果
}
});
//等号实现(加减乘除功能)
button_equal.addActionListener(new ActionListener(){ //监听 “等号” 按键
public void actionPerformed(ActionEvent arg0)
{
//处理“等号” 按键被按下事件
//加法运算
if(text.getText().indexOf("+")!= -1)
{
//将字符串分割为子字符串,然后将结果作为字符串数组返回
String[] s = text.getText().split("[+]");//转义字符,要用"[+]"或者"\+"
Double d1 = Double.parseDouble(s[0]);//返回一个指定字符串表示的double值
Double d2 = Double.parseDouble(s[1]);
double ans = d1 + d2;
String answer = String.valueOf(ans);//将结果转换为字符串
text.setText(answer);//将加法运算的结果以字符串形式在文本框中显示
}
//减法运算
else if(text.getText().indexOf("-")!= -1)
{
String[] s = text.getText().split("-");
text.setText("");
Double d1 = Double.parseDouble(s[0]);
Double d2 = Double.parseDouble(s[1]);
double ans = d1-d2;
String answer =String.valueOf(ans);
text.setText(answer);
}
//乘法运算
else if(text.getText().indexOf("*")!= -1)
{
String[] s = text.getText().split("[*]");//*是转义字符,要用"[*]",或者"\*"
text.setText("");
Double d1 = Double.parseDouble(s[0]);
Double d2 = Double.parseDouble(s[1]);
double ans = d1*d2;
String answer =String.valueOf(ans);
text.setText(answer);
}
//除法运算
else if(text.getText().indexOf("/")!= -1)
{
String[] s = text.getText().split("/");
text.setText("");
Double d1 = Double.parseDouble(s[0]);
Double d2 = Double.parseDouble(s[1]);
double ans = d1/d2;
String answer =String.valueOf(ans);
text.setText(answer);
}
else
{
text.setText("请选择要进行的运算");
}
}
});
}
}
编程小白,仅作学习记录📝