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的兴起,人工智能领域在不断的向前迈进,也在不断深入的探索.但背后错综复杂的技术和利益成本也是很多企业亟待解决的难题.对于人工智 ...
随机推荐
- Responsive Design in 3 Steps
Responsive web design is no doubt a big thing now. If you still not familiar with responsive design, ...
- Objective-c中的单例
单例是指静态分配的实例,就是只开辟一块内存,不会重新开辟内存,而 iphone sdk 中全是这种实例,例如[UIApplication sharedApplication] 返回一个指向代表应用程序 ...
- C语言宏的高级应用
原文:C语言宏的高级应用 关于#和##在C语言的宏中,#的功能是将其后面的宏参数进行字符串化操作(Stringfication),简单说就是在对它所引用的宏变量通过替换后在其左右各加上一个双引号.比如 ...
- Cocos2d-x 2.3.3版本 FlappyBird
Cocos2d-x 2.3.3版本 FlappyBird 本篇博客基于Cocos2d-x 2.3.3, 介绍怎样开发一款之前非常火的一款游戏FlappyBird.本篇博客内容大纲例如以下: 1 ...
- C#获取本机所有用户名
using System.DirectoryServices; using System.Runtime.InteropServices; (需要添加引用) [StructLayout(LayoutK ...
- 原生JS的DOM节点操作
DOM(Document Object Model/文档对象模型)是针对HTML和XML文档的一个API.DOM节点树:在文档中出现的空格.回车.标签.注释.文本.doctype.标签等都属于DOM节 ...
- UC编程:字符读取与行读取
字符读取函数的应用 下面的演示程序实现从/etc/passwd文件中提取用户名,打印到屏幕上并保存在copyname.txt文件中 使用的函数是getc().putc().putchar() [c] ...
- 系统预定义委托与Lambda表达式
NET中那些所谓的新语法之三:系统预定义委托与Lambda表达式 开篇:在上一篇中,我们了解了匿名类.匿名方法与扩展方法等所谓的新语法,这一篇我们继续征程,看看系统预定义委托(Action/Fun ...
- Error with mysqld_safe
出处:http://bugs.mysql.com/bug.php?id=18403 Description: - I downloaded the binary file “Standard 5.0. ...
- C#中使用消息队列RabbitMQ
在C#中使用消息队列RabbitMQ 2014-10-27 14:41 by qy1141, 745 阅读, 2 评论, 收藏, 编辑 1.什么是RabbitMQ.详见 http://www.rabb ...