编写程序实现一个简单计算器的基本功能,具体可以模仿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. synchronized到底锁住的是谁?

    本文代码仓库:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/sync 先来一道校招级并发编程笔试题 题 ...

  2. Hibernate 数据层基类实现

    提取经常操作表如新增.修改.删除.查询.分页查询.统计等业务功能,形成基类,用泛型传参,有利于每个实体对象数据层继承. package com.base.dao; import java.io.Ser ...

  3. "(error during evaluation)" computed

    在vue-cli搭建的去哪网app项目中使用了  computed  计算属性 computed计算属性在chrome插件中的 vue devtools 插件中报错 应该显示出来 computed 属 ...

  4. tomcat在centos7能启动不显示

    首先查看启动日志,日志显示成功启动,java路径也对,没有问题. 日志目录路径为$(tomcat)/logs/catalina.log 查看命令为:tail -300f catalina.log 然后 ...

  5. [译]Vulkan教程(24)索引buffer

    [译]Vulkan教程(24)索引buffer Index buffer 索引buffer Introduction 入门 The 3D meshes you'll be rendering in a ...

  6. jQuery-点击返回顶部

    在页面上,有时需要点击某个图标钮实现返回顶部的效果. 实现方式如下,给图标按钮增加名叫goTop-hook的类. // 点击返回顶部 $(window).scroll(function() { if ...

  7. SpringBoot2.0 整合 JWT 框架,解决Token跨域验证问题

    本文源码:GitHub·点这里 || GitEE·点这里 一.传统Session认证 1.认证过程 1.用户向服务器发送用户名和密码. 2.服务器验证后在当前对话(session)保存相关数据. 3. ...

  8. css实现左右两个div等高

    提出问题: 现在有两个div,但是两个div里面内容多少不确定,可能左边多,可能右边多,css要如何设置可以保证左右两边的div等高呢? 解决方案: 每个div使用display:table-cell ...

  9. 【UR #5】怎样更有力气

    Problem Description 大力水手问禅师:"大师,很多事情都需要用很大力气才能完成,而我在吃了菠菜之后力气很大,于是就导致我现在非常依赖菠菜.我很讨厌我的现状,有没有办法少吃点 ...

  10. 关于HACLON程序导出C#程序,运行报错解决方法

    摘要:一些环境配置异常的解决方法. 一,打不开相机: 1.打开系统高级设置--环境变量中是否有 HALCONROOT+安装目录名,若无进行添加. 2.关闭计算机其他连接相机的软件,例如海康的MVS,H ...