java  --  swt  - -  计算器

环境搭建 
安装java、eclipse、以及对应的swt插件。

开始工程

  • 建立工程: 
    在java下建立一个在其他 —- WindowsBuilder —- SWTdesigner —- SWT/JFrame Java Project。建立好以后运行将得到一个swt窗口。

  • 设计界面 : 
    点击代码下的Design选项卡 —- 拖动一个布局到窗口里 —- 然后自定义界面 
    如果没有发现Design选项卡的话在代码编辑窗口内右击 选择打开方式为: Windows Builder edit

  • 码代码 : 
    获取框里的文本:text.getText(); 
    设置文本:text.setText(); 
    很简单的函数。

Question & BUG 
  TODO 处理按下 1 后显示 01 的问题 
  FIXME 按下后变为0.1 
  FIXME 直接按下 . 的话前面没有变为 0 . 
  TODO 字符串 转 double 
  TODO 处理浮点数运算后结果多余显示的“.0”

    直接复制代码或把代码放到不同的计算器中运行的话可能hi出现一大片红色的错误,是因为在新的环境中没有把需要的包给导入。

需要在项目上右击 —- 属性 —- 然后一步步把需要的包导入,很多简单,不懂就多试试。

    在java中运算浮点数的话会得不到准确的值是因为计算器中是用二进制来存数据的,所以一个小数在计算机中会是无限循环的。

要处理这个BUG的话就需要使用java的BigDecimal类。

怎么把左上角的图标自定义? 
——–在Design里点击窗口、在左边的属性框里找到Image。

怎么设置文本框里的文本出现在后边? 
——也是在属性框里找到Style 找到 align,设置。

怎么把工作空间导入eclipse? 
——–在工作空间那里(如果没有这个窗口的话,在窗口那里找到它,把它显示出来)右击导入 —- General —- 现有项目到工作空间 —- 选择根目录 —- 选项目文件夹的上层文件夹 —- Finish

package com.java;
 * 计算器
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.wb.swt.SWTResourceManager;

public class Test {

    protected Shell shell;
    private Text text;

    private int op = 0;
    private double num1;
    private double num2;
    private static final int op_ADD =1;
    private static final int op_SUB =2;
    private static final int op_MUL =3;
    private static final int op_DIV =4;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            Test window = new Test();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //TODO 把文本设置在文本框的右边:在 text 属性的 style 的 align 里设置。
    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setImage(SWTResourceManager.getImage("E:\\Design\\nexus4full(96  96)\\circle.png"));
        shell.setSize(240, 297);
        shell.setText("SWT Application");
        shell.setLayout(new GridLayout(4, false));

        text = new Text(shell, SWT.BORDER | SWT.RIGHT);
        GridData gd_text = new GridData(SWT.FILL, SWT.FILL, true, false, 4, 1);
        gd_text.heightHint = 36;
        gd_text.widthHint = 424;
        text.setLayoutData(gd_text);

