这个程序终于完完整整地做完了,虽然还不完善,但基本的功能实现了。这个程序零零散散花费了我近一个月的时间,在这一个月的时间里,通过别人的帮助和对程序的调试本人收获不小。希望通过这个博客和大家分享一下我在程序中犯的错误。
1.其实用SWT做东西,基本的组件什么的都挺容易,难的是两个或多个面板的调用(我想这就是面向对象思想吧)。当调用另一个Display时,一定要通过参数将主程序中的display传递到另一个程序中,然后在另一个程序用构造函数接受传递过来的display。
2.在用按钮调用另一个面板时,一定不要将面板设置为static类型的,否则第二次点击这个按钮时将会出错。
3.不管是主程序还是其它程序一定要有shell.open(),即将面板打开
4.在非主程序中还要有shell.close()即将面板关闭
下面看这个程序的要求:
要求:
1.实现增、删、改、查功能
2.使用SWT进行界面管理
下面大致的看一下这个程序的运行效果。
1、初次运行界面:
2、选择添加按钮:
输入信息,如果信息输入格式不正确:
输入正确的信息,就会添加到表格中。
3、选择删除按钮前,必须选中一条或多条信息,否则会出现:
选择将要删除的学生信息,点击删除按钮就会将改学生的信息在表格中删除。
4、选择修改按钮前,也必须选中一条信息,否则会出现:
选中信息后,点击修改按钮,就会出现修改学生信息窗口:
将信息修改后,点击确定按钮就会将原先的信息覆盖并显示在表格中
5、选择查询按钮,就会出现:
可以通过学号、姓名、成绩、班级或者他们中的任意一项或几项进行查询,如果一项都不写将出现:
如果没有您要查询的信息,将出现:
如果有您的信息,那么这个信息在表格中将以绿色显示:
这个程序还有许多要修改的地方。
//学生类
package com.dr.swt.XueChengXiTong;
public class Student {
private int id;
private String name;
private float score;
private String cls;
public int getId() {
return id;
}
public void setId( int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getScore() {
return score;
}
public void setScore( float score) {
this.score = score;
}
public String getCls() {
return cls;
}
public void setCls(String cls) {
this.cls = cls;
}
}
//主程序
package com.dr.swt.XueChengXiTong;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class MainUI
{
static Table table= null;
public static void main(String args[])
{
final Display display= new Display();
final Shell shell= new Shell(display);
//创建窗体
shell.setLayout( new FillLayout());
shell.setText( "学生成绩管理系统");
shell.setBounds(0,0,600,600);
shell.setVisible( true);
//创建表单
table= new Table(shell,SWT.MULTI | SWT.FULL_SELECTION);
table.setHeaderVisible( true);
table.setLinesVisible( true);
//创建列
TableColumn column1= new TableColumn(table, SWT.NONE);
column1.setText( "学号");
column1.setWidth(100);
TableColumn column2= new TableColumn(table,SWT.NONE);
column2.setText( "姓名");
column2.setWidth(100);
TableColumn column3= new TableColumn(table,SWT.NONE);
column3.setText( "成绩");
column3.setWidth(100);
TableColumn column4= new TableColumn(table,SWT.NONE);
column4.setText( "班级");
column4.setWidth(100);
//创建按钮容器
Composite post= new Composite(shell,SWT.NONE);
Button button1= new Button(post,SWT.NONE);
//创建按钮
button1.setText( "添加");
button1.setBounds(30,30,100,50);
Button button2= new Button(post,SWT.NONE);
button1.setBackground( new Color(display, SWT.COLOR_DARK_RED, 200, 20));
button2.setText( "删除");
button2.setBounds(200,30,100,50);
button2.setFont( new Font(display, "宋体",20,SWT.NONE));
Button button3= new Button(post,SWT.NONE);
button3.setText( "修改");
button3.setBounds(30,150,100,50);
Button button4= new Button(post,SWT.NONE);
button3.setEnabled( true);
button4.setText( "查询");
button4.setBounds(200,150,100,50);
//table初始化
new TableItem(table,SWT.LEFT).setText( new String[]{ "10", "张三", "60", "08楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "11", "李四", "90", "09电本"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "12", "王倩", "70", "08计本"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "13", "李明", "80", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "14", "刘德华", "50", "08机电"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "15", "范伟", "40", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "16", "朱元璋", "70", "08经贸"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "17", "周星驰", "65", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "18", "李连杰", "55", "08楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "19", "赵薇", "78", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "20", "林心如", "70", "08机械"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "21", "周润发", "88", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "22", "成龙", "73", "08汉语言"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "23", "赵本山", "80", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "24", "郭德纲", "56", "08小品"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "25", "周迅", "35", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "26", "王络丹", "49", "08土木"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "27", "刘亦菲", "60", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "28", "张静初", "55", "08建工"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "29", "文章", "78", "09通信"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "30", "王力宏", "80", "09楼宇"});
//为添加按钮添加事件处理
button1.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
TableItem[] item=table.getItems();
for( int i=0;i {
item[i].setBackground( new Color(display,255, 255, 255));
}
System.out.println( "test");
AddStudentUI.addStuShow(display,table);
}
});
//为删除按钮添加事件处理
button2.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
TableItem[] item=table.getItems();
for( int i=0;i {
item[i].setBackground( new Color(display,255, 255, 255));
}
if(table.getSelectionIndex()==-1)
{
MessageBox box= new MessageBox(shell);
box.setMessage( "请选择要删除的内容");
box.open();
}
else
{
int[] selInices = table.getSelectionIndices(); //将选中的序号放在数组中
table.remove(selInices);
}
}
});
//为修改按钮添加事件处理
button3.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
TableItem[] item=table.getItems();
for( int i=0;i {
item[i].setBackground( new Color(display,255, 255, 255));
}
if(table.getSelectionIndex()==-1)
{
MessageBox box= new MessageBox(shell);
box.setMessage( "请选择要修改的内容");
box.open();
}
else
{
ModifyStudentUI.modifyStuShow(display, table);
}
}
});
//为查找按钮添加事件处理
button4.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
TableItem[] item=table.getItems();
for( int i=0;i {
item[i].setBackground( new Color(display,255, 255, 255));
}
FindStuUI.findStuShow(display,table);
}
});
shell.pack();
shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
}
}
//添加学生程序
package com.dr.swt.XueChengXiTong;
import java.util.ArrayList;
import java.util.List;
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.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
class AddStudentUI
{
Display display= null;
Shell shell= new Shell(display);
public AddStudentUI(Display dy) {
display=dy;
}
public static void addStuShow(Display dy,Table table)
{
AddStudentUI ast= new AddStudentUI(dy);
ast.run(table);
}
private void run( final Table table) {
shell.setBounds(500,200,250,400);
shell.setText( "添加学生信息");
//添加标签
Label id= new Label(shell,SWT.NONE);
id.setText( "学号");
id.setBounds(40,50,60,40);
Label name= new Label(shell,SWT.NONE);
name.setText( "姓名");
name.setBounds(40,100,60,40);
Label score= new Label(shell,SWT.NONE);
score.setText( "成绩");
score.setBounds(40,150,60,40);
Label cls= new Label(shell,SWT.NONE);
cls.setText( "班级");
cls.setBounds(40,200,60,40);
//添加文本框
final Text text1= new Text(shell,SWT.NONE);
text1.setBounds(100,45,100,25);
text1.setTextLimit(2); //学号必须是两位
final Text text2= new Text(shell,SWT.NONE);
text2.setBounds(100,95,100,25);
text2.setTextLimit(6); //姓名最多为三位
final Text text3= new Text(shell,SWT.NONE);
text3.setBounds(100,145,100,25);
final Text text4= new Text(shell,SWT.NONE);
text4.setBounds(100,195,100,25);
//添加按钮
Button button1= new Button(shell,SWT.NONE);
button1.setText( "确定");
button1.setBounds(55,250,60,40);
Button button2= new Button(shell,SWT.NONE);
button2.setText( "取消");
button2.setBounds(135,250,60,40);
//为确定按钮添加事件处理,添加信息
button1.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
try{
Student stu= new Student();
stu.setId(Integer.parseInt(text1.getText()));
stu.setName(text2.getText());
stu.setScore(Float.valueOf(text3.getText()).floatValue());
stu.setCls(text4.getText());
new TableItem(table,0).setText( new String[] { Integer.toString(stu.getId()) ,stu.getName(),Float.toString(stu.getScore()),stu.getCls()});
shell.close();
} catch(NumberFormatException e1)
{
MessageBox box= new MessageBox(shell,0);
box.setMessage( "输入信息无效");
box.open();
}
}
});
//为取消按钮添加事件处理
button2.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
shell.close();
}
});
shell.open();
}
}
//修改学生程序
package com.dr.swt.XueChengXiTong;
import java.util.List;
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.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
public class ModifyStudentUI {
Display display= null;
Shell shell= new Shell(display);
public ModifyStudentUI(Display dy) {
display=dy;
} public static void modifyStuShow(Display dy,Table table)
{
ModifyStudentUI mst= new ModifyStudentUI(dy);
mst.run(table);
}
void run( final Table table) {
shell.setBounds(500,200,250,400);
shell.setText( "修改学生信息");
//添加标签
Label id= new Label(shell,SWT.NONE);
id.setText( "学号");
id.setBounds(40,50,60,40);
Label name= new Label(shell,SWT.NONE);
name.setText( "姓名");
name.setBounds(40,100,60,40);
Label score= new Label(shell,SWT.NONE);
score.setText( "成绩");
score.setBounds(40,150,60,40);
Label cls= new Label(shell,SWT.NONE);
cls.setText( "班级");
cls.setBounds(40,200,60,40);
//添加文本框
final Text text1= new Text(shell,SWT.NONE);
text1.setBounds(100,45,100,25);
text1.setText(table.getItem(table.getSelectionIndex()).getText(0));
final Text text2= new Text(shell,SWT.NONE);
text2.setBounds(100,95,100,25);
text2.setText(table.getItem(table.getSelectionIndex()).getText(1));
final Text text3= new Text(shell,SWT.NONE);
text3.setBounds(100,145,100,25);
text3.setText(table.getItem(table.getSelectionIndex()).getText(2));
final Text text4= new Text(shell,SWT.NONE);
text4.setBounds(100,195,100,25);
text4.setText(table.getItem(table.getSelectionIndex()).getText(3));
//添加按钮
Button button1= new Button(shell,SWT.NONE);
button1.setText( "确定");
button1.setBounds(55,250,60,40);
Button button2= new Button(shell,SWT.NONE);
button2.setText( "取消");
button2.setBounds(135,250,60,40);
//为确定按钮添加事件处理,添加信息
button1.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
try{
Student stu= new Student();
stu.setId(Integer.parseInt(text1.getText()));
stu.setName(text2.getText());
stu.setScore(Float.valueOf(text3.getText()).floatValue());
stu.setCls(text4.getText());
TableItem tableitem=table.getItem(table.getSelectionIndex());
tableitem.setText( new String[] { Integer.toString(stu.getId()) ,stu.getName(),Float.toString(stu.getScore()),stu.getCls()});
shell.close();
} catch(NumberFormatException e1)
{
MessageBox box= new MessageBox(shell,0);
box.setMessage( "输入信息无效");
box.open();
}
}
});
//为取消按钮添加事件处理
button2.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
shell.close();
}
});
shell.open();
}
}
//查询学生程序
package com.dr.swt.XueChengXiTong;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
public class FindStuUI {
Display display= null;
Shell shell= new Shell(display);
public FindStuUI(Display dy)
{
this.display=dy;
}
static void findStuShow(Display dy,Table table)
{
FindStuUI fst= new FindStuUI(dy);
fst.run(table);
}
public void run( final Table table)
{
shell.setBounds(500,200,280,400);
shell.setText( "查询学生信息");
//添加标签
Label shuoming= new Label(shell,SWT.NONE);
shuoming.setText( "请输入所要查询的信息,至少输入一项");
shuoming.setFont( new Font(display, "宋体",10,SWT.BOLD));
shuoming.setBounds(10,30,245,20);
Label id= new Label(shell,SWT.NONE);
id.setText( "请输入要查询的学号");
id.setBounds(20,70,120,40);
Label name= new Label(shell,SWT.NONE);
name.setText( "请输入要查询的姓名");
name.setBounds(20,120,120,40);
Label score= new Label(shell,SWT.NONE);
score.setText( "请输入要查询的成绩");
score.setBounds(20,170,120,40);
Label cls= new Label(shell,SWT.NONE);
cls.setText( "请输入要查询的班级");
cls.setBounds(20,220,120,40);
//添加文本框
final Text text1= new Text(shell,SWT.NONE);
text1.setBounds(140,65,100,25);
final Text text2= new Text(shell,SWT.NONE);
text2.setBounds(140,115,100,25);
final Text text3= new Text(shell,SWT.NONE);
text3.setBounds(140,165,100,25);
final Text text4= new Text(shell,SWT.NONE);
text4.setBounds(140,215,100,25);
//添加按钮
Button button1= new Button(shell,SWT.NONE);
button1.setText( "确定");
button1.setBounds(55,270,60,40);
Button button2= new Button(shell,SWT.NONE);
button2.setText( "取消");
button2.setBounds(135,270,60,40);
//为确定按钮添加事件处理
button1.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
boolean flag= false;
if( "".equals(text1.getText())&&"".equals(text2.getText())&& "".equals(text3.getText())&&"".equals(text4.getText()))
{
MessageBox box = new MessageBox(shell);
box.setMessage( "请至少输入一项信息");
box.open();
}
else
{
int count=0;
TableItem it[]=table.getItems();
for( int i=0;i {
flag= true;
if(text1.getText().equals(it[i].getText(0)))
{
it[i].setBackground( new Color(display, SWT.COLOR_DARK_RED, 200, 20));flag= false;
}
else if(text2.getText().equals(it[i].getText(1)))
{
it[i].setBackground( new Color(display, SWT.COLOR_DARK_RED, 200, 20));flag= false;
}
else if(text3.getText().equals(it[i].getText(2)))
{
it[i].setBackground( new Color(display, SWT.COLOR_DARK_RED, 200, 20));flag= false;
}
else if(text4.getText().equals(it[i].getText(3)))
{
it[i].setBackground( new Color(display, SWT.COLOR_DARK_RED, 200, 20));flag= false;
}
if(flag== false)
{
count++;
}
}
if(count==0)
{
MessageBox box= new MessageBox(shell);
box.setMessage( "很抱歉,没有你所要查询的信息");
box.open();
}
}
if(flag)
{
shell.close();
}
}
});
//为取消按钮添加事件处理
button2.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
shell.close();
}
});
shell.open();
}
}
package com.dr.swt.XueChengXiTong;
public class Student {
private int id;
private String name;
private float score;
private String cls;
public int getId() {
return id;
}
public void setId( int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getScore() {
return score;
}
public void setScore( float score) {
this.score = score;
}
public String getCls() {
return cls;
}
public void setCls(String cls) {
this.cls = cls;
}
}
//主程序
package com.dr.swt.XueChengXiTong;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class MainUI
{
static Table table= null;
public static void main(String args[])
{
final Display display= new Display();
final Shell shell= new Shell(display);
//创建窗体
shell.setLayout( new FillLayout());
shell.setText( "学生成绩管理系统");
shell.setBounds(0,0,600,600);
shell.setVisible( true);
//创建表单
table= new Table(shell,SWT.MULTI | SWT.FULL_SELECTION);
table.setHeaderVisible( true);
table.setLinesVisible( true);
//创建列
TableColumn column1= new TableColumn(table, SWT.NONE);
column1.setText( "学号");
column1.setWidth(100);
TableColumn column2= new TableColumn(table,SWT.NONE);
column2.setText( "姓名");
column2.setWidth(100);
TableColumn column3= new TableColumn(table,SWT.NONE);
column3.setText( "成绩");
column3.setWidth(100);
TableColumn column4= new TableColumn(table,SWT.NONE);
column4.setText( "班级");
column4.setWidth(100);
//创建按钮容器
Composite post= new Composite(shell,SWT.NONE);
Button button1= new Button(post,SWT.NONE);
//创建按钮
button1.setText( "添加");
button1.setBounds(30,30,100,50);
Button button2= new Button(post,SWT.NONE);
button1.setBackground( new Color(display, SWT.COLOR_DARK_RED, 200, 20));
button2.setText( "删除");
button2.setBounds(200,30,100,50);
button2.setFont( new Font(display, "宋体",20,SWT.NONE));
Button button3= new Button(post,SWT.NONE);
button3.setText( "修改");
button3.setBounds(30,150,100,50);
Button button4= new Button(post,SWT.NONE);
button3.setEnabled( true);
button4.setText( "查询");
button4.setBounds(200,150,100,50);
//table初始化
new TableItem(table,SWT.LEFT).setText( new String[]{ "10", "张三", "60", "08楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "11", "李四", "90", "09电本"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "12", "王倩", "70", "08计本"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "13", "李明", "80", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "14", "刘德华", "50", "08机电"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "15", "范伟", "40", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "16", "朱元璋", "70", "08经贸"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "17", "周星驰", "65", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "18", "李连杰", "55", "08楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "19", "赵薇", "78", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "20", "林心如", "70", "08机械"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "21", "周润发", "88", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "22", "成龙", "73", "08汉语言"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "23", "赵本山", "80", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "24", "郭德纲", "56", "08小品"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "25", "周迅", "35", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "26", "王络丹", "49", "08土木"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "27", "刘亦菲", "60", "09楼宇"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "28", "张静初", "55", "08建工"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "29", "文章", "78", "09通信"});
new TableItem(table,SWT.LEFT).setText( new String[]{ "30", "王力宏", "80", "09楼宇"});
//为添加按钮添加事件处理
button1.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
TableItem[] item=table.getItems();
for( int i=0;i
item[i].setBackground( new Color(display,255, 255, 255));
}
System.out.println( "test");
AddStudentUI.addStuShow(display,table);
}
});
//为删除按钮添加事件处理
button2.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
TableItem[] item=table.getItems();
for( int i=0;i
item[i].setBackground( new Color(display,255, 255, 255));
}
if(table.getSelectionIndex()==-1)
{
MessageBox box= new MessageBox(shell);
box.setMessage( "请选择要删除的内容");
box.open();
}
else
{
int[] selInices = table.getSelectionIndices(); //将选中的序号放在数组中
table.remove(selInices);
}
}
});
//为修改按钮添加事件处理
button3.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
TableItem[] item=table.getItems();
for( int i=0;i
item[i].setBackground( new Color(display,255, 255, 255));
}
if(table.getSelectionIndex()==-1)
{
MessageBox box= new MessageBox(shell);
box.setMessage( "请选择要修改的内容");
box.open();
}
else
{
ModifyStudentUI.modifyStuShow(display, table);
}
}
});
//为查找按钮添加事件处理
button4.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
TableItem[] item=table.getItems();
for( int i=0;i
item[i].setBackground( new Color(display,255, 255, 255));
}
FindStuUI.findStuShow(display,table);
}
});
shell.pack();
shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
}
}
//添加学生程序
package com.dr.swt.XueChengXiTong;
import java.util.ArrayList;
import java.util.List;
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.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
class AddStudentUI
{
Display display= null;
Shell shell= new Shell(display);
public AddStudentUI(Display dy) {
display=dy;
}
public static void addStuShow(Display dy,Table table)
{
AddStudentUI ast= new AddStudentUI(dy);
ast.run(table);
}
private void run( final Table table) {
shell.setBounds(500,200,250,400);
shell.setText( "添加学生信息");
//添加标签
Label id= new Label(shell,SWT.NONE);
id.setText( "学号");
id.setBounds(40,50,60,40);
Label name= new Label(shell,SWT.NONE);
name.setText( "姓名");
name.setBounds(40,100,60,40);
Label score= new Label(shell,SWT.NONE);
score.setText( "成绩");
score.setBounds(40,150,60,40);
Label cls= new Label(shell,SWT.NONE);
cls.setText( "班级");
cls.setBounds(40,200,60,40);
//添加文本框
final Text text1= new Text(shell,SWT.NONE);
text1.setBounds(100,45,100,25);
text1.setTextLimit(2); //学号必须是两位
final Text text2= new Text(shell,SWT.NONE);
text2.setBounds(100,95,100,25);
text2.setTextLimit(6); //姓名最多为三位
final Text text3= new Text(shell,SWT.NONE);
text3.setBounds(100,145,100,25);
final Text text4= new Text(shell,SWT.NONE);
text4.setBounds(100,195,100,25);
//添加按钮
Button button1= new Button(shell,SWT.NONE);
button1.setText( "确定");
button1.setBounds(55,250,60,40);
Button button2= new Button(shell,SWT.NONE);
button2.setText( "取消");
button2.setBounds(135,250,60,40);
//为确定按钮添加事件处理,添加信息
button1.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
try{
Student stu= new Student();
stu.setId(Integer.parseInt(text1.getText()));
stu.setName(text2.getText());
stu.setScore(Float.valueOf(text3.getText()).floatValue());
stu.setCls(text4.getText());
new TableItem(table,0).setText( new String[] { Integer.toString(stu.getId()) ,stu.getName(),Float.toString(stu.getScore()),stu.getCls()});
shell.close();
} catch(NumberFormatException e1)
{
MessageBox box= new MessageBox(shell,0);
box.setMessage( "输入信息无效");
box.open();
}
}
});
//为取消按钮添加事件处理
button2.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
shell.close();
}
});
shell.open();
}
}
//修改学生程序
package com.dr.swt.XueChengXiTong;
import java.util.List;
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.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
public class ModifyStudentUI {
Display display= null;
Shell shell= new Shell(display);
public ModifyStudentUI(Display dy) {
display=dy;
} public static void modifyStuShow(Display dy,Table table)
{
ModifyStudentUI mst= new ModifyStudentUI(dy);
mst.run(table);
}
void run( final Table table) {
shell.setBounds(500,200,250,400);
shell.setText( "修改学生信息");
//添加标签
Label id= new Label(shell,SWT.NONE);
id.setText( "学号");
id.setBounds(40,50,60,40);
Label name= new Label(shell,SWT.NONE);
name.setText( "姓名");
name.setBounds(40,100,60,40);
Label score= new Label(shell,SWT.NONE);
score.setText( "成绩");
score.setBounds(40,150,60,40);
Label cls= new Label(shell,SWT.NONE);
cls.setText( "班级");
cls.setBounds(40,200,60,40);
//添加文本框
final Text text1= new Text(shell,SWT.NONE);
text1.setBounds(100,45,100,25);
text1.setText(table.getItem(table.getSelectionIndex()).getText(0));
final Text text2= new Text(shell,SWT.NONE);
text2.setBounds(100,95,100,25);
text2.setText(table.getItem(table.getSelectionIndex()).getText(1));
final Text text3= new Text(shell,SWT.NONE);
text3.setBounds(100,145,100,25);
text3.setText(table.getItem(table.getSelectionIndex()).getText(2));
final Text text4= new Text(shell,SWT.NONE);
text4.setBounds(100,195,100,25);
text4.setText(table.getItem(table.getSelectionIndex()).getText(3));
//添加按钮
Button button1= new Button(shell,SWT.NONE);
button1.setText( "确定");
button1.setBounds(55,250,60,40);
Button button2= new Button(shell,SWT.NONE);
button2.setText( "取消");
button2.setBounds(135,250,60,40);
//为确定按钮添加事件处理,添加信息
button1.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
try{
Student stu= new Student();
stu.setId(Integer.parseInt(text1.getText()));
stu.setName(text2.getText());
stu.setScore(Float.valueOf(text3.getText()).floatValue());
stu.setCls(text4.getText());
TableItem tableitem=table.getItem(table.getSelectionIndex());
tableitem.setText( new String[] { Integer.toString(stu.getId()) ,stu.getName(),Float.toString(stu.getScore()),stu.getCls()});
shell.close();
} catch(NumberFormatException e1)
{
MessageBox box= new MessageBox(shell,0);
box.setMessage( "输入信息无效");
box.open();
}
}
});
//为取消按钮添加事件处理
button2.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
shell.close();
}
});
shell.open();
}
}
//查询学生程序
package com.dr.swt.XueChengXiTong;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
public class FindStuUI {
Display display= null;
Shell shell= new Shell(display);
public FindStuUI(Display dy)
{
this.display=dy;
}
static void findStuShow(Display dy,Table table)
{
FindStuUI fst= new FindStuUI(dy);
fst.run(table);
}
public void run( final Table table)
{
shell.setBounds(500,200,280,400);
shell.setText( "查询学生信息");
//添加标签
Label shuoming= new Label(shell,SWT.NONE);
shuoming.setText( "请输入所要查询的信息,至少输入一项");
shuoming.setFont( new Font(display, "宋体",10,SWT.BOLD));
shuoming.setBounds(10,30,245,20);
Label id= new Label(shell,SWT.NONE);
id.setText( "请输入要查询的学号");
id.setBounds(20,70,120,40);
Label name= new Label(shell,SWT.NONE);
name.setText( "请输入要查询的姓名");
name.setBounds(20,120,120,40);
Label score= new Label(shell,SWT.NONE);
score.setText( "请输入要查询的成绩");
score.setBounds(20,170,120,40);
Label cls= new Label(shell,SWT.NONE);
cls.setText( "请输入要查询的班级");
cls.setBounds(20,220,120,40);
//添加文本框
final Text text1= new Text(shell,SWT.NONE);
text1.setBounds(140,65,100,25);
final Text text2= new Text(shell,SWT.NONE);
text2.setBounds(140,115,100,25);
final Text text3= new Text(shell,SWT.NONE);
text3.setBounds(140,165,100,25);
final Text text4= new Text(shell,SWT.NONE);
text4.setBounds(140,215,100,25);
//添加按钮
Button button1= new Button(shell,SWT.NONE);
button1.setText( "确定");
button1.setBounds(55,270,60,40);
Button button2= new Button(shell,SWT.NONE);
button2.setText( "取消");
button2.setBounds(135,270,60,40);
//为确定按钮添加事件处理
button1.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
boolean flag= false;
if( "".equals(text1.getText())&&"".equals(text2.getText())&& "".equals(text3.getText())&&"".equals(text4.getText()))
{
MessageBox box = new MessageBox(shell);
box.setMessage( "请至少输入一项信息");
box.open();
}
else
{
int count=0;
TableItem it[]=table.getItems();
for( int i=0;i
flag= true;
if(text1.getText().equals(it[i].getText(0)))
{
it[i].setBackground( new Color(display, SWT.COLOR_DARK_RED, 200, 20));flag= false;
}
else if(text2.getText().equals(it[i].getText(1)))
{
it[i].setBackground( new Color(display, SWT.COLOR_DARK_RED, 200, 20));flag= false;
}
else if(text3.getText().equals(it[i].getText(2)))
{
it[i].setBackground( new Color(display, SWT.COLOR_DARK_RED, 200, 20));flag= false;
}
else if(text4.getText().equals(it[i].getText(3)))
{
it[i].setBackground( new Color(display, SWT.COLOR_DARK_RED, 200, 20));flag= false;
}
if(flag== false)
{
count++;
}
}
if(count==0)
{
MessageBox box= new MessageBox(shell);
box.setMessage( "很抱歉,没有你所要查询的信息");
box.open();
}
}
if(flag)
{
shell.close();
}
}
});
//为取消按钮添加事件处理
button2.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
shell.close();
}
});
shell.open();
}
}