eclipse 不能运行swt
2个回答
展开全部
不知道这个程序对你有没有帮助
package swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
public class SimpleTable2 {
/**
* Launch the application
* @param args
*/
private Shell shell;
private Composite composite;
private ViewForm viewForm;
private Table table;
private void createShell(){
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
createViewForm();
}
private void createViewForm(){
viewForm=new ViewForm(shell,SWT.NONE);
createComposite();
viewForm.setContent(composite);
viewForm.pack();
}
private void createComposite(){
composite=new Composite(viewForm,SWT.NONE);
createTable();
composite.pack();
}
private void createTable() {
table=new Table(composite,SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
String[] header=;
for(int i=0;i<header.length;i++){
TableColumn column=new TableColumn(table,SWT.NONE);
column.setText(header[i]);
}
TableItem tableItem=new TableItem(table,SWT.NONE);
tableItem.setText(new String[]);
tableItem=new TableItem(table,SWT.NONE);
tableItem.setText(new String[]);
for(int i=0;i<table.getColumnCount();i++)
table.getColumn(i).pack();
table.pack();
//add ControlEditor on TableCursor
final TableCursor cursor=new TableCursor(table,SWT.NONE);
//cursor令使用左右键选择单元格成为可能
final ControlEditor editor=new ControlEditor(cursor);
editor.grabHorizontal=true;
editor.grabVertical=true;
cursor.addSelectionListener(new SelectionListener(){
public void widgetDefaultSelected(SelectionEvent e) {
//当触发此事件时才创建text(并为她添加事件),并用setFocus()把她固定住
final Text text=new Text(cursor,SWT.NONE);
TableItem row=cursor.getRow();
int column=cursor.getColumn();
text.setText(row.getText(column));
text.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
if(e.character==SWT.CR){
TableItem row=cursor.getRow();
int column=cursor.getColumn();
row.setText(column,text.getText());
text.dispose();
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
});
text.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
public void focusLost(FocusEvent e) {
text.dispose(); //目前认为可有可无
}
});
editor.setEditor(text);
text.setFocus(); //将text固定于某个单元格上
}
public void widgetSelected(SelectionEvent e) {
table.setSelection(new TableItem[]);
//当选中某行时,此行出现阴影
}
});
}
public static void main(String[] args) {
final Display display = Display.getDefault();
SimpleTable2 st=new SimpleTable2();
//
st.createShell();
st.shell.pack();
st.shell.open();
st.shell.layout();
while (!st.shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela
package swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
public class SimpleTable2 {
/**
* Launch the application
* @param args
*/
private Shell shell;
private Composite composite;
private ViewForm viewForm;
private Table table;
private void createShell(){
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
createViewForm();
}
private void createViewForm(){
viewForm=new ViewForm(shell,SWT.NONE);
createComposite();
viewForm.setContent(composite);
viewForm.pack();
}
private void createComposite(){
composite=new Composite(viewForm,SWT.NONE);
createTable();
composite.pack();
}
private void createTable() {
table=new Table(composite,SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
String[] header=;
for(int i=0;i<header.length;i++){
TableColumn column=new TableColumn(table,SWT.NONE);
column.setText(header[i]);
}
TableItem tableItem=new TableItem(table,SWT.NONE);
tableItem.setText(new String[]);
tableItem=new TableItem(table,SWT.NONE);
tableItem.setText(new String[]);
for(int i=0;i<table.getColumnCount();i++)
table.getColumn(i).pack();
table.pack();
//add ControlEditor on TableCursor
final TableCursor cursor=new TableCursor(table,SWT.NONE);
//cursor令使用左右键选择单元格成为可能
final ControlEditor editor=new ControlEditor(cursor);
editor.grabHorizontal=true;
editor.grabVertical=true;
cursor.addSelectionListener(new SelectionListener(){
public void widgetDefaultSelected(SelectionEvent e) {
//当触发此事件时才创建text(并为她添加事件),并用setFocus()把她固定住
final Text text=new Text(cursor,SWT.NONE);
TableItem row=cursor.getRow();
int column=cursor.getColumn();
text.setText(row.getText(column));
text.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
if(e.character==SWT.CR){
TableItem row=cursor.getRow();
int column=cursor.getColumn();
row.setText(column,text.getText());
text.dispose();
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
});
text.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
public void focusLost(FocusEvent e) {
text.dispose(); //目前认为可有可无
}
});
editor.setEditor(text);
text.setFocus(); //将text固定于某个单元格上
}
public void widgetSelected(SelectionEvent e) {
table.setSelection(new TableItem[]);
//当选中某行时,此行出现阴影
}
});
}
public static void main(String[] args) {
final Display display = Display.getDefault();
SimpleTable2 st=new SimpleTable2();
//
st.createShell();
st.shell.pack();
st.shell.open();
st.shell.layout();
while (!st.shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询