        Button button = new Button(shell, SWT.NONE);
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText(text.getText() + "7");
                if (text.getText().startsWith("0")){
                    text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ;
                }
            }
        });
        GridData gd_button = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_button.heightHint = 37;
        gd_button.widthHint = 51;
        button.setLayoutData(gd_button);
        button.setText("7");

        Button button_1 = new Button(shell, SWT.NONE);
        button_1.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText(text.getText() + "8");
                if (text.getText().startsWith("0")){
                    text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ;
                }
            }
        });
        GridData gd_button_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_button_1.heightHint = 37;
        gd_button_1.widthHint = 51;
        button_1.setLayoutData(gd_button_1);
        button_1.setText("8");

        Button button_2 = new Button(shell, SWT.NONE);
        GridData gd_button_2 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_button_2.heightHint = 37;
        gd_button_2.widthHint = 51;
        button_2.setLayoutData(gd_button_2);
        button_2.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText(text.getText() + "9");
                if (text.getText().startsWith("0")){
                    text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ;
                }
            }
        });
        button_2.setText("9");

        Button button_10 = new Button(shell, SWT.NONE);
        GridData gd_button_10 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_button_10.widthHint = 46;
        gd_button_10.heightHint = 40;
        button_10.setLayoutData(gd_button_10);
        button_10.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                op = op_ADD;
                num1 =Double.parseDouble (text.getText() );
                text.setText("0");
            }
        });
        button_10.setText("+");

        Button button_3 = new Button(shell, SWT.NONE);
        button_3.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText(text.getText() + "4");
                if (text.getText().startsWith("0")){
                    text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ;
                }
            }
        });
        GridData gd_button_3 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_button_3.heightHint = 37;
        gd_button_3.widthHint = 51;
        button_3.setLayoutData(gd_button_3);
        button_3.setText("4");

        Button button_4 = new Button(shell, SWT.NONE);
        button_4.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText(text.getText() + "5");
                if (text.getText().startsWith("0")){
                    text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ;
                }
            }
        });
        GridData gd_button_4 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
        gd_button_4.heightHint = 33;
        button_4.setLayoutData(gd_button_4);
        button_4.setText("5");

        Button button_5 = new Button(shell, SWT.NONE);
        button_5.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText(text.getText() + "6");
                if (text.getText().startsWith("0")){
                    text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ;
                }
            }
        });
        GridData gd_button_5 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
        gd_button_5.heightHint = 35;
        button_5.setLayoutData(gd_button_5);
        button_5.setText("6");

        Button button_11 = new Button(shell, SWT.NONE);
        button_11.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                op = op_SUB ;
                num1 =Double.parseDouble (text.getText() );
                //num1 = Integer.parseInt ( text.getText() );
                text.setText("0");
            }
        });
        button_11.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
        button_11.setText("-");

        Button button_6 = new Button(shell, SWT.NONE);
        button_6.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.NORMAL));
        button_6.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {

                //TODO 处理按下 1 后显示 01 的问题
                text.setText (text.getText() + "1");
                if (text.getText().startsWith("0")){
                    text.setText ( text.getText().substring (1, text.getText().length( ) ) ) ;
                }
                //FIXME 按下后变为0.1
//                else if(text.getText().startsWith("0.")){
//                    text.setText ( text.getText() +"1") ;
//                }
            }
        });
        GridData gd_button_6 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
        gd_button_6.heightHint = 37;
        gd_button_6.widthHint = 51;
        button_6.setLayoutData(gd_button_6);
        button_6.setText("1");

        Button button_7 = new Button(shell, SWT.NONE);
        button_7.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText(text.getText() + "2");
                if (text.getText().startsWith("0")){
                    text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ;
                }
            }
        });
        button_7.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
        button_7.setText("2");

        Button button_8 = new Button(shell, SWT.NONE);
        button_8.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText(text.getText() + "3");
                if (text.getText().startsWith("0")){
                    text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ;
                }
            }
        });
        button_8.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
        button_8.setText("3");

        Button button_12 = new Button(shell, SWT.NONE);
        button_12.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                op = op_MUL;
                num1 =Double.parseDouble (text.getText() );
                //num1 = Integer.parseInt ( text.getText() );
                text.setText("0");
            }
        });
        button_12.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
        button_12.setText("\u00D7");

        Button btnC = new Button(shell, SWT.NONE);
        btnC.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText("0");
            }
        });
        btnC.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
        btnC.setText("C");

        Button button_9 = new Button(shell, SWT.NONE);
        button_9.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText(text.getText() + "0");
//                if (text.getText().startsWith("0")){
//                    text.setText ( text.getText().substring ( 1, text.getText().length( ) ) ) ;
//                }
            }
        });
        GridData gd_button_9 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
        gd_button_9.heightHint = 37;
        gd_button_9.widthHint = 51;
        button_9.setLayoutData(gd_button_9);
        button_9.setText("0");

        Button button_14 = new Button(shell, SWT.NONE);
        button_14.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                    text.setText (text.getText() + ".");

                //FIXME 直接按下  .  的话前面没有变为 0 .

            }
        });
        button_14.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
        button_14.setText(".");

        Button button_13 = new Button(shell, SWT.NONE);
        button_13.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                op = op_DIV;
                num1 =Double.parseDouble (text.getText() );
                //num1 = Integer.parseInt( text.getText() );
                text.setText("0");
            }
        });
        button_13.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
        button_13.setText("\u00F7");

        Button button_15 = new Button(shell, SWT.NONE);
        button_15.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                double  result ;
                //TODO   字符串 转 double
                num2 =Double.parseDouble ( text.getText() );
                switch ( op) {
                case op_ADD:
                    result = num1+num2;
                    text.setText( result +"");

                    //FIXME java中计算浮点数的时候不精确,如:0.3 * 3
                    //需要使用 java 的大数类 BigDecimal。

                    //TODO 处理浮点数运算后结果多余显示的“.0”
                    if (text.getText().endsWith("0"))
                        text.setText(text.getText().substring(0,text.getText().length()-2));
                    break;
                case op_SUB:
                     result = num1 - num2;
                    text.setText( result +"");
                    if (text.getText().endsWith("0"))
                        text.setText(text.getText().substring(0,text.getText().length()-2));
                    break;
                case op_MUL:
                    result = num1 * num2;
                    text.setText( result +"");
                    if (text.getText().endsWith("0"))
                        text.setText(text.getText().substring(0,text.getText().length()-2));
                    break;
                case op_DIV:
                    result = num1 / num2;
                    text.setText( result +"");
                    if (text.getText().endsWith("0"))
                        text.setText(text.getText().substring(0,text.getText().length()-2));
                    break;
                default:
                    break;
                }
            }
        });
        GridData gd_button_15 = new GridData(SWT.FILL, SWT.FILL, false, false, 4, 1);
        gd_button_15.heightHint = 34;
        button_15.setLayoutData(gd_button_15);
        button_15.setText("=");

    }

}

接下来要做的就是把它给打包,让他脱离eclipse运行了,现在还什么都不懂,加油!!

eclipse --> extport --> runable jar --> ....--> finish.
新建一个bat文件,编辑--> java -jar Calcultor.jar.
用bat的方式来运行。

参考:

