java图形界面 计算器实现
编写程序实现一个简单计算器的基本功能,具体可以模仿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图形界面 计算器实现的更多相关文章
- 【计项02组01号】Java版图形界面计算器
Java版图形界面计算器1.0版本 项目分析[1.0] 组成部分 代码结构 (1)窗口的创建 在<JDK 核心 API>中我们提到,创建一个窗口需要使用 JFrame 类.在本实验中,我们 ...
- Java图形界面学习---------简易登录界面
/** * @author Administrator * Java图形界面学习---------简易登录界面 * date:2015/10/31 */ import java.awt.BorderL ...
- Java 图形界面开发--图文并茂建立学生管理系统
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/50932501 冷血之心的博客) 图形用户界面(Graphics U ...
- Java图形界面GUI
Java图形界面GUI 设置窗体JFrame对象 package com.Aha.Best; import javax.swing.ImageIcon; import javax.swing.JFra ...
- Java第5次实验提纲(Java图形界面编程)
1. Swing与NetBeans 使用NetBeans编写简单界面.见GUI实验参考文件中的0.第06次实验(图形程序设计.事件处理与Swing).doc 题目1: Swing用户界面组件与事件处理 ...
- Java第05次实验提纲(Java图形界面编程)
1. Swing与NetBeans 使用NetBeans编写简单界面.见GUI实验参考文件中的0.第06次实验(图形程序设计.事件处理与Swing).doc 题目1: Swing用户界面组件与事件处理 ...
- java 图形界面 Socket编程
一.使用图形界面实现客户端服务器端的通信: 上代码: 服务器端代码: package cn.MyNET; import java.io.*; import java.net.*; import jav ...
- java 图形界面
1.创建一个窗口框架 /** * java 用户界面框架 * 2016/5/10 */ package org.windows; import javax.swing.*; public class ...
- JAVA 图形界面开发基础详解
与C的win32一样,JAVA也有自己的图形界面开发,将在此篇博客中对基础部分进行讲解. 1.Java提供的图形界面类有哪些? Java提供了两套图形界面 (1)AWT组建(基础) AWT组件是jdk ...
随机推荐
- 通过U盘在物理机安装CentOS出现Timeout的问题
错误信息:centos dracut timeout..... 解决方案: 在进入install页面是,按e,启动编辑.要保证Label与U盘的卷标保持一致即可.
- thinkphp6安装报错,composer install tp6 报错 Parse error: syntax error
composer install thinkphp6 报错 Parse error: syntax error, unexpected ':', expecting '{' in vendor\top ...
- python中json与pickle的简要说明
import json ======> 注意:不同语言之间通用但不能传输对象类型 该模块中最重要的方法: 1.json.dump(‘python数据’,‘json文件’) # 将pyt ...
- 12-《Node.js开发指南》-核心模块
全局对象 Node.js中的全局对象是global 所有全局变量(除了global本身以外)都是global对象的属性 最根本的作用为全局变量的宿主 全局变量 //满足以下条件的是全局变量 a.在最外 ...
- CountDownLatch(倒计时计数器)使用说明 --并发
方法说明: public void countDown() 递减锁存器的计数,如果计数到达零,则释放所有等待的线程.如果当前计数大于零,则将计数减少.如果新的计数为零,出于线程调度目的, ...
- 从荣耀 xSport Pro 运动蓝牙耳机发布看蓝牙立体声耳机的新动态
10月22日,荣耀在北京举行新品发布会,不仅带来了荣耀20青春版手机,还正式发布了荣耀xSport PRO运动蓝牙耳机.该款耳机是荣耀全新一代颈戴式运动蓝牙耳机,兼具运动和时尚属性,高颜值的渐变色机身 ...
- js中call、apply、bind到底有什么区别?bind返回的方法还能修改this指向吗?
壹 ❀ 引 同事最近在看angularjs源码,被源码中各种bind,apply弄的晕头转向:于是他问我,你知道apply,call与bind的区别吗?我说apply与call是函数应用,指定thi ...
- 织女星开发板调试器升级为Jlink固件
前言 为了能使用板载的FreeLink调试器来调试RISC-V内核,我们需要把默认的CMSIC-DAP固件,升级为JLink固件,固件升级之后,通过选择使用不同的驱动程序,来支持ARM内核还是RISC ...
- Airtest 之 游戏自动化(5分钟教你王者农药刷金币)
一.准备工作: 1)安装腾讯手游助手,下载王者荣耀,安装启动( 你也可以直接连接手机启动游戏,或者使用其他的模拟器 ) 2)安装AirtestIDE,在设备窗中连接游戏Windows(详情参考笔者另 ...
- Python中7个不一样的代码写法
打印index 对于一个列表,或者说一个序列我们经常需要打印它的index,一般传统的做法或者说比较low的写法: 更优雅的写法是多用enumerate 两个序列的循环 我们会经常对两个序列进行计算或 ...