這個程序終于完完整整地做完了,雖然還不完善,但基本的功能實(shí)現(xiàn)了。這個程序零零散散花費(fèi)了我近一個月的時間,在這一個月的時間里,通過別人的幫助和對程序的調(diào)試本人收獲不小。希望通過這個博客和大家分享一下我在程序中犯的錯誤。
1.其實(shí)用SWT做東西,基本的組件什么的都挺容易,難的是兩個或多個面板的調(diào)用(我想這就是面向?qū)ο笏枷氚桑.?dāng)調(diào)用另一個Display時,一定要通過參數(shù)將主程序中的display傳遞到另一個程序中,然后在另一個程序用構(gòu)造函數(shù)接受傳遞過來的display。
2.在用按鈕調(diào)用另一個面板時,一定不要將面板設(shè)置為static類型的,否則第二次點(diǎn)擊這個按鈕時將會出錯。
3.不管是主程序還是其它程序一定要有shell.open(),即將面板打開
4.在非主程序中還要有shell.close()即將面板關(guān)閉
下面看這個程序的要求:
要求:
1.實(shí)現(xiàn)增、刪、改、查功能
2.使用SWT進(jìn)行界面管理
下面大致的看一下這個程序的運(yùn)行效果。
1、初次運(yùn)行界面:
2、選擇添加按鈕:
輸入信息,如果信息輸入格式不正確:
輸入正確的信息,就會添加到表格中。
3、選擇刪除按鈕前,必須選中一條或多條信息,否則會出現(xiàn):
選擇將要刪除的學(xué)生信息,點(diǎn)擊刪除按鈕就會將改學(xué)生的信息在表格中刪除。
4、選擇修改按鈕前,也必須選中一條信息,否則會出現(xiàn):
選中信息后,點(diǎn)擊修改按鈕,就會出現(xiàn)修改學(xué)生信息窗口:
將信息修改后,點(diǎn)擊確定按鈕就會將原先的信息覆蓋并顯示在表格中
5、選擇查詢按鈕,就會出現(xiàn):
可以通過學(xué)號、姓名、成績、班級或者他們中的任意一項(xiàng)或幾項(xiàng)進(jìn)行查詢,如果一項(xiàng)都不寫將出現(xiàn):
如果沒有您要查詢的信息,將出現(xiàn):
如果有您的信息,那么這個信息在表格中將以綠色顯示:
這個程序還有許多要修改的地方。
//學(xué)生類
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);
//創(chuàng)建窗體
shell.setLayout(new FillLayout());
shell.setText("學(xué)生成績管理系統(tǒng)");
shell.setBounds(0,0,600,600);
shell.setVisible(true);
//創(chuàng)建表單
table=new Table(shell,SWT.MULTI | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
//創(chuàng)建列
TableColumn column1=new TableColumn(table, SWT.NONE);
column1.setText("學(xué)號");
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);
//創(chuàng)建按鈕容器
Composite post=new Composite(shell,SWT.NONE);
Button button1=new Button(post,SWT.NONE);
//創(chuàng)建按鈕
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計(jì)本"});

new TableItem(table,SWT.LEFT).setText(new String[]
{"13","李明","80","09樓宇"});

new TableItem(table,SWT.LEFT).setText(new String[]
{"14","劉德華","50","08機(jī)電"});

new TableItem(table,SWT.LEFT).setText(new String[]
{"15","范偉","40","09樓宇"});

new TableItem(table,SWT.LEFT).setText(new String[]
{"16","朱元璋","70","08經(jīng)貿(mào)"});

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機(jī)械"});

new TableItem(table,SWT.LEFT).setText(new String[]
{"21","周潤發(fā)","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","王絡(luò)丹","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("請選擇要刪除的內(nèi)容");
box.open();
}
else

{
int[] selInices = table.getSelectionIndices();//將選中的序號放在數(shù)組中
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("請選擇要修改的內(nèi)容");
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();
}
}
}

}

//添加學(xué)生信息窗體
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("添加學(xué)生信息");
//添加標(biāo)簽
Label id=new Label(shell,SWT.NONE);
id.setText("學(xué)號");
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);//學(xué)號必須是兩位
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();
}
}

//修改學(xué)生窗體
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("修改學(xué)生信息");
//添加標(biāo)簽
Label id=new Label(shell,SWT.NONE);
id.setText("學(xué)號");
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();
}
}

//查詢學(xué)生窗體
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("查詢學(xué)生信息");
//添加標(biāo)簽
Label shuoming=new Label(shell,SWT.NONE);
shuoming.setText("請輸入所要查詢的信息,至少輸入一項(xiàng)");
shuoming.setFont(new Font(display,"宋體",10,SWT.BOLD));
shuoming.setBounds(10,30,245,20);
Label id=new Label(shell,SWT.NONE);
id.setText("請輸入要查詢的學(xué)號");
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("請至少輸入一項(xiàng)信息");
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();
}

}

