java applet
1.使用Myeclipse 6.5创建一个web工程,命名为“jatest”,创建包为com
2.导入Jfreechart包jfreechart-1.0.6.jar和jcommon-1.0.10.jar。右键点击工程名,新建applet。
package com; import java.applet.Applet; import javax.swing.JApplet; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.ChartPanel; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.chart.plot.PlotOrientation; import java.awt.Container; import java.awt.Dimension; public class AppletTest extends JApplet { /** * Constructor of the applet. * * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. */ public AppletTest() { super(); } /** * Called by the browser or applet viewer to inform * this applet that it is being reclaimed and that it should destroy * any resources that it has allocated. The <code>stop</code> method * will always be called before <code>destroy</code>. <p> * * A subclass of <code>Applet</code> should override this method if * it has any operation that it wants to perform before it is * destroyed. For example, an applet with threads would use the * <code>init</code> method to create the threads and the * <code>destroy</code> method to kill them. <p> */ public void destroy() { // Put your code here } /** * Returns information about this applet. An applet should override * this method to return a <code>String</code> containing information * about the author, version, and copyright of the applet. <p> * * @return a string containing information about the author, version, and * copyright of the applet. */ public String getAppletInfo() { return "This is my default applet created by Eclipse"; } /** * Called by the browser or applet viewer to inform * this applet that it has been loaded into the system. It is always * called before the first time that the <code>start</code> method is * called. <p> * * A subclass of <code>Applet</code> should override this method if * it has initialization to perform. For example, an applet with * threads would use the <code>init</code> method to create the * threads and the <code>destroy</code> method to kill them. <p> */ public void init() { // Put your code here DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(1.0, "Row 1", "Column 1"); dataset.addValue(5.0, "Row 1", "Column 2"); dataset.addValue(3.0, "Row 1", "Column 3"); dataset.addValue(2.0, "Row 2", "Column 1"); dataset.addValue(3.0, "Row 2", "Column 2"); dataset.addValue(2.0, "Row 2", "Column 3"); JFreeChart chart = ChartFactory.createBarChart( "Bar Chart Demo", // chart title "Category", // domain axis label "Value", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips? false // URLs? ); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(500, 270)); Container content = getContentPane(); content.add(chartPanel); } /** * Called by the browser or applet viewer to inform * this applet that it should start its execution. It is called after * the <code>init</code> method and each time the applet is revisited * in a Web page. <p> * * A subclass of <code>Applet</code> should override this method if * it has any operation that it wants to perform each time the Web * page containing it is visited. For example, an applet with * animation might want to use the <code>start</code> method to * resume animation, and the <code>stop</code> method to suspend the * animation. <p> */ public void start() { // Put your code here } /** * Called by the browser or applet viewer to inform * this applet that it should stop its execution. It is called when * the Web page that contains this applet has been replaced by * another page, and also just before the applet is to be destroyed. <p> * * A subclass of <code>Applet</code> should override this method if * it has any operation that it wants to perform each time the Web * page containing it is no longer visible. For example, an applet * with animation might want to use the <code>start</code> method to * resume animation, and the <code>stop</code> method to suspend the * animation. <p> */ public void stop() { // Put your code here } }
3.右键Run As-Java Applet执行,可以看到报表图形,显示正常
4.在webroot下新建文件夹applet/com 编译后,将tomcat下的\webapps\jatest\applet\com AppletTest.class文件复制到webroot建立的子目录applet/com下;将jfreechart-1.0.6.jar,jcommon-1.0.10.jar 再复制到webroot的子目录applet/com下
5.修改HTML文件StartPageForAppletTest.html为index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My applet 'AppletTest' starting page</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <applet codebase="/jatest/applet/" code="com.AppletTest.class" archive="jfreechart-1.0.6.jar,jcommon-1.0.10.jar" name="AppletTest" width="320" height="240"> </applet> </body> </html>
codebase 从根目录开始/webproject/folder 下面存储code和archive
code src下编译出来的class 带.层次的class文件 code要建立同样层次的目录
archive 引用的包
name 类名
6.运行tomcat,地址栏输入:
http://localhost:8080/jatest/index.html
就可以看到结果
如果觉得在webroot下新建文件夹applet/com/麻烦,也可以在src里将做好的applet的java文件打成jar包放入webroot下,相应的修改codebase和archive就可以了。