http://scorpiomiracle.iteye.com/blog/630358

Java SWT 做计算器。的更多相关文章

  1. java SWT/Rap 计算器版本2(键盘鼠标兼容)

    package cn.lesaas.nof.rwtswt.ui.dialog; import java.math.BigDecimal; import org.eclipse.swt.SWT;impo ...

  2. Java 浅做计算器

    package www.nihao; import java.util.Scanner; public class counter { public static void main(String[] ...

  3. paip.java swt 乱码问题解决

    paip.java swt 乱码问题解决 看累挂,Dfile.encoding是gbk的.. 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专 ...

  4. java SWT嵌入IE,SafeArray .

    java SWT嵌入IE,SafeArray );    // Create a by ref variant    Variant variantByRef = new Variant(pVaria ...

  5. 使用 jquery 的 上传文件插件 uploadify 3.1 配合 java 来做一个简单的文件上次功能。并且在界面上有radio 的选择内容也要上传

    使用 jquery 的 上传文件插件 uploadify 3.1 配合 java 来做一个简单的文件上次功能.并且在界面上有radio 的选择内容也要上传 uploadify 插件的 下载和文档地址  ...

  6. 学java可以做些什么

    学java可以做些什么 对于很多新手来说,刚开始接触Java会很迷惘,不知道Java可以做什么.其实Java 可以做的东西太多了,手机游戏.中间件.软件.网站,电脑游戏,以及现在流行的安卓手机app等 ...

  7. Java课程设计-计算器 郑子杰(201521123021)

    1.团队课程设计博客链接 http://www.cnblogs.com/I-love-java/p/7058752.html 2.个人负责模块或任务说明 ①图形界面的初始化 ②图形界面的排版设计 ③主 ...

  8. 第二次作业利用java语言编写计算器进行四则运算

    随着第一次作业的完成,助教 牛老师又布置了第二次作业:用java语言编写一个程序然后进行四则运算用户用键盘输入一个字符来结束程序显示统计结果.一开始看到这个题目我也着实吓了一跳 因为不知道如何下手而且 ...

  9. Java实现GUI计算器【代码】

    这几天用java写了一个计算器程序,开始写的时候原本只是想实现一下GUI(这个是直接读三个字母还是什么“固椅”的发音)界面,熟悉一下Java GUI 编程,为Java期末大作业练练手,本以为代码不会很 ...

随机推荐

  1. ASP.NET的简单与面向对象开发

    ASP.NET开发,一开始是为了超赶时间完成任务,只能把功能实现即可.如下面一个功能,在网页中有一个铵钮,用户点一点切换网页的图片,再点一点又切换回来.我们要怎样做?在铵钮事件中去变更图片的路径即可. ...

  2. 批处理安装Windows服务,提示"InstallUtil.exe"不是内部命令也不是外部命令解决方式

    今天在测试一个C#写的windows服务的时候,在用bat进行调用cmd安装的时候, cd C:\Windows\Microsoft.NET\Framework\v2.0.50727 InstallU ...

  3. php学习笔记:自定义函数的调用

    PHP内置了超过1000个函数,因此函数使得PHP成为一门非常强大的语言.大多数时候我们使用系统的内置函数就可以满足需求,但是自定义函数通过将一组代码封装起来,使代码进行复用,程序结构与逻辑更加清晰. ...

  4. 亲们! 首次见面! 带来不适!多多见谅!--------->>Bank系统

    亲们!您们好! 讲一下Bank系统的做法: 01.首先创建一个Card类 using System; using System.Collections.Generic; using System.Li ...

  5. ahjesus ubuntu10.4安装ruby2.1.1

    sudo apt-get install python-software-properties sudo apt-add-repository ppa:brightbox/ruby-ng sudo a ...

  6. online judge(ACM) 的设计与分析 (有c#demo)

    ACM.OJ,算法在线判题系统. 帮朋友完成毕业设计而写的,软件环境windows系统,语言是C# winform(因为我不熟悉asp.net,现在暂用winform写的demo). 看了下其他相关论 ...

  7. [Angularjs]视图和路由(四)

    写在前面 关于angularjs的路由的概念基本上这篇就要结束了,通过学习,以及在实际项目中的实践,还是比较容易上手的.自己也通过angularjs做了一个在app上的一个模块,效果还是可以的. 系列 ...

  8. 转:NLog之:文件类型目标(File target)

    转:http://www.cnblogs.com/RitchieChen/archive/2012/07/16/2594308.html 英文原文[http://nlog-project.org/wi ...

  9. 2015年第11本:代码整洁之道Clean Code

    前一段时间一直在看英文小说,在读到<Before I fall>这本书时,读了40%多实在看不下去了,受不了美国人啰啰嗦嗦的写作风格,还是读IT专业书吧. 从5月9日开始看<代码整洁 ...

  10. Spring(九)Spring对事务的支持

    一.对事务的支持 事务:是一组原子操作的工作单元,要么全部成功,要么全部失败 Spring管理事务方式: JDBC编程事务管理:--可以控制到代码中的行 可以清楚的控制事务的边界,事务控制粒度化细(编 ...