swt中使用系统的资源文件,比如图片,跟在web中方法是类似的,我们都需要先找到这个资源,之后才能谈得上去使用它。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @Test public void systemResouce(){ Display display= new Display(); Shell topShell= new Shell(display); topShell.setSize( 800 , 500 ); topShell.setLayout( new GridLayout()); Image image = new Image(display, this .getClass().getResourceAsStream( "tray.gif" )); Label label= new Label(topShell, SWT.NONE); label.setImage(image); topShell.open(); while (!topShell.isDisposed()){ if (!display.readAndDispatch()) { display.sleep(); } } //父类对象废弃后其子类和关联的菜单等也废弃了, //但是(image。color。font)[仅仅只有new出来的]要手动废弃 display.dispose(); image.dispose(); } |
运行效果: