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.length;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.length;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.length;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.length;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<it.length;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();
}
}