?? brokergui.java
字號:
package trader.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import trader.*;
public class BrokerGui{
// GUI Components
// main frame components
protected JFrame frame;
protected Container contentPane;
protected CardLayout card = new CardLayout();
protected JPanel cardPan = new JPanel();
// selection panel components
protected JPanel selPan = new JPanel();
protected JButton custBt = new JButton("Customer Details");
protected JButton allCustBt = new JButton("All Customers");
protected JButton portBt = new JButton("Portfolio");
protected JButton stockBt = new JButton("Stocks");
// customer panel components
protected JPanel custPan = new JPanel();
protected JLabel nameLb = new JLabel("Customer Name");
protected JLabel idLb = new JLabel("Customer Identity");
protected JLabel addrLb = new JLabel("Customer Address");
protected JTextField nameTf = new JTextField(25);
protected JTextField idTf = new JTextField(25);
protected JTextField addrTf = new JTextField(25);
protected JButton getBt = new JButton("Get Customer");
protected JButton updBt = new JButton("Update Customer");
protected JButton addBt = new JButton("Add Customer");
protected JButton delBt = new JButton("Delete Customer");
//modified by ourteam 051228
//begin
//portfolio panel components
protected JPanel portPan = new JPanel();
protected JLabel nameLp = new JLabel("Customer Name");
protected JLabel idLp = new JLabel("Customer Identity");
protected JLabel sharesLp = new JLabel("Customer Shares (symbol price quantity)");
protected JTextField nameTp = new JTextField(25);
protected JTextField idTp = new JTextField(25);
protected JTextArea sharesTp = new JTextArea();
protected JScrollPane sharesSp=new JScrollPane(sharesTp);
protected JButton welBp = new JButton("welcome to invest!");
protected JButton getBp = new JButton("get portfolio");
protected JButton addBp = new JButton("Buy Stock");
protected JButton delBp = new JButton("Sell Stock");
//end
//modified by ourteam 051228
//begin
//stock panel components
protected JPanel stockPan=new JPanel();
protected JLabel symbolLb=new JLabel("Stock Symbol");
protected JLabel priceLb =new JLabel("Stock Price");
protected JTextField symbolTf=new JTextField(25);
protected JTextField priceTf =new JTextField(25);
protected JButton getBf=new JButton("Get Stock");
protected JButton addBf=new JButton("welcome to query!");
//AllStocksPanel components
protected JPanel allStockPan=new JPanel();
protected JButton allStockLb=
new JButton("All Stocks");
protected JTextArea allStockTa=new JTextArea();
protected JScrollPane allStockSp=new JScrollPane(allStockTa);
//end
// AllCustomersPanel components
protected JPanel allCustPan = new JPanel();
protected JLabel allCustLb =
new JLabel("All Customers", SwingConstants.CENTER);
//protected JTextArea allCustTa= new JTextArea();
//protected JScrollPane allCustSp = new JScrollPane(allCustTa);
String[] tableHeaders1 = {"SSN", "Name", "Address"};
String[] tableHeaders2 ={"Symbol","Price"};
JTable table1,table2;
JScrollPane tablePane11;
JScrollPane tablePane12;
DefaultTableModel tableModel1,tableModel2;
//** Students add the JTable related attributes here
//** 1 Declare attribute tableHeaders of type String[]
//** and initialize to "Customer Id", "Name", "Address"
//** 2 Declare attribute table of type JTable
//** 3 Declare attribute tablePane of type JScrollPane
//** 4 Declare attribute tableModel of type DefaultTableModel
// LogPanel Components
protected JPanel logPan = new JPanel();
protected JLabel logLb =
new JLabel("BrokerTool Log", SwingConstants.CENTER);
protected JTextArea logTa= new JTextArea(9, 50);
protected JScrollPane logSp = new JScrollPane(logTa);
// methods
public void refreshCustPan(Customer cust){
idTf.setText(cust.getId());
nameTf.setText(cust.getName());
addrTf.setText(cust.getAddr());
}
//modified by ourteam 051228
//begin
public void refreshPortPan(Portfolio port){
String temp="";
idTp.setText(port.getCustomer().getId());
nameTp.setText(port.getCustomer().getName());
for(int i=0;i<port.getShares().length;i++){
temp+=(port.getShares()[i]).getStock().getSymbol();
temp+=" ";
temp+=(port.getShares()[i]).getStock().getPrice();
temp+=" ";
temp+=(port.getShares()[i]).getQuantity();
temp+="\n";
}
sharesTp.setText(temp);
}
//end
public void refreshAllCustPan(Customer[] custs){
// To be completed by Students in Mod9
// Hint This method is similar to the updateTable method
// of TableExample class
String newData[][];
newData = new String[custs.length][3];
//modified by ourteam 051228
//begin
//String temp="SSN Name Address\n";
//end
//** 1 Create a 2-dimensional string array with no of rows
//** equal to cust.length and no of columns set to 3, i
//** and assign to newData.
//** 2 Write a for loop to populate the newData array with
//** customer id, name, and addr obtained from custs array
//System.out.println(custs.length);
//System.out.println(custs[0].getId());
for (int i=0; i<custs.length; i++) {
newData[i][0] = custs[i].getId();
newData[i][1] = custs[i].getName();
newData[i][2] = custs[i].getAddr();
//modified by ourteam 051228
//begin
//newData[i][0]=custs[i+1].getId();
//newData[i][1]=custs[i+1].getName();
//newData[i][2]=custs[i+1].getAddr();
//temp += custs[i].getId() + " " + custs[i].getName() + " " + custs[i].getAddr() + "\n";
/*idTf.setText(custs[i].getId());
nameTf.setText(custs[i].getName());
addrTf.setText(custs[i].getAddr());*/
//end
}
//** 3 Invoke the setDataVector method on the tableModel
//** passing it newData and tableHeaders arrays.
// custPan.show(false);
// allCustPan.show(true);
//allCustTa.setText(temp);
tableModel1.setDataVector(newData, tableHeaders1);
}
/* uncomment in final iteration
public void refreshPorfolioPan(Portfolio p){
// optional TBD in mod 9 by students
}
*/
//modified by ourteam 051228
//begin
public void refreshStockPan(Stock stock){
symbolTf.setText(stock.getSymbol());
priceTf.setText(Float.toString(stock.getPrice()));//記住
// optional TBD in mod 9 by students
}
public void refreshAllStockPan(Stock[] stocks){
String newData[][];
newData=new String[stocks.length][2];
for(int i=0;i<stocks.length;i++)
{
//temp+=stocks[i].getSymbol()+" "+stocks[i].getPrice()+"\n";
newData[i][0]=stocks[i].getSymbol();
newData[i][1]=""+stocks[i].getPrice();
}
//allStockTa.setText(temp);
tableModel2.setDataVector(newData,tableHeaders2);
}
//end
//modified by ourteam 051228
//begin
public String getSymbolOnStockPan(){
return symbolTf.getText();
}
public String getPriceOnStockPan(){
return priceTf.getText();
}
//end
public void updateLog(String msg) {
logTa.append(msg + "\n");
}
public String getCustIdOnCustPan(){
return idTf.getText();
}
//modified by ourteam 051228
//begin
public String getCustNameOnCustPan(){
return nameTf.getText();
}
//end
//modified by ourteam 051228
//begin
public String getCustAddrOnCustPan(){
return addrTf.getText();
}
//end
//modified by ourteam 051228
//begin
public String getCustIdOnPortPan(){
//System.out.println("gui Customer:"+idTp.getText());
return idTp.getText();
}
public String getCustNameOnPortPan(){
return nameTp.getText();
}
public Portfolio getPortfolioOnPortPan(){
//System.out.println("gui Customer:"+idTp.getText()+","+nameTp.getText());
return new Portfolio(new Customer(idTp.getText(),nameTp.getText(),""),
this.getCustSharesOnPortPan());
}
public Customer getCustOnPortPan(){
// optional in mod 9 by student
Customer cust = null;
return cust;
}
public ArrayList getCustSharesOnPortPan(){
//modified by ourteam 051228
//deal the string to construct share object
// "symbol price quantity"
// "a 1.2 200
// b 2.4 100
// c 5 1000"
int i,j;
ArrayList shares=null;
//Share[] shares = new Share[];
String symbol, price, quantity;
String temp;
String[] strShares=null;
temp = sharesTp.getText();
//System.out.println("temp ="+temp);
try{
if(temp!=null && !temp.trim().equals("")){
shares=new ArrayList();
strShares = temp.split("\n");
for(i=0; i<strShares.length; i++){
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -