编写程序实现一个简单计算器的基本功能,具体可以模仿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. Android框架式编程之ViewModel

    一.ViewModel介绍 ViewModel类是被设计用来以可感知生命周期的方式存储和管理 UI 相关数据.ViewModel中数据会一直存活即使 Activity Configuration发生变 ...

  2. 【转载】Vue.js 安装及其环境搭建

    注:最近在学习Vue,以下是环境搭配方法: ****************************************************************************** ...

  3. (办公)记事本_通过xshell连接Liunx服务器

    任务:需要用xshell连接到Liunx服务器,装配环境,放置项目,查看日志,以后就要做,磁盘扩容,均衡负载,以及病毒错误. 第一步,先连接上: 1.xshell新建会话,刚才买的liunx的公网地址 ...

  4. SQL Server事务复制(sql 2008 r2)

    一.环境准备 1.两个虚拟服务器 主机1:XINXIBU01  作为发布和分发服务器   主 机2:XINXIBU02 192.168.1.160  作业阅服务器 2.SQL SERVER sql 2 ...

  5. 数组中重复的数字(剑指offer_3)

    在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的,也不知道每个数字重复几次.请找出数组中任意一个重复的数字. Input: {2,3,1,0 ...

  6. vue-property-decorator和typescript结合构建的class类组件,父组件触发子组件方法的方式

    vue-property-decorator和typescript结合构建的class类组件,父组件触发子组件方法的方式 class类组件示例 Father类组件 <template> & ...

  7. opencv在VS2017上的环境搭建

    最近开始做一个图像识别的小项目,需要安装opencv,VS里报的错迷的一批,网上教程好多,找了好长时间,终于找的两个解决了问题,在这儿记录一下. 安装很简单,在opencv官网(https://ope ...

  8. Web开发跨域问题

    什么是域?    协议,  ip(域名). 端口 前端:域  后端:域   js 进行跨域请求, 因为浏览器的同源策略,导致了两个不同域请求出错 浏览器 会尝试向后端发送 option 请求, --- ...

  9. 软件测试价值提升之路- 第二章"价值实现的起点"读书笔记

    价值实现的起点 2.1 打破常规 打破哪些已经不适应现在软件开发需要的“准则”,明确需要在什么样的环境下.瞄准什么目标来实现测试的价值 找风险:研发内部测试 测试最基础的是找bug,但需要根据风险找最 ...

  10. java遍历request.getParameterMap()中的值

    在开发过程中发现request对象有提供一个request.getParameterMap()方法可以获取到从前端请求发送的参数Map. 但是在使用get()方法通过key(键)去获取这个参数Map中 ...