小弟在写一个类似QQ的程序,最近在写主界面的好友列表时遇见了问题.想写一个树型结构,就像MSN的好友列表,展开后前面是一个可以显示好友信息的button,后面是好友的名字.可是在swt下试了很多种方法也得不到满意的效果.后来就仿照着<Eclipes从入门到精通>上面,想用jface中的TableViewer来实现.可是从没用过jface,完全照着书上写也编译错误,实在不知道自己错在哪了.希望高手指点一下~~!这是我的主界面代码,基本上是copy书上的import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;public class Frame
{
private static TableViewer tableViewer; public static void main(String[] args)
{
Frame frame = new Frame();
frame.open();
} public void open()
{
final Display display = new Display();
final Shell shell = new Shell();
shell.setSize(300, 400);
shell.setLayout(new FillLayout()); createTableViewer(shell); tableViewer.setContentProvider(new TableViewerContentProvider());
tableViewer.setLabelProvider(new TableViewerLabelProvider());
tableViewer.setInput(new PeopleFactory().getContacts()); shell.open(); while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
} public static void createTableViewer(Shell parent)
{
tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.V_SCROLL
| SWT.FULL_SELECTION); Table table = tableViewer.getTable();
table.setHeaderVisible(false);
table.setLinesVisible(false); TableLayout tableLayout = new TableLayout();
table.setLayout(tableLayout); tableLayout.addColumnData(new ColumnWeightData(20));
new TableColumn(table, SWT.NONE).setText("image"); tableLayout.addColumnData(new ColumnWeightData(200));
new TableColumn(table, SWT.NONE).setText("information");
}
}这是提示的错误Thread [main] (Suspended (exception ClassNotFoundException))
URLClassLoader$1.run() line: 200
AccessController.doPrivileged(PrivilegedExceptionAction<T>, AccessControlContext) line: not available [native method]
Launcher$AppClassLoader(URLClassLoader).findClass(String) line: 188
Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: 306
Launcher$AppClassLoader.loadClass(String, boolean) line: 268
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: 251
Launcher$AppClassLoader(ClassLoader).loadClassInternal(String) line: 319
Frame.createTableViewer(Shell) line: 63
Frame.open() line: 44
Frame.main(String[]) line: 34 如果我把所有关于TableViewer的内容注释掉,只new一个新的TableViewer,也会提示这个错误.不知道是不是还需要导入什么库或者设置什么变量,希望哪位高手能帮助小弟一下~~~ 哦,对了,我用的是eclipse,写swt类用的是ve插件.也导入了jface,runtime,workbench库.还想问一下,如果不用jface,只用swt,能实现我说的那个功能吗?实在多谢了~~~~~~~!!!!
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;public class Frame
{
private static TableViewer tableViewer; public static void main(String[] args)
{
Frame frame = new Frame();
frame.open();
} public void open()
{
final Display display = new Display();
final Shell shell = new Shell();
shell.setSize(300, 400);
shell.setLayout(new FillLayout()); createTableViewer(shell); tableViewer.setContentProvider(new TableViewerContentProvider());
tableViewer.setLabelProvider(new TableViewerLabelProvider());
tableViewer.setInput(new PeopleFactory().getContacts()); shell.open(); while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
} public static void createTableViewer(Shell parent)
{
tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.V_SCROLL
| SWT.FULL_SELECTION); Table table = tableViewer.getTable();
table.setHeaderVisible(false);
table.setLinesVisible(false); TableLayout tableLayout = new TableLayout();
table.setLayout(tableLayout); tableLayout.addColumnData(new ColumnWeightData(20));
new TableColumn(table, SWT.NONE).setText("image"); tableLayout.addColumnData(new ColumnWeightData(200));
new TableColumn(table, SWT.NONE).setText("information");
}
}这是提示的错误Thread [main] (Suspended (exception ClassNotFoundException))
URLClassLoader$1.run() line: 200
AccessController.doPrivileged(PrivilegedExceptionAction<T>, AccessControlContext) line: not available [native method]
Launcher$AppClassLoader(URLClassLoader).findClass(String) line: 188
Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: 306
Launcher$AppClassLoader.loadClass(String, boolean) line: 268
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: 251
Launcher$AppClassLoader(ClassLoader).loadClassInternal(String) line: 319
Frame.createTableViewer(Shell) line: 63
Frame.open() line: 44
Frame.main(String[]) line: 34 如果我把所有关于TableViewer的内容注释掉,只new一个新的TableViewer,也会提示这个错误.不知道是不是还需要导入什么库或者设置什么变量,希望哪位高手能帮助小弟一下~~~ 哦,对了,我用的是eclipse,写swt类用的是ve插件.也导入了jface,runtime,workbench库.还想问一下,如果不用jface,只用swt,能实现我说的那个功能吗?实在多谢了~~~~~~~!!!!
package org.bill;import java.util.List;import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;public class MainDlg
{
private TableViewer tv;
final private Shell shell = new Shell();/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
MainDlg window = new MainDlg();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
//final Shell shell = new Shell();shell.setLayout(new GridLayout());
shell.setSize(500, 375);
shell.setText("家庭理财");
//
createTableViewer(shell);
addListener();shell.open();final Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);final MenuItem menuItem = new MenuItem(menu, SWT.NONE);
menuItem.setText("文件");final MenuItem menuItem_1 = new MenuItem(menu, SWT.NONE);
menuItem_1.setText("编辑");final MenuItem menuItem_2 = new MenuItem(menu, SWT.NONE);
menuItem_2.setText("视图");final Button button = new Button(shell, SWT.NONE);
button.setText("button");tv.setSorter(new Sorter());
tv.setLabelProvider(new TableLabelProvider());
tv.setContentProvider(new ContentProvider());
tv.setInput((new AccountBookIMPL("xml/AccountBook.xml")).getAllAccounts());shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
private void createTableViewer(Composite parent)
{
/*
* 定义一个TableViewer对象
*/tv=new TableViewer(parent,SWT.MULTI|SWT.BORDER|SWT.FULL_SELECTION);/*
* 通过TableViewer中的Table对其布局
*/Table table=tv.getTable();
table.setLayoutData(new GridData(347, 161));
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableLayout tLayout=new TableLayout();
table.setLayout(tLayout);/*
* 建立TableViewer中的列
*/tLayout.addColumnData(new ColumnWeightData(10));
new TableColumn(table,SWT.NONE).setText("序号");tLayout.addColumnData(new ColumnWeightData(10));
new TableColumn(table,SWT.NONE).setText("日期");tLayout.addColumnData(new ColumnWeightData(10));
new TableColumn(table,SWT.NONE).setText("用途");tLayout.addColumnData(new ColumnWeightData(10));
new TableColumn(table,SWT.NONE).setText("金额");tLayout.addColumnData(new ColumnWeightData(10));
new TableColumn(table,SWT.NONE).setText("记录人");tLayout.addColumnData(new ColumnWeightData(10));
new TableColumn(table,SWT.NONE).setText("记录日期");
}class Sorter extends ViewerSorter {
public int compare(Viewer viewer, Object e1, Object e2) {
Object item1 = e1;
Object item2 = e2;
return 0;
}
}
class TableLabelProvider extends LabelProvider implements ITableLabelProvider
{
public String getColumnText(Object element, int columnIndex)
{
Account account=(Account)element;
if(columnIndex==0)
{
return account.getId().toString();
}
if(columnIndex==1)
{
return account.getDate().toString();
}
if(columnIndex==2)
{
return account.getUseWhere().toString();
}
if(columnIndex==3)
{
return (new Integer(account.getNumber())).toString();
}
if(columnIndex==4)
{
return account.getRecordUser().toString();
}
if(columnIndex==5)
{
return account.getRecordDate().toString();
}
return "";
}
public Image getColumnImage(Object element, int columnIndex)
{
return null;
}
}
class ContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement)
{
if(inputElement instanceof List)
{
return ((List)inputElement).toArray();
}
else
{
return new Object[0];
}
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}private void addListener()
{
tv.addDoubleClickListener(new IDoubleClickListener()
{
public void doubleClick(DoubleClickEvent event)
{
IStructuredSelection selection=(IStructuredSelection)event.getSelection();
Account account=(Account)selection.getFirstElement();
MessageDialog.openInformation(null,"提示",
"序号:"+account.getId()+
"\n日期:"+account.getDate()+
"\n用途:"+account.getUseWhere() +
"\n金额:"+String.valueOf(account.getNumber())+
"\n记录人:"+account.getRecordUser()+
"\n记录日期:"+account.getRecordDate());
}
}
);
}}
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/core/runtime/ISafeRunnable
我怀疑是我哪儿没配置对.可是真想不出来..........
不要在eclipse里面运行
有的时候eclipse运行不起来,我以前碰到过