编写程序实现一个简单计算器的基本功能,具体可以模仿Windows附件中的计算器或模拟常见的实物计算器。


package beizi;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane; import java.awt.TextArea;
import java.awt.Color;
import javax.swing.JTextField; public class cal { private JFrame frame;
private JTextField textField;
private JTextField textField_1; /**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
cal window = new cal();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the application.
*/
public cal() {
initialize();
// calResult();
} /**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); JButton btnNewButton = new JButton("1");
btnNewButton.setBackground(Color.LIGHT_GRAY);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"1");
}
});
btnNewButton.setBounds(33, 142, 68, 38);
frame.getContentPane().add(btnNewButton); JButton button = new JButton("4");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"4");
}
});
button.setBackground(Color.LIGHT_GRAY);
button.setBounds(33, 194, 68, 38);
frame.getContentPane().add(button); JButton button_1 = new JButton("7");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"7");
}
});
button_1.setBackground(Color.LIGHT_GRAY);
button_1.setBounds(33, 242, 68, 38);
frame.getContentPane().add(button_1); JButton button_2 = new JButton("2");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"2");
}
});
button_2.setBackground(Color.LIGHT_GRAY);
button_2.setBounds(111, 142, 68, 38);
frame.getContentPane().add(button_2); JButton button_3 = new JButton("5");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"5");
}
});
button_3.setBackground(Color.LIGHT_GRAY);
button_3.setBounds(111, 194, 68, 38);
frame.getContentPane().add(button_3); JButton button_4 = new JButton("8");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"8");
}
});
button_4.setBackground(Color.LIGHT_GRAY);
button_4.setBounds(111, 242, 68, 38);
frame.getContentPane().add(button_4); JButton button_5 = new JButton("3");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"3");
}
});
button_5.setBackground(Color.LIGHT_GRAY);
button_5.setBounds(189, 142, 68, 38);
frame.getContentPane().add(button_5); JButton button_6 = new JButton("6");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"6");
}
});
button_6.setBackground(Color.LIGHT_GRAY);
button_6.setBounds(189, 194, 68, 38);
frame.getContentPane().add(button_6); JButton button_7 = new JButton("9");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"9");
}
});
button_7.setBackground(Color.LIGHT_GRAY);
button_7.setBounds(189, 242, 68, 38);
frame.getContentPane().add(button_7); JButton button_8 = new JButton("0");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"0");
}
});
button_8.setBackground(Color.LIGHT_GRAY);
button_8.setBounds(111, 290, 68, 38);
frame.getContentPane().add(button_8); JButton button_9 = new JButton("(");
button_9.setBackground(Color.ORANGE);
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"(");
}
});
button_9.setBounds(33, 93, 68, 38);
frame.getContentPane().add(button_9); JButton button_10 = new JButton(")");
button_10.setBackground(Color.ORANGE);
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+")");
}
});
button_10.setBounds(109, 94, 70, 38);
frame.getContentPane().add(button_10); JButton button_11 = new JButton("+");
button_11.setBackground(Color.ORANGE);
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"+");
}
});
button_11.setBounds(267, 142, 44, 38);
frame.getContentPane().add(button_11); JButton button_12 = new JButton("-");
button_12.setBackground(Color.ORANGE);
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"-");
}
});
button_12.setBounds(267, 194, 44, 38);
frame.getContentPane().add(button_12); JButton button_13 = new JButton("*");
button_13.setBackground(Color.ORANGE);
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"*");
}
});
button_13.setBounds(267, 242, 44, 38);
frame.getContentPane().add(button_13); JButton button_14 = new JButton("/");
button_14.setBackground(Color.ORANGE);
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"/");
}
});
button_14.setBounds(267, 290, 44, 38);
frame.getContentPane().add(button_14); JButton button_15 = new JButton("=");
button_15.setBackground(Color.GREEN);
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");//将字符串公式转换为计算公式
String formula=textField.getText();
try {
textField_1.setText(js.eval(formula).toString());
// textField_1.set
}catch(Exception e) { }
}
});
button_15.setBounds(334, 252, 44, 76);
frame.getContentPane().add(button_15); textField = new JTextField();
textField.setBounds(33, 21, 345, 37);
frame.getContentPane().add(textField);
textField.setColumns(10); textField_1 = new JTextField();
textField_1.setBackground(Color.WHITE);
textField_1.setBounds(189, 94, 189, 37);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10); JButton button_16 = new JButton("C");
button_16.setBackground(Color.MAGENTA);
button_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
int t=text.length()-1;
if(t<0) {
JOptionPane.showMessageDialog(null, "已经没有了(("); }
else {
text=text.substring(0, t);
textField.setText(text);
} }
});
button_16.setBounds(334, 142, 44, 38);
frame.getContentPane().add(button_16); JButton button_17 = new JButton("X");
button_17.setBackground(Color.RED);
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText("");
textField_1.setText("");
}
});
button_17.setBounds(334, 202, 44, 38);
frame.getContentPane().add(button_17); JButton button_18 = new JButton(".");
button_18.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+".");
}
});
button_18.setBackground(Color.ORANGE);
button_18.setBounds(33, 290, 68, 38);
frame.getContentPane().add(button_18); JButton button_19 = new JButton("%");
button_19.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText(text+"%");
}
});
button_19.setBackground(Color.ORANGE);
button_19.setBounds(189, 290, 68, 38);
frame.getContentPane().add(button_19); }
}

大概的效果,Java初学者,,代码重复严重,以后改,,,

java图形界面 计算器实现的更多相关文章

  1. 【计项02组01号】Java版图形界面计算器

    Java版图形界面计算器1.0版本 项目分析[1.0] 组成部分 代码结构 (1)窗口的创建 在<JDK 核心 API>中我们提到,创建一个窗口需要使用 JFrame 类.在本实验中,我们 ...

  2. Java图形界面学习---------简易登录界面

    /** * @author Administrator * Java图形界面学习---------简易登录界面 * date:2015/10/31 */ import java.awt.BorderL ...

  3. Java 图形界面开发--图文并茂建立学生管理系统

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/50932501 冷血之心的博客) 图形用户界面(Graphics U ...

  4. Java图形界面GUI

    Java图形界面GUI 设置窗体JFrame对象 package com.Aha.Best; import javax.swing.ImageIcon; import javax.swing.JFra ...

  5. Java第5次实验提纲(Java图形界面编程)

    1. Swing与NetBeans 使用NetBeans编写简单界面.见GUI实验参考文件中的0.第06次实验(图形程序设计.事件处理与Swing).doc 题目1: Swing用户界面组件与事件处理 ...

  6. Java第05次实验提纲(Java图形界面编程)

    1. Swing与NetBeans 使用NetBeans编写简单界面.见GUI实验参考文件中的0.第06次实验(图形程序设计.事件处理与Swing).doc 题目1: Swing用户界面组件与事件处理 ...

  7. java 图形界面 Socket编程

    一.使用图形界面实现客户端服务器端的通信: 上代码: 服务器端代码: package cn.MyNET; import java.io.*; import java.net.*; import jav ...

  8. java 图形界面

    1.创建一个窗口框架 /** * java 用户界面框架 * 2016/5/10 */ package org.windows; import javax.swing.*; public class ...

  9. JAVA 图形界面开发基础详解

    与C的win32一样,JAVA也有自己的图形界面开发,将在此篇博客中对基础部分进行讲解. 1.Java提供的图形界面类有哪些? Java提供了两套图形界面 (1)AWT组建(基础) AWT组件是jdk ...

随机推荐

  1. Pinpoint-agent监控springboot编译的jar启动方式

    由于springboot在打包发版时已经将tomcat容器内嵌到jar文件中,可以通过以下命令来使pinpoint-agent监控生成的jar服务 java -javaagent:D:\Softwar ...

  2. n个数字相加

    求s=a+aa+aaa+aaaa+aa...a的值 其中a是一个数字,多少个数字相加由键盘输入控制 a = int(input("数字:")) count = int(input( ...

  3. Pipe——高性能IO(二)

    Pipelines - .NET中的新IO API指引(一) Pipelines - .NET中的新IO API指引(二) 关于System.IO.Pipelines的一篇说明 System.IO.P ...

  4. 安装PS

    1:下载溜云库 2:查找PS软件,下载 3:按照教程安装

  5. 【docker部署】基于linux的centos操作系统部署安装docker容器

    一.docker介绍 容器是轻量级的,包含应用运行所需所有东西(代码.库.运行时环境.系统设置,以及依赖关系)的独立的包.每个容器都部署于它自己的 CPU.内存.块 I/O,以及网络资源上,所有这些都 ...

  6. Web安全测试学习笔记-DVWA-存储型XSS

    XSS(Cross-Site Scripting)大致分为反射型和存储型两种,之前对XSS的认知仅停留在如果网站输入框没有屏蔽类似<script>alert('ok')</scrip ...

  7. mac怎么连接windows远程桌面

    首先需要下载一个软件,因为苹果电脑并没有提供免费的软件给我们,所以不能像windows一样, 直接在任务管理中搜素远程桌面然后输入ip地址,用户名,密码就可以远程连接, 而苹果也有提供一个软件,但要付 ...

  8. opencv---(腐蚀、膨胀、边缘检测、轮廓检索、凸包、多边形拟合)

    一.腐蚀(Erode) 取符合模板的点, 用区域最小值代替中心位置值(锚点) 作用: 平滑对象边缘.弱化对象之间的连接. opencv 中相关函数:(erode) // C++ /** shape: ...

  9. 利用QQ获取ip

    首先启动任务管理器,选择性能选型,点击打开资源管理器  点击网络,找到qq.exe 点击下面的TCP链接  最好让你的qq好友发一个离线文件,在接收的时候注意远程连接,即使您所要的你好友的ip地址 

  10. Android App图片资源文件压缩利器McImage

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/195 Android App图片资源文件压缩利器McIma ...