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 ...
随机推荐
- Dynamics 365 Customer Engagement导入解决方案时出错:Microsoft.Crm.CrmException: Plug-in assembly does not contain the required types or assembly content cannot be updated.
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- MySQL 社区版 安装小记
根据刘铁猛老师的教程,自己折腾一下 1. 安装包准备 在Windows10 64bit上安装,故需要准备vc++ 2013和2015的Redistributable的包,搜索即有,无需细说. 示例数据 ...
- zipalign的使用
zipalign 是一个存档对齐工具,可为Android应用程序(.apk)文件提供重要的优化.目的是确保所有未压缩数据以相对于文件开头的特定对齐开始.具体来说,它会导致.apk中的所有未压缩数据(如 ...
- TCP安全,SYN Flooding 和 nmap
目录 SYN flooding nmap nmap idle SYN flooding 简介:向target持续发送SYN=1的TCP报文,使target因内存满而拒绝服务. 命令:netwox 76 ...
- 深挖的Java源代码之Integer.parseInt()vs Integer.valueOf()
Integer.parseInt()和Integer.valueOf()都是用来将String转换为Int的,但是为什么Java会提供两个这样的方法呢,他们如果是同样的操作,岂不是多此一举? 我们来深 ...
- October 06th, 2019. Week 41st, Sunday
Life is very capricious. 生命无常. Is life capricious? Maybe. But we can still make life a little more c ...
- 外部调用Tomcat启动脚本后日志中文显示乱码问题的解决
外部sh脚本如下 #!/bin/bash while read LINE do echo "Hello $LINE!" case $LINE in all) tail -f -n2 ...
- 七、3Dslicer的坐标系
一.参考博客 https://blog.csdn.net/Huadong_eddy/article/details/84988166
- Android Fragment 隐藏或显示时调用的生命周期方法
Fragment使用方式大体分两种: 大家要注意不同的Fragment使用方法,Fragment隐藏和显示调用的生命周期方法是不同的,以下是Fragment显示隐藏调用的方法: //判断是否展示—与V ...
- 关于C# webapi ,接口返回字符串和json格式 ,返回值中有反斜杠
最近遇到一个比较郁闷的问题,记录一下 写了一个接口,想返回json 数据,但是返回值中总是带有反斜杠... ,下面来看原因 首先,配置 webapi的路由 App_Start 文件夹下 ,WebApi ...