?? 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");
// 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);
//** 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());
}
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[][];
//** 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
for (int i=0; i<custs.length; i++) {
}
//** 3 Invoke the setDataVector method on the tableModel
//** passing it newData and tableHeaders arrays.
}
/* uncomment in final iteration
public void refreshPorfolioPan(Portfolio p){
// optional TBD in mod 9 by students
}
*/
public void refreshStockPan(){
// optional TBD in mod 9 by students
}
public void updateLog(String msg) {
logTa.append(msg + "\n");
}
public String getCustIdOnCustPan(){
return idTf.getText();
}
public Customer getCustomerOnCustPan(){
return new Customer
(idTf.getText(), nameTf.getText(), addrTf.getText());
}
public Customer getCustOnPortPan(){
// optional TBD in mod 9 by students
Customer cust = null;
return cust;
}
public void showCard(String cardStr){
System.out.println("showCard(" + cardStr +")");
card.show(cardPan, cardStr);
}
public BrokerGui() {
System.out.println("BrokerGui");
buildDisplay();
}
//private and protected methods
private void buildDisplay(){
frame = new JFrame("BrokerTool");
buildSelectionPanel(); // build selection panel
buildCustPanel(); // build customer panel
buildAllCustPanel(); // build allCustomer panel
buildLogPanel(); // build log panel
// add panels to cardPan
cardPan.setLayout(card);
cardPan.add(custPan, "customer");
cardPan.add(allCustPan, "allcustomers");
// build and display frame
contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(cardPan, BorderLayout.CENTER);
contentPane.add(selPan, BorderLayout.NORTH);
contentPane.add(logPan, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible( true );
// showCard("allcustomers");
}
//build selection panel
private void buildSelectionPanel() {
selPan.setLayout(new GridLayout(1,4));
selPan.add(custBt);
selPan.add(portBt);
selPan.add(allCustBt);
selPan.add(stockBt);
}
public void addSelectionPanelListeners(ActionListener a[]) {
int len = a.length;
if (len !=4) {
System.out.println("BrokerGui addCustPanListeners error "
+ "incorrect array len " + len);
return;
}
custBt.addActionListener(a[0]);
allCustBt.addActionListener(a[1]);
portBt.addActionListener(a[2]);
stockBt.addActionListener(a[3]);
}
//build customer panel
private void buildCustPanel() {
custPan.setLayout(new GridLayout(5,2));
custPan.add(nameLb);
custPan.add(nameTf);
custPan.add(idLb);
custPan.add(idTf);
custPan.add(addrLb);
custPan.add(addrTf);
custPan.add(getBt);
custPan.add(updBt);
custPan.add(addBt);
custPan.add(delBt);
}
public void addCustPanelListeners(ActionListener a[]) {
int len = a.length;
if (len !=4) {
System.out.println("BrokerGui addCustPanListeners error "
+ "incorrect array len " + len);
return;
}
getBt.addActionListener(a[0]);
addBt.addActionListener(a[1]);
delBt.addActionListener(a[2]);
updBt.addActionListener(a[3]);
}
//build all customer panel
private void buildAllCustPanel() {
allCustPan.setLayout(new BorderLayout());
allCustPan.add(allCustLb, BorderLayout.NORTH);
//** 1 For the JTable exercise comment following 2 lines
allCustPan.add(allCustSp, BorderLayout.CENTER);
allCustTa.setText("all customer display TBD in mod 9");
//** 2 Create a DefaultTableModel and assign it to
//** tableModel. Hint - see TableExample class
//** 3 Create a JTable and assign it to
//** table. Hint - see TableExample class
//** 4 Create a JScrollPane object to scroll the table
//** and assign it to tablePane;
//** 5 Add the tablePan to CENTER region of allCustPan
//** Hint - this line is similar but not the same as
//** the commented out code under step 1.
//Optional lines - uncomment to set size of viewport
Dimension dim = new Dimension(500, 150);
// table.setPreferredScrollableViewportSize(dim);
}
//build message log panel
private void buildLogPanel() {
logPan.setLayout(new BorderLayout());
logPan.add(logLb, BorderLayout.NORTH);
logPan.add(logSp, BorderLayout.CENTER);
}
public static void main(String args[]){
BrokerGui gui = new BrokerGui();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -