首页 / JAVA / Java简单计算器的实现(+-*/)
Java简单计算器的实现(+-*/)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java简单计算器的实现(+-*/),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5616字,纯文字阅读大概需要9分钟。
内容图文
![Java简单计算器的实现(+-*/)](/upload/InfoBanner/zyjiaocheng/758/645af463f357435faf01f09f3c9a700f.jpg)
package JFrame_001;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JiSuanQi {
public static void main(String[] args) {
//窗体
JFrame f = new JFrame("计算器");
//StringBuffer允许对字符串多次修改,不产生新的未使用对象
StringBuffer s = new StringBuffer();
//主窗体的宽和高
f.setSize(740, 779);
//主窗体的位置
f.setLocation(0, 0);
// 主窗体中的组件设置为绝对定位
f.setLayout(new FlowLayout(FlowLayout.LEFT,0,0)); //组件布局
//输入框
JTextField text = new JTextField();
text.setPreferredSize(new Dimension(721,123));
text.setFont(new Font("宋体",Font.BOLD,50));
f.add(text);
//按钮组件
JButton but[] = new JButton[21];
but[1] = new JButton("CE");
but[1].setPreferredSize(new Dimension(360,123));
but[1].setFont(new Font("宋体",Font.BOLD,50));
but[3] = new JButton("清除");
but[4] = new JButton("/");
but[5] = new JButton("7");
but[6] = new JButton("8");
but[7] = new JButton("9");
but[8] = new JButton("*");
but[9] = new JButton("4");
but[10] = new JButton("5");
but[11] = new JButton("6");
but[12] = new JButton("-");
but[13] = new JButton("1");
but[14] = new JButton("2");
but[15] = new JButton("3");
but[16] = new JButton("+");
but[17] = new JButton(".");
but[18] = new JButton("0");
but[19] = new JButton("=");
but[19].setPreferredSize(new Dimension(360,123));
but[19].setFont(new Font("宋体",Font.BOLD,50));
f.add(but[1]);
//设置按钮大小并添加按钮到主窗体
for(int i=3;i<but.length-2;i++) {
but[i].setPreferredSize(new Dimension(180,123));
//but[i].setFont(new Font("宋体",Font.BOLD,50);
but[i].setFont(new Font("宋体",Font.BOLD,50));
f.add(but[i]);
}
f.add(but[19]);
//监听器
but[18].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('0');
String str = s.toString();
text.setText(str);
}
});
but[17].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
s.append('.');
String str = s.toString();
text.setText(str);
}
});
but[16].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('+');
String str = s.toString();
text.setText(str);
}
});
but[15].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('3');
String str = s.toString();
text.setText(str);
}
});
but[14].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('2');
String str = s.toString();
text.setText(str);
}
});
but[13].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('1');
String str = s.toString();
text.setText(str);
}
});
but[12].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('-');
String str = s.toString();
text.setText(str);
}
});
but[11].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('6');
String str = s.toString();
text.setText(str);
}
});
but[10].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('5');
String str = s.toString();
text.setText(str);
}
});
but[9].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('4');
String str = s.toString();
text.setText(str);
}
});
but[8].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('*');
String str = s.toString();
text.setText(str);
}
});
but[7].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('9');
String str = s.toString();
text.setText(str);
}
});
but[6].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('8');
String str = s.toString();
text.setText(str);
}
});
but[5].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('7');
String str = s.toString();
text.setText(str);
}
});
but[4].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.append('/');
String str = s.toString();
text.setText(str);
}
});
//清除
but[3].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(s.length()!=0)
s.delete(s.length()-1, s.length());
String str = s.toString();
text.setText(str);
}
});
//CE
but[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s.delete(0, s.length());
text.setText(" ");
}
});
//结果“=”,split()拆分字符串与StringTokenizer拆分字符串
but[19].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sGet = text.getText().toString();//获取文本框的内容
if(sGet.contains("+")) {
String sArgs[] = sGet.split("\\+");
double number[] = new double[2];
for(int i=0;i<2;i++) {
number[i] = Double.parseDouble(sArgs[i]);
}
double num = number[0]+number[1];
String ss = String.valueOf(num);
text.setText(sGet+"="+ss);
}
if(sGet.contains("-")) {
String sArgs[] = sGet.split("\\-");
double number[] = new double[2];
for(int i=0;i<2;i++) {
number[i] = Double.parseDouble(sArgs[i]);
}
double num = number[0]-number[1];
String ss = String.valueOf(num);
text.setText(sGet+"="+ss);
}
if(sGet.contains("*")) {
String sArgs[] = sGet.split("\\*");
double number[] = new double[2];
for(int i=0;i<2;i++) {
number[i] = Double.parseDouble(sArgs[i]);
}
double num = number[0]*number[1];
String ss = String.valueOf(num);
text.setText(sGet+"="+ss);
}
if(sGet.contains("/")) {
String sArgs[] = sGet.split("\\/");
double number[] = new double[2];
for(int i=0;i<2;i++) {
number[i] = Double.parseDouble(sArgs[i]);
}
double num = number[0]/number[1];
if(number[1]==0) {
text.setText(sGet+"="+"Erroring");
}
else {
String ss = String.valueOf(num);
text.setText(sGet+"="+ss);
}
}
}
});
// 关闭窗体的时候,退出程序.
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示窗体
f.setVisible(true);
}
}
内容总结
以上是互联网集市为您收集整理的Java简单计算器的实现(+-*/)全部内容,希望文章能够帮你解决Java简单计算器的实现(+-*/)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。