本文目录一览:
- 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/n/152550.html