本文目錄一覽:
- 1、java swt出錯,初學者求解決
- 2、java,ant,swt,eclipse還有好多,它們是什麼關係?
- 3、java有了Swing,為什麼還要使用SWT呢?
- 4、java SWT 加載文件內容
java swt出錯,初學者求解決
額。。我想問,你這寫法是什麼語言轉過來的,看着怪彆扭的,大小寫之類的全部都沒區分。錯誤的話,應該是,text.setText(null);這個不能設置為null,改成text.setText(“”);就可以了。
java,ant,swt,eclipse還有好多,它們是什麼關係?
Java是一門語言,ant和Eclipse是它的開發工具,前者基於命令行,後者是集成開發工具,不過前者也可以以插件形式集成到後者當中。SWT是由Eclipse所倡導的一門新的Java
圖形界面解決方案,並率先在Eclipse裏面使用到,相比於傳統的AWT和Swing有相當大的改進
java有了Swing,為什麼還要使用SWT呢?
由於在不同的操作系統下,提供的控件是不一樣的,AWT採用最小公約數的辦法,只提供所有操作系統都有的控件。但後來SUN改變了做法,在Swing里除了JFrame,JWinodows,JDialog(記不太清了,好像是這幾個)是調用本地操作系統的控件,其它JPanel,JButton之類的都是繪出來的,所以Swing在所有平台看起來都是一樣的外觀。這樣保持了外觀一致性,但犧牲了性能。 IBM更喜歡AWT的實現機制,做出了SWT,SWT採用的是最大公倍數的做法。SWT大部分都是用的本地操作系統的控件,一些在windows里有的控件可能在linux下沒有,對這種控件才採用自己繪製的方式。SWT採用類似JAVA虛擬機的方式,在不同的平台,有不同的開發包,我們寫的java代碼是一樣的,但不同平台下看起來外觀是不一樣的,但性能提升很高,據說和C++做的界面速度差不多:) 也許你會問哪種更好,引一名話:this is equivalent to asking whether a harmmer is better than a screw driver,of course ,a hammer wieldded with sufficient force can probably drive a screw into a wall ,and the butt of a screw can be used in a pinch to knock in a nail. However, a good carpenter keeps both harmer and screw drivers in her tool box and will use the tool that is appropriate for the job at hand. 個人感覺以前java做界面完全沒有優勢,從外觀到性能(我很喜歡Swing的look and feel,可以改變風格),SWT的出現改變了性能上的缺點,再加上JFace,及Eclipse的RCP,我還是傾向於用SWT。
java SWT 加載文件內容
樓主,你的代碼好多錯誤。幫你改了部分,主要還是看「 //這裡應該怎麼寫代碼」下面的代碼。java處理文件需要流的知識。我這裡用的Scanner需要jdk1.5以上版本才能用。你先運行看看吧
package com.swtjface;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Test {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(500, 375);
final Button button = new Button(shell, SWT.PUSH);
button.setBounds(0, 0, 150, 35);
button.setText(“打開文件”);
final Text text = new Text(shell, SWT.BORDER|SWT.MULTI);
text.setBounds(0, 40, 500, 375);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// 這裡應該怎麼寫代碼
FileDialog dlg = new FileDialog(shell, SWT.OPEN);
dlg.setText(“選擇一個TXT文件”);
dlg.setFilterExtensions(new String[] { “*.txt” });
dlg.setFilterNames(new String[] { “文本文件(*.txt)” });
String filePath = dlg.open();
System.out.println(filePath);
try {
Scanner in = new Scanner(new FileInputStream(new File(
filePath)));
while (in.hasNextLine()) {
text.append(in.nextLine() + “\r\n”);
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/152550.html