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;

}

上一篇:"use strict"详解


下一篇:php单链表实现的代码