參考答案
import java.awt.*; import java.awt.event.*; import java.io.*; public class JDD02 extends Frame implements ItemListener { BufferedWriter bw = null; BufferedReader br = null; //護士名字 String name[] =new String[4]; String weekDay[] = {"週一","週二","週三","週四","週五"}; //Panel pn1 包含了 Panel pn1a 及 Panel pn1b Panel pn1 = new Panel(); //pn1a 用來放排班表 Panel pn1a = new Panel(); Choice chAM[] = new Choice[5]; //週一至週五早班 Choice chPM[] = new Choice[5]; //週一至週五晚班 Choice chNigh[] = new Choice[5]; //週一至週五夜班 //pn1b 放的是每位護士的班次統計表 Panel pn1b = new Panel(); Label lname[] = new Label[4]; //顯示每位護士名字 int AM[] = new int[4]; //每一位護士早班次數 int PM[] = new int[4]; //每一位護士晚班次數 int Nigh[] = new int[4]; //每一位護士夜班次數 Label lbAM[] = new Label[4]; //顯示每一位護士早班次數 Label lbPM[] = new Label[4]; //顯示每一位護士晚班次數 Label lbNigh[] = new Label[4]; //顯示每一位護士夜班次數 //pn3 是最下方的按鈕區 Panel pn3 = new Panel(); Button b1 = new Button("輸出"); Button b2 = new Button("離開"); public JDD02() { super("急診科護士排班表"); addWindowListener(new WinListener()); } public static void main(String[] args) { JDD02 JD02 = new JDD02(); JD02.setup(); JD02.setSize(400,300); JD02.setVisible(true); } void setup() { //請將護士的名單讀入 try{ br = new BufferedReader(new FileReader("list.txt")); int i=0; while(br.ready()){ name[i++] = br.readLine().trim(); } br.close(); }catch(Exception ex) { ex.printStackTrace(); } setpn1(); setpn3(); add(pn1); add(pn3,BorderLayout.SOUTH); } //設定排班表內容 void setpn1() { pn1a.setLayout(new GridLayout(4,0)); pn1a.add(new Label(" ")); pn1a.add(new Label(weekDay[0])); pn1a.add(new Label(weekDay[1])); pn1a.add(new Label(weekDay[2])); pn1a.add(new Label(weekDay[3])); pn1a.add(new Label(weekDay[4])); pn1a.add(new Label("早班")); for(int h=0; h<chAM.length; h++) { chAM[h] = new Choice(); chPM[h] = new Choice(); chNigh[h] = new Choice(); //週一至週五夜班 //請將名單放入下拉式選單 for(int i=0;i<name.length;i++) { chAM[h].add(name[i]); chPM[h].add(name[i]); chNigh[h].add(name[i]); } chAM[h].addItemListener(this); chPM[h].addItemListener(this); chNigh[h].addItemListener(this); } pn1a.add(chAM[0]); pn1a.add(chAM[1]); pn1a.add(chAM[2]); pn1a.add(chAM[3]); pn1a.add(chAM[4]); pn1a.add(new Label("晚班")); pn1a.add(chPM[0]); pn1a.add(chPM[1]); pn1a.add(chPM[2]); pn1a.add(chPM[3]); pn1a.add(chPM[4]); pn1a.add(new Label("夜班")); pn1a.add(chNigh[0]); pn1a.add(chNigh[1]); pn1a.add(chNigh[2]); pn1a.add(chNigh[3]); pn1a.add(chNigh[4]); //設定班別統計表 pn1b.setLayout(new GridLayout(4,4)); for(int a=0; a<lname.length; a++) { lname[a] = new Label(name[a]); } pn1b.add(new Label(" ")); pn1b.add(lname[0]); pn1b.add(lname[1]); pn1b.add(lname[2]); pn1b.add(lname[3]); pn1b.add(new Label("早班")); lbAM[0] = new Label(""); lbAM[1] = new Label(""); lbAM[2] = new Label(""); lbAM[3] = new Label(""); pn1b.add(lbAM[0]); pn1b.add(lbAM[1]); pn1b.add(lbAM[2]); pn1b.add(lbAM[3]); pn1b.add(new Label("晚班")); lbPM[0] = new Label(""); lbPM[1] = new Label(""); lbPM[2] = new Label(""); lbPM[3] = new Label(""); pn1b.add(lbPM[0]); pn1b.add(lbPM[1]); pn1b.add(lbPM[2]); pn1b.add(lbPM[3]); pn1b.add(new Label("夜班")); lbNigh[0] = new Label(""); lbNigh[1] = new Label(""); lbNigh[2] = new Label(""); lbNigh[3] = new Label(""); pn1b.add(lbNigh[0]); pn1b.add(lbNigh[1]); pn1b.add(lbNigh[2]); pn1b.add(lbNigh[3]); pn1.setLayout(new GridLayout(0,1)); pn1.add(pn1a); pn1.add(pn1b); } void setpn3() { b1.addActionListener(new act()); pn3.add(b1); b2.addActionListener(new act()); pn3.add(b2); } //對Item的變動作出即時反應 public void itemStateChanged(ItemEvent e) { //先將資料清空 for(int i=0; i<AM.length; i++) { AM[i] =0; PM[i] =0; Nigh[i] = 0; } //請計算每一位護士早班次數 for(int i=0;i<chAM.length;i++) for(int j=0;j<name.length;j++) if(chAM[i].getSelectedItem().equals(name[j])) AM[j]++; //請計算每一位護士晚班次數 for(int i=0;i<chPM.length;i++) for(int j=0;j<name.length;j++) if(chPM[i].getSelectedItem().equals(name[j])) PM[j]++; //請計算每一位護士夜班次數 for(int i=0;i<chNigh.length;i++) for(int j=0;j<name.length;j++) if(chNigh[i].getSelectedItem().equals(name[j])) Nigh[j]++; //將資料填入畫面 for(int i=0; i<AM.length; i++) { lbAM[i].setText("" + AM[i]); lbPM[i].setText("" + PM[i]); lbNigh[i].setText("" + Nigh[i]); } } //建立一個用來處理按鈕的 ActionListener inner class class act implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource() == b2) { System.exit(0); } else if(e.getSource() == b1) { StringBuffer sbAM = new StringBuffer(""); StringBuffer sbPM = new StringBuffer(""); StringBuffer sbNight = new StringBuffer(""); //請撰寫這部份,將排班資料寫入檔案 try { System.out.println("Writing JDA02.txt"); bw = new BufferedWriter(new FileWriter("JDA02.txt")); bw.write("值班通知書"); bw.newLine(); bw.newLine(); for(int h=0;h<name.length;h++){ sbAM= new StringBuffer(""); sbPM=new StringBuffer(""); sbNight=new StringBuffer(""); bw.write(name[h]+"."); bw.newLine(); bw.newLine(); for(int i = 0;i<chAM.length;i++) if(chAM[i].getSelectedItem().equals(name[h])) sbAM.append(" ").append(weekDay[i]); bw.write("早班"+sbAM); bw.newLine(); for(int i = 0;i<chPM.length;i++) if(chPM[i].getSelectedItem().equals(name[h])) sbPM.append(" ").append(weekDay[i]); bw.write("晚班"+sbPM); bw.newLine(); for(int i = 0;i<chNigh.length;i++) if(chNigh[i].getSelectedItem().equals(name[h])) sbNight.append(" ").append(weekDay[i]); bw.write("夜班"+sbNight); bw.newLine(); bw.newLine(); } bw.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } /* 覆寫 WindowAdapter 中的 windowClosing() 讓關閉視窗的按鈕有作用 */ class WinListener extends WindowAdapter { public void windowClosing(WindowEvent e) { e.getWindow().dispose(); System.exit(0); } } }