/*******************************************
*
* 程序四:计算器
* Calculator.java
*
* Author: Hejie 20044440219
*
********************************************/
//简易计算器,实现+、-、*、/四个功能,原理非常简单,只是工作量大
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame
{
Container ct=this.getContentPane();
JTextField text=new JTextField("0.0");
String source1=null;
String source2=null;
String opera=null;
boolean flag1;
boolean flag2;
boolean flag3;
JPanel pane=new JPanel();
JPanel pane1=new JPanel();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b0=new JButton("0");
JButton b11=new JButton("+");
JButton b12=new JButton("-");
JButton b13=new JButton("*");
JButton b14=new JButton("/");
JButton b15=new JButton(".");
JButton b16=new JButton("=");
JButton b17=new JButton("clean");
JButton b18=new JButton("exit");
Calculator()
{
//初始化程序界面
super("-杰哥牌计算器-");
setBounds(200,200,200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
text.setHorizontalAlignment(JTextField.RIGHT);
text.setEditable(false);
pane1.setLayout(new GridLayout(1,2,2,2));
pane.setLayout(new GridLayout(4,4,2,2));
pane.add(b1);
pane.add(b2);
pane.add(b3);
pane.add(b11);
pane.add(b4);
pane.add(b5);
pane.add(b6);
pane.add(b12);
pane.add(b7);
pane.add(b8);
pane.add(b9);
pane.add(b13);
pane.add(b0);
pane.add(b15);
pane.add(b16);
pane.add(b14);
pane1.add(b17);
pane1.add(b18);
b11.setForeground(Color.red);
b12.setForeground(Color.red);
b13.setForeground(Color.red);
b14.setForeground(Color.red);
b15.setForeground(Color.red);
b16.setForeground(Color.red);
b17.setForeground(Color.red);
b18.setForeground(Color.red);
ct.add(text,"North");
text.setForeground(Color.red);
ct.add(pane,"Center");
ct.add(pane1,"South");
setVisible(true);
Listen1 l1=new Listen1();
Listen2 l2=new Listen2();
Listen3 l3=new Listen3();
Listen4 l4=new Listen4();
b1.addActionListener(l1);
b2.addActionListener(l1);
b3.addActionListener(l1);
b4.addActionListener(l1);
b5.addActionListener(l1);
b6.addActionListener(l1);
b7.addActionListener(l1);
b8.addActionListener(l1);
b9.addActionListener(l1);
b0.addActionListener(l1);
b11.addActionListener(l2);
b12.addActionListener(l2);
b13.addActionListener(l2);
b14.addActionListener(l2);
b15.addActionListener(l2);
b16.addActionListener(l3);
b17.addActionListener(l4);
b18.addActionListener(l4);
}
class Listen1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String text1=text.getText();
String text2=e.getActionCommand();
if(text1.equals("0.0")||text1.equals("+")||text1.equals("-")||text1.equals("*")||text1.equals("/"))
{
opera=new String(text1);
text.setText(text2);
}
else
text.setText(text1+text2);
}
}
class Listen2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String text1=text.getText();
String text2=e.getActionCommand();
if(!(text1.equals("0.0")||text1.equals("+")||text1.equals("-")||text1.equals("*")||text1.equals("/")))
{
source1=new String(text1);
text.setText(text2);
}
}
}
class Listen3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String text1=text.getText();
if(!(text1.equals("+")||text1.equals("-")||text1.equals("*")||text1.equals("/")))
{
source2=new String(text1);
double s1=Double.parseDouble(source1);
double s2=Double.parseDouble(source2);
double answer;
if(opera.equals("+"))
answer=s1+s2;
else if(opera.equals("-"))
answer=s1-s2;
else if(opera.equals("*"))
answer=s1*s2;
else//(opera.equals("/"))
{
if(s2==0.0)
{
System.out.println("除数不能为0!");
answer=0.0;
}
else
answer=s1/s2;
}
text.setText(Double.toString(answer));
}
}
}
class Listen4 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object butt=e.getSource();
if(butt==b17)
{
source1=source2=null;
text.setText("0.0");
}
else if(butt==b18)
{
System.exit(0);
}
}
}
public static void main(String [] args)
{
try {
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println("Couldn't use the system look and feel:"+e);
}
//让程序界面呈现系统当前外观
//JFrame.setDefaultLookAndFeelDecorated(true);
Calculator calculator=new Calculator();
}
}
java版计算器 源代码
5星 · 超过95%的资源 需积分: 10 114 浏览量
2007-12-27
05:04:04
上传
评论 6
收藏 13KB RAR 举报
PostMonitor
- 粉丝: 32
- 资源: 21
最新资源
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页