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;
}
java applet初探之计算器的更多相关文章
- java sound初探
网上关于java sound的正规资源讲解的非常好,本文不再给出示例,主要提供一些好的资源,并说说我的一些理解,用于形成对java sound的整体认识. 一.几个词汇 TTS:text-to-spe ...
- Java—Applet
1 Applet的定义 Applet是Java语言编写的,无法独立运行,但可以嵌入到网页中执行.它扩展了传统的编程结构和方法,可以通过互联网发布到任何具有Java编译环境浏览器的个体计算机上. 如下 ...
- The differences between Java application and Java applet
在Java语言中,能够独立运行的程序称为Java应用程序(Application).Java语言还有另外一种程序--Applet程序.Applet程序(也称Java小程序)是运行于各种网页文件中,用于 ...
- Java Applet与Java Application的区别
转自:http://www.educity.cn/java/500609.html 在Java语言中,能够独立运行的程序称为Java应用程序(Application).Java语言还有另外一种程序-- ...
- Java Applet使用
问题描述: Java Applet使用 参考资料: http://docs.oracle.com/javase/tutorial/deployment/applet/depl ...
- Java Applet与Java Application的特点
java application是应用程序,用于桌面开发,java applet是小应用程序,一般嵌入到网页里运行.applet一般用于B/S页面上作为插件式的开发,而application主要是桌面 ...
- 在浏览器运行 java applet时遇到的一些问题及其解决方法
运行 java applet时提示:您的安全设置已阻止本地应用程序运行,如何解决?如下图所示 这时候通过设置java的安全级别就可以了. 控制面板->程序->Java->安全 将安全 ...
- 使用Java Applet在客户端解压缩,以及使用证书的意义
以前解压缩是用Java Applet在客户端解压缩,而且用户不知道这回事.但是现在Chrome不支持NP API了,所以不得不把Java去掉,然后在服务器里解压缩.风险在于,解压缩以后,传输到客户端的 ...
- Java Applet实现五子棋游戏
从谷歌的AlphaGo到腾讯的绝艺,从人脸识别到无人驾驶,从谷歌眼镜到VR的兴起,人工智能领域在不断的向前迈进,也在不断深入的探索.但背后错综复杂的技术和利益成本也是很多企业亟待解决的难题.对于人工智 ...
随机推荐
- Swift 简简单单实现手机九宫格手势密码解锁
原文:Swift 简简单单实现手机九宫格手势密码解锁 大家可以看到我之前的文章[HTML5 Canvas简简单单实现手机九宫格手势密码解锁] 本文是使用苹果语言对其进行了移植 颜色配色是拾取的支付宝的 ...
- Python_生成測试数据
本文出自:http://blog.csdn.net/svitter 生成1~10的随机数1000个: import random fp = open("test", 'w'); f ...
- C语言宏的高级应用
原文:C语言宏的高级应用 关于#和##在C语言的宏中,#的功能是将其后面的宏参数进行字符串化操作(Stringfication),简单说就是在对它所引用的宏变量通过替换后在其左右各加上一个双引号.比如 ...
- JVM监控概述(图文)
JVM内存分配概述 Jvm 内存分为:堆.非堆及直接内存三大块. 堆区分为年轻代和老年代,永生代属于非堆内存. 对象优先在Eden区分配 大对象直接进入老年代 长期存活的对象将进入老年代 class. ...
- MVC5 + EF6 + Bootstrap3-10
MVC5 + EF6 + Bootstrap3 (10) 数据查询页面 上一节:MVC5 + EF6 + Bootstrap3 (9) HtmlHelper用法大全(下) 源码下载:点我下载 目录 前 ...
- POJ3243 EXT-BSGS算法
需要解决问题之前,首先要做到POJ2417,我的解决问题的方法:http://blog.csdn.net/wyfcyx_forever/article/details/40538515 如今来看这个问 ...
- Your build host version of Xamarin.IOS (release NO.)is too recent to work with the IOS designer
Encounted such error in VS after I update the xamarin at Mac side.Here is the solution for u to refe ...
- extern用法汇总
extern 在源文件A里定义的函数,在其他源文件中是看不见的(即不能訪问).为了在源文件B里能调用这个函数,应该在B的头部加上一个外部声明: extern 函数原型: 这样,在源文件B里也能够调 ...
- Maven包装过程中跳过测试
在具体项目,步骤需要跳过假设检验,应采取skipTests此属性设置为true. <pre name="code" class="html">< ...
- Libcurl最初的实现tfp上传和下载功能
研究报告指出的目标是使用libcurl实现ftp文件上传和下载功能 一.Libcurlde简要 Libcurl的而且易于使用的利用url进行文件传输的库. , libcurl当前支持DICT, FIL ...