java applet初探之计算器

这里是用java写的一个计算器,用appllet的方式在浏览器中运行,如果电脑上装有java运行环境jre就能一试。将html代码保存为*.html(名称能够自定),applettest编译为class文件放在同一文件夹下就能运行了。下面给出代码

applettest.html:


<html>
<head>
    <title>CalculatorApplet</title>
    </head>
    <body>
    <h1>CalculatorApplet</h1>
    <h2>by:Carp_and_Wind</h2>
    <applet code="applettest.class" width="400" height="400">
    if your browser support java you would see javaapplet here.
    </applet>
    <br />
    <a href="http://blog.csdn.net/Carp_and_Wind">My blog here to see the source code.</a> 
    </body>
</html>




applettest.java:



import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
public class applettest extends JApplet 
{


/**
* @param args
*/
public void init() {
EventQueue.invokeLater(new Runnable(){
public void run()
{
CalculatorPanel panel = new CalculatorPanel();//加入组件
     add(panel);
}
});
}


}
/**
 * A panel with calculator buttons and a result display.
 */
class CalculatorPanel extends JPanel//这个组建当中还能嵌套别的
{
   public CalculatorPanel()
   {
      setLayout(new BorderLayout());


      result = 0;
      lastCommand = "=";
      start = true;//初始化初始显示


      // add the display


      display = new JButton("0");
      clear=new JButton("Clear");
      display.setEnabled(false);
      add(display, BorderLayout.NORTH);
      ActionListener clearall = new ClearAction();
      clear.addActionListener(clearall);
      add(clear,BorderLayout.SOUTH);
      ActionListener insert = new InsertAction();
      ActionListener command = new CommandAction();
      
      // add the buttons in a 4 x 4 grid


      panel = new JPanel();
      panel.setLayout(new GridLayout(4, 4));


      addButton("7", insert);//注意addbutton为自定义方法
      addButton("8", insert);
      addButton("9", insert);
      addButton("/", command);


      addButton("4", insert);
      addButton("5", insert);
      addButton("6", insert);
      addButton("*", command);


      addButton("1", insert);
      addButton("2", insert);
      addButton("3", insert);
      addButton("-", command);


      addButton("0", insert);
      addButton(".", insert);
      addButton("=", command);
      addButton("+", command);


      add(panel, BorderLayout.CENTER);
   }


   /**
    * Adds a button to the center panel.
    * @param label the button label
    * @param listener the button listener
    */
   private void addButton(String label, ActionListener listener)
   {
      JButton button = new JButton(label);
      button.addActionListener(listener);
      panel.add(button);
   }


   /**
    * This action inserts the button action string to the end of the display text.
    */
   private class ClearAction implements ActionListener
   {
  public void actionPerformed(ActionEvent event)
  {
  start=true;
  display.setText("0");
  result = 0;
  lastCommand = "=";
  }
   }
   private class InsertAction implements ActionListener
   {
      public void actionPerformed(ActionEvent event)//event系统提供
      {
         String input = event.getActionCommand();
         if (start)//why use start ?
         {
            display.setText("");//大概是初始化需要吧
            start = false;
         }
         display.setText(display.getText() + input);
      }
   }


   /**
    * This action executes the command that the button action string denotes.
    */
   private class CommandAction implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         String command = event.getActionCommand();


         if (start)//start什么用途?原来是用来表示第一计算顺序,这尼玛。。。够费心的!
         {
            if (command.equals("-"))
            {
               display.setText(command);
               start = false;
            }
            else lastCommand = command;
         }
         else
         {
            calculate(Double.parseDouble(display.getText()));
            lastCommand = command;
            start = true;
         }
      }
   }


   /**
    * Carries out the pending calculation.
    * @param x the value to be accumulated with the prior result.
    */
   public void calculate(double x)
   {
      if (lastCommand.equals("+")) result += x;
      else if (lastCommand.equals("-")) result -= x;
      else if (lastCommand.equals("*")) result *= x;
      else if (lastCommand.equals("/")) result /= x;
      else if (lastCommand.equals("=")) result = x;
      display.setText("" + result);
   }
   private JButton clear;
   private JButton display;
   private JPanel panel;
   private double result;
   private String lastCommand;
   private boolean start;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值