该程序是很简单 只是把我上一个程序可视化希望能给初学java的人提供帮助!!!
//stock
package StockUI;
public class Stock {
private int id; //
private String name; //
private int price; //
private int flag; //
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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
}
//服务器端
package StockUI;
import java.util.Random;
public class StockServer {
public static void main(String[] args){
Stock sk[] = getStock();
for(int i=0;i<sk.length;i++){
if(sk[i] != null){
System.out.println("name = " + sk[i].getPrice());
}
}
}
public static Stock[] getStock(){
Stock stock[] = new Stock[3];
//#####################################################
Stock sk = new Stock();
sk.setId(1);
sk.setName("ibm");
Random rd = new Random();
sk.setPrice(rd.nextInt(100));
sk.setFlag(1);
stock[0] = sk;
//#######################################################
Stock sk1 = new Stock();
sk1.setId(2);
sk1.setName("sun");
sk1.setPrice(rd.nextInt(100));
sk1.setFlag(1);
stock[1] = sk1;
//######################################################
Stock sk2 = new Stock();
sk2.setId(3);
sk2.setName("oracle");
sk2.setPrice(rd.nextInt(100));
sk2.setFlag(1);
stock[2] = sk2;
return stock;
}
}
package StockUI;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
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 StockUI {
private TableEditor editor = null;
private Table table = null;
public static void main(String[] args) {
new StockUI();
}
private StockUI() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("大智慧股票模拟系统");
createTable(shell,display);
shell.pack(); //窗口变大
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* 创建表格
*
* @param shell
*/
private void createTable(final Shell shell,final Display display) {
table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
editor = new TableEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn col1 = new TableColumn(table, SWT.LEFT);
col1.setText("股票代码");
col1.setWidth(100);
TableColumn col2 = new TableColumn(table, SWT.LEFT);
col2.setText("公司名");
col2.setWidth(100);
TableColumn col5 = new TableColumn(table, SWT.LEFT);
col5.setText("现价");
col5.setWidth(100);
TableColumn col3 = new TableColumn(table, SWT.LEFT);
col3.setText("涨幅");
col3.setWidth(100);
TableColumn col4 = new TableColumn(table, SWT.LEFT);
col4.setText("换手率");
col4.setWidth(100);
/**
* 添加表格数据
*/
Stock[] sk = StockServer.getStock();
final TableItem[] itemArr = new TableItem[sk.length];
for(int i=0;i<itemArr.length;i++){
itemArr[i] = new TableItem(table, SWT.LEFT);
}
final int time=1000;
Runnable showTime = new Runnable(){
public void run(){
Stock[] sk = StockServer.getStock();
for(int i=0;i<itemArr.length;i++){
itemArr[i].setText(new String[] { String.valueOf(sk[i].getId()), String.valueOf(sk[i].getName()), String.valueOf(sk[i].getPrice())
});
}
display.timerExec(time, this);
}
};
display.timerExec(time,showTime);//你的swt程序的display
// 删除菜单
Menu menu1 = new Menu(shell, SWT.POP_UP);
table.setMenu(menu1);
MenuItem menuitem1 = new MenuItem(menu1, SWT.PUSH);
menuitem1.setText("删除");
menuitem1.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
MessageBox mbox = new MessageBox(shell, SWT.DIALOG_TRIM|SWT.ICON_INFORMATION);
mbox.setText("删除成功");
mbox.setMessage("删除了" + table.getSelectionCount() + "条记录");
table.remove(table.getSelectionIndices());
mbox.open();
}
});
// 修改table
{}
}
}
//运行结果如下:
不同时段 不同结果