zoukankan      html  css  js  c++  java
  • java小计算器

    只支持鼠标操作的。

    打包好的源代码下载地址:

    http://115.com/file/anc92nmj#
    calculator.zip

    /**
    * @(#)CaculatorFrame.java
    *
    *
    *
    @author huangliangming
    * @说明: 实现了基本功能,只支持鼠标操作

    *
    @version 1.00 2012/4/3
    */
    package calculator;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class CaculatorFrame extends JFrame
    {
    private JButton jButton;
    private JTextField textField;
    int w = 242,
    h = 260;
    int buttonSizeX = 45,
    buttonSizeY = 35;
    static Double num1=0.0;
    static String cal;
    static boolean isANext = false;
    public CaculatorFrame()
    {
    Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
    int swidth = (int)screensize.getWidth();
    int sheight = (int)screensize.getHeight();
    setBounds(swidth/2-w/2,sheight/2-h/2,w,h);
    setResizable(false);
    setVisible(true);
    this.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    CaculatorFrame.this.windowClosed();
    }
    });
    initButton();
    repaint();
    }

    public void initButton()
    {
    setLayout(null);
    textField = new JTextField("0");
    add(textField);
    textField.setBounds(2,2,w-10,30);
    textField.setHorizontalAlignment(JTextField.RIGHT);
    textField.setEditable(false);
    textField.setBackground(Color.WHITE);
    //画数字键
    JPanel numPanel = new JPanel();
    numPanel.setLayout(new GridLayout(4,4,5,5));
    String str = "789+456-123x0.c/";
    for (int i=0; i<16; i++)
    {
    jButton = new JButton(str.substring(i,i+1));
    numPanel.add(jButton);
    if (i == 3 || i == 7 || i == 11 || i == 15)
    {
    jButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    cal = e.getActionCommand();
    try
    {
    num1 = Double.parseDouble(textField.getText());
    if (num1 == 0.0)
    {
    textField.setText("");
    }
    }
    catch (NumberFormatException event)
    {
    textField.setText("");
    }
    textField.setText(e.getActionCommand());
    }
    });
    }
    else if (i == 14)
    {
    jButton.addActionListener(new DelEvent(textField));
    }
    else
    {
    jButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    String text = textField.getText();
    String s = e.getActionCommand();
    System.out.println("text:"+text);
    System.out.println("s:"+s);
    if (isANext == true)
    {
    text = "";
    isANext = false;
    }
    try
    {
    double tmp = Double.parseDouble(text);
    System.out.println("tmp"+tmp);
    if (tmp == 0.0)
    {
    text = "";
    }
    }
    catch (NumberFormatException event)
    {
    text = "";
    }
    if (s.equals("."))
    {
    if (text.indexOf('.')>=0)
    {
    return;
    }
    else
    {
    textField.setText(text+s);
    return;
    }
    }
    textField.setText(text+s);
    }
    });
    }
    }
    add(numPanel);
    numPanel.setBounds(2,40,2+(buttonSizeX*4),50+buttonSizeY*4);

    JButton jButtonR = new JButton("=");
    add(jButtonR);
    jButtonR.setBounds(7+(buttonSizeX*4),80+buttonSizeY*3,buttonSizeX, 5+buttonSizeY);
    jButtonR.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    String result;
    double num2=0;
    JButton jB = (JButton)e.getSource();
    try
    {
    num2 = Double.parseDouble(textField.getText());
    }
    catch (NumberFormatException event)
    {
    textField.setText("");
    System.out.println("num2输入错误");
    }
    System.out.println(cal);
    if (cal.equals("+"))
    {
    result = String.valueOf(num1 + num2);
    }
    else if (cal.equals("-"))
    {
    result = String.valueOf(num1 - num2);
    }
    else if (cal.equals("x"))
    {
    result = String.valueOf(num1 * num2);
    }
    else if (cal.equals("/"))
    {
    result = String.valueOf(num1 / num2);
    }
    else
    {
    result = "";
    System.out.println("计算出错");
    }
    textField.setText(result);
    isANext = true;
    }
    });
    validate();
    }

    protected void windowClosed()
    {
    // Exit application.
    System.out.println("exited!");
    System.exit(0);
    }
    }

    class DelEvent implements ActionListener
    {
    private JTextField tField;
    DelEvent(JTextField textField)
    {
    tField = textField;
    }
    public void actionPerformed(ActionEvent e)
    {
    tField.setText("0");
    }
    }
    /**
    * @(#)Caculator.java
    *
    * Caculator application
    *
    *
    @author huangliangming
    *
    @version 1.00 2012/4/3
    */
    package calculator;

    public class Caculator
    {
    public static void main(String[] args)
    {
    CaculatorFrame caculatorFrame = new CaculatorFrame();
    // TODO, add your application code
    System.out.println("Hello World!");
    }
    }




    作者:涵曦www.hanxi.cc
    出处:hanxi.cnblogs.com
    GitHub:github.com/hanxi
    Email:im.hanxi@gmail.com
    文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

    《 Skynet 游戏服务器开发实战》

  • 相关阅读:
    下一座“金矿”:移动医疗的契机和风险
    android ViewStub简单介绍
    IT人员必须掌握的10项软技能
    Android ListView item 点击事件失效问题的解决
    前端之Android入门(3):MVC模式(上)
    Android 错误提示: Can't create handler inside thread that has not called Looper.prepare()
    Android 性能优化提示
    Android 学习资源
    元素水平对齐
    div垂直居中
  • 原文地址:https://www.cnblogs.com/hanxi/p/2435884.html
Copyright © 2011-2022 走看看