java使用window builder图形界面开发简易计算器
界面效果:

/**
*
*/
package calculator;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import javax.swing.SpringLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JSpinner;
import javax.swing.JFormattedTextField;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import java.awt.Color;
/**
* @author Ocean
*
*/
public class Calc_Main extends JFrame {
private JTextField textField;
private JTable table;
String s = "";// 显示算式
long[] num = new long[100];// 储存运算数据
char[] b = new char[100];// 储存运算符
int i = 0;// 输入运算数据和运算符是给数组计数
int c;// 计算结果时计数
long jieguo = 0;// 计算结果
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calc_Main frame = new Calc_Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 构造方法
* Create the frame.
*/
public Calc_Main() {
setFont(new Font("Dialog", Font.PLAIN, 12));
setTitle("计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(800, 300, 382, 494);
getContentPane().setLayout(null);
textField = new JTextField();
textField.setForeground(new Color(102, 51, 255));
textField.setFont(new Font("宋体", Font.PLAIN, 18));
textField.setBounds(10, 10, 344, 50);
getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("C");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s="";
textField.setText(s);//清空文本框
for(c=0;c<100;c++)
{
num[c]=0;//清空运算数据
b[c]=0;//清空运算符
}
i=0;
c=0;
jieguo=0;
}
});
btnNewButton.setBounds(20, 99, 65, 40);
getContentPane().add(btnNewButton);
JButton button = new JButton("/");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s=s+"/";
textField.setText(s);//在文本框中显示相应运算符
b[i]='/';
i++; //输入运算符号后给数组地址+1存放后面的输入
}
});
button.setBounds(107, 99, 65, 40);
getContentPane().add(button);
JButton button_1 = new JButton("*");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s=s+"*";
textField.setText(s);//在文本框中显示相应运算符
b[i]='*';
i++;
}
});
button_1.setBounds(189, 99, 65, 40);
getContentPane().add(button_1);
JButton button_2 = new JButton("-");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s=s+"-";
textField.setText(s);//在文本框中显示相应运算符
b[i]='-';
i++;
}
});
button_2.setBounds(276, 99, 65, 40);
getContentPane().add(button_2);
JButton button_3 = new JButton("1");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
s += "1";
textField.setText(s);//在文本框中显示相应数字
num[i] = num[i] * 10 + 1;
}
});
button_3.setBounds(20, 170, 65, 40);
getContentPane().add(button_3);
JButton button_4 = new JButton("2");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s += "2";
textField.setText(s);//在文本框中显示相应数字
num[i] = num[i] * 10 + 2;
}
});
button_4.setBounds(107, 170, 65, 40);
getContentPane().add(button_4);
JButton button_5 = new JButton("3");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s += "3";
textField.setText(s);//在文本框中显示相应数字
num[i] = num[i] * 10 + 3;
}
});
button_5.setBounds(189, 170, 65, 40);
getContentPane().add(button_5);
JButton button_6 = new JButton("+");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s=s+"+";
textField.setText(s);//在文本框中显示相应运算符
b[i]='+';
i++;
}
});
button_6.setBounds(276, 170, 65, 116);
getContentPane().add(button_6);
JButton button_7 = new JButton("4");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s += "4";
textField.setText(s);//在文本框中显示相应数字
num[i] = num[i] * 10 + 4;
}
});
button_7.setBounds(20, 246, 65, 40);
getContentPane().add(button_7);
JButton button_8 = new JButton("5");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s += "5";
textField.setText(s);//在文本框中显示相应数字
num[i] = num[i] * 10 + 5;
}
});
button_8.setBounds(107, 246, 65, 40);
getContentPane().add(button_8);
JButton button_9 = new JButton("6");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s += "6";
textField.setText(s);//在文本框中显示相应数字
num[i] = num[i] * 10 + 6;
}
});
button_9.setBounds(189, 246, 65, 40);
getContentPane().add(button_9);
JButton button_11 = new JButton("7");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s += "7";
textField.setText(s);//在文本框中显示相应数字
num[i] = num[i] * 10 + 7;
}
});
button_11.setBounds(20, 315, 65, 40);
getContentPane().add(button_11);
JButton button_12 = new JButton("8");
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s += "8";
textField.setText(s);//在文本框中显示相应数字
num[i] = num[i] * 10 + 8;
}
});
button_12.setBounds(107, 315, 65, 40);
getContentPane().add(button_12);
JButton button_13 = new JButton("9");
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s += "9";
textField.setText(s);//在文本框中显示相应数字
num[i] = num[i] * 10 + 9;
}
});
button_13.setBounds(189, 315, 65, 40);
getContentPane().add(button_13);
JButton button_14 = new JButton("=");
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
switch(b[0])//为了方便用循环来计算,我想到的是先把第一次计算提出来
{
case '+':
{
jieguo=num[0]+num[1];
break;
}
case '-':
{
jieguo=num[0]-num[1];
break;
}
case '*':
{
jieguo=num[0]*num[1];
break;
}
case '/':
{
jieguo=num[0]/num[1];
break;
}
}
c++;
// 一个算式多次运算
for(c=1;c<i;c++)
{
switch(b[c])
{
case '+':
{
jieguo=jieguo+num[c+1];
break;
}
case '-':
{
jieguo=jieguo-num[c+1];
break;
}
case '*':
{
jieguo=jieguo*num[c+1];
break;
}
case '/':
{
jieguo=jieguo/num[c+1];
break;
}
}
}
textField.setText(String.valueOf(jieguo));//.setText()的参数需要String,所以用String.valueOf()将long转换为String
}
});
button_14.setBounds(276, 315, 65, 104);
getContentPane().add(button_14);
JButton button_15 = new JButton("0");
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s += "0";
textField.setText(s);//在文本框中显示相应数字
num[i] = num[i] * 10 + 0;
}
});
button_15.setBounds(20, 379, 152, 40);
getContentPane().add(button_15);
JButton button_17 = new JButton(".");
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//还没想出来怎么整,有知道的大佬还请多指教``
}
});
button_17.setBounds(189, 379, 65, 40);
getContentPane().add(button_17);
JLabel label = new JLabel("注意本计算机没有计算优先级(一律从左到右计算)");
label.setBounds(22, 430, 321, 15);
getContentPane().add(label);
}
}
java使用window builder图形界面开发简易计算器的更多相关文章
- Java图形界面开发—简易登录注册小程序
登录注册小代码,将学过的一些小知识融合在一起进行了使用,加深印象.本例中如果有注释不详细的地方,详见其它博客. Java程序操作数据库SQLserver详解 功能介绍:简单的登录注册系统,使用了数据库 ...
- Java图形界面开发—简易记事本
在学习了Java事件之后,自己写了一个极其简单的记事本.用到了MenuBar,Menu,MenuITem等控件,事件包括ActionListener以及KeyListener. 代码如下: ...
- Java 图形界面开发--图文并茂建立学生管理系统
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/50932501 冷血之心的博客) 图形用户界面(Graphics U ...
- JAVA与图形界面开发(Applet应用程序、AWT库、Swing)
Applet 1)简单说,Applet就是嵌入到网页中的小程序,Java代码. 2)编写Applet程序,要继承JApplet类,并根据自己需要覆写相关方法(init.start.stop.destr ...
- Java GUI图形界面开发工具
Applet 应用程序 一种可以在 Web 浏览器中执行的小程序,扩展了浏览器中的网页功能. 缺: 1.需要下载 Applet 及其相关文件 2.Applet 的功能是受限制的 优: 3.无需 ...
- Java图形界面学习---------简易登录界面
/** * @author Administrator * Java图形界面学习---------简易登录界面 * date:2015/10/31 */ import java.awt.BorderL ...
- JAVA 图形界面开发基础详解
与C的win32一样,JAVA也有自己的图形界面开发,将在此篇博客中对基础部分进行讲解. 1.Java提供的图形界面类有哪些? Java提供了两套图形界面 (1)AWT组建(基础) AWT组件是jdk ...
- Java Swing 图形界面开发(目录)
Java Swing 图形界面开发(目录) 2017年05月30日 23:50:42 阅读数:5228 本文链接: http://blog.csdn.net/xietansheng/article/d ...
- Java Swing图形界面开发
本文转自xietansheng的CSDN博客内容,这是自己见过的最通俗易懂.最适合快速上手做Java GUI开发的教程了,这里整合一下作为自己以后复习的笔记: 原文地址:https://blog.cs ...
随机推荐
- AJ整理问题之:copy,对象自定义copy 什么是property
AJ分享,必须精品 copy copy的正目的 copy 目的:建立一个副本,彼此修改,各不干扰 Copy(不可变)和MutableCopy(可变)针对Foundation框架的数据类型. 对于自定义 ...
- I. 蚂蚁上树
蚂蚁上树(Sauteed Vermicelli with minced Pork),又名肉末粉条,是四川省及重庆市的特色传统名菜之一.因肉末贴在粉丝上,形似蚂蚁爬在树枝上而得名.这道菜具体的历史,已不 ...
- Robberies 杭电
可怜的POIUYTREWQ最近想买下dota2的商品,但是手头缺钱.他想起了之前看过的一部大片,觉得抢银行也许是个不错的选择.他认为,坏人被抓是因为没有预先规划.于是他在之前的几个月对各大银行进行了一 ...
- G - GCD and LCM 杭电
Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, sa ...
- 在数组添加元素时报错:IndexError: list index out of range
今天第一次发随笔还有许多不足之处,欢迎评论!!! 最近在写一个成语接龙的小游戏,结果在数组添加元素时报错:IndexError: list index out of range 源码: import ...
- Java优秀教程
1.java中局部变量是在栈上分配的: 2.数组是储存在堆上的对象,可以保存多个同类型变量: 3.在Java语言中,所有的变量在使用前必须声明. 4.局部变量没有默认值,所以局部变量被声明后,必须经过 ...
- Laravel - 基础
1.使用 composer 创建项目 composer create-project --prefer-dist laravel/laravel blog 报错1 [ErrorException]pr ...
- [Asp.Net Core] Blazor Server Side 项目实践 - 切换页面时保留状态
前言: 这是 项目实践系列 , 算是中高级系列博文, 用于为项目开发过程中不好解决的问题提出解决方案的. 不属于入门级系列. 解释起来也比较跳跃, 只讲重点. 因为有网友的项目需求, 所以提前把这些解 ...
- Java多台中成员访问特点
多态中的成员访问特点: A:成员变量 编译看左边,运行看左边 B:构造方法 创建子类对象的时候,访问父类的构造方法,对父类的数据进行初始化 C:成员方法 编译看左边,运行看右边.//因为调用对象时,子 ...
- AWR发现TOP Event log file sequential read
对客户DB进行巡检,发现TOP EVENT是LOG FILE Sequential read 等待事件说明 https://www.xuebuyuan.com/zh-hant/1743045.html ...