有人说,RCP一定会变得非常流行。因为RCP,可以开发界面象Eclipse这样漂亮的桌面程序。那就来做一个JFreeChat的示例吧。
一、下载JFreeChart包
1.http://www.jfree.org/jfreechart/
2.用到的包:(注,我的开发环境是eclipse3.2)
jcommon-1.0.10.jar
jfreechart-1.0.6.jar
jfreechart-1.0.6-experimental.jar
jfreechart-1.0.6-swt.jar
swtgraphics2d.jar
二、新建项目
在Eclipse下如何建立plug-in的内容在Google上一搜一大把,有些人会觉得乏味,但是没关系,这只是一个快速的起步。下面大致说一下。有不明白的,可以参考http://www.vogella.de/articles/RichClientPlatform/article.html#using_jars,非常的详细
1、建完项目后记得把上面提到的JFreeChart包加入的项目“Propertires”的“Java Build Path”中。
2、还需要在项目的META-INF/MANIFEST.MF文件的“runtime”中加入以上的包,不然运行时会说找不相关的类,如下图
三、建立View
建View在上面文档http://www.vogella.de/articles/RichClientPlatform/article.html#using_jars中5.2.Add a view to your application有讲,我在这里大致上几个图说明一下。
1.点击配置文件的“extensions”,选择"org.eclipse.views",右键,》"new"》"view",
2.点击右边class*.;链接,来创建新的类
3.此类的代码如下
importjava.awt.Font;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.widgets.Composite;
importorg.eclipse.ui.part.ViewPart;
importorg.jfree.chart.ChartFactory;
importorg.jfree.chart.JFreeChart;
importorg.jfree.chart.plot.PiePlot;
importorg.jfree.data.general.DefaultPieDataset;
importorg.jfree.data.general.PieDataset;
importorg.jfree.experimental.chart.swt.ChartComposite;
publicclassMyNewViewextendsViewPart{
publicMyNewView(){
//TODOAuto-generatedconstructorstub
}
privatestaticPieDatasetcreateDataset(){
DefaultPieDatasetdataset=newDefaultPieDataset();
dataset.setValue("猪肉",newDouble(43.2));
dataset.setValue("牛肉",newDouble(10.0));
dataset.setValue("猪肉",newDouble(27.5));
dataset.setValue("羊肉",newDouble(17.5));
dataset.setValue("鸡肉",newDouble(11.0));
dataset.setValue("鸭肉",newDouble(19.4));
returndataset;
}
/***//**
*Createsachart.
*
*@paramdataset
*thedataset.
*
*@returnAchart.
*/
privatestaticJFreeChartcreateChart(PieDatasetdataset){
JFreeChartchart=ChartFactory.createPieChart("肉食品消费统计图示例",//chart
//title
dataset,//data
true,//includelegend
true,false);
PiePlotplot=(PiePlot)chart.getPlot();
plot.setSectionOutlinesVisible(false);
plot.setLabelFont(newFont("SansSerif",Font.PLAIN,12));
plot.setNoDataMessage("没有统计数据");
plot.setCircular(false);
plot.setLabelGap(0.02);
returnchart;
}
publicvoidcreatePartControl(Compositeparent){
JFreeChartchart=createChart(createDataset());
finalChartCompositeframe=newChartComposite(parent,SWT.NONE,
chart,true);
}
/***//**
*Passingthefocusrequesttotheviewer'scontrol.
*/
publicvoidsetFocus(){
}
}
4.修改Perspective类,使程序能够运行
importorg.eclipse.ui.IPageLayout;
importorg.eclipse.ui.IPerspectiveFactory;
publicclassPerspectiveimplementsIPerspectiveFactory{
publicvoidcreateInitialLayout(IPageLayoutlayout){
layout.addView("jfreechatrcp.MyNewView",IPageLayout.TOP,IPageLayout.RATIO_MAX,IPageLayout.ID_EDITOR_AREA);
}
}
5.运行程序
META-INF/MANIFEST.MF》》Launch asEclipse application
四、结果
源代码jfreechatRCP.rar,请自行下载jfreechart包
参考文献
http://www.vogella.de/articles/RichClientPlatform/article.html#firstplugin
http://www.vogella.de/articles/EclipseJFreeChart/article.html