shell swt 样式_SWT之路:SWT图像显示
簡明現(xiàn)代魔法 -> Java編程語言 -> SWT之路:SWT圖像顯示
SWT之路:SWT圖像顯示
2009-10-03
程序演示
還是先用SWT Desiner創(chuàng)建界面程序。然后創(chuàng)建一個(gè)Display對象和Image對象,和一個(gè)GC對象。類org.eclipse.swt.graphics.GC是一個(gè)封裝了所有可執(zhí)行的繪圖操作的圖形上下文(Graphics Context)。然后以Display和圖片路徑創(chuàng)建Image對象,再調(diào)用gc.drawImage();就可以顯示圖片了。
程序代碼
package SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class showImages {
protected Shell shell;
public static Display myDisplay;
public static boolean internalCall = false;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
internalCall = true;
myDisplay = new Display();
try {
showImages window = new showImages();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents(myDisplay);
Image img = new Image(display, "images/3.jpg");
shell.open();
GC gc = new GC(shell);
gc.drawImage(img, 0, 0);
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
img.dispose();
if (internalCall) display.dispose();
}
/**
* Create contents of the window.
*/
protected void createContents(Display display) {
myDisplay = display;
shell = new Shell();
shell.setSize(520, 280);
shell.setText("圖像顯示");
}
}
一旦你創(chuàng)建了一個(gè)GC,你就有責(zé)任通過它的dispose方法釋放它的資源。一個(gè)由應(yīng)用程序創(chuàng)建的GC需要立即被繪制,然后盡快釋放掉。這是因?yàn)槊總€(gè)GC都需要一個(gè)底層的系統(tǒng)資源,而在某些操作系統(tǒng)中這些資源是稀缺的,像Win98就只允許同時(shí)創(chuàng)建五個(gè)GC對象。
總結(jié)
以上是生活随笔為你收集整理的shell swt 样式_SWT之路:SWT图像显示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: antd新增一行页码不正确_antd-T
- 下一篇: java 输出当月日历_java 实现打