本帖最後由 ray 於 2012-4-21 20:12 編輯
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TQC303 extends JFrame implements ItemListener
{
//pnMain 包含了 pnMainA ,pnMainB
JPanel pnMain = new JPanel();
//pnMainA 包含了 pn1,pn2
JPanel pnMainA = new JPanel();
//pn1 包含了 name, chSex
JPanel pn1 = new JPanel();
JTextField name = new JTextField(5); //姓名
String sex[] = {"先生","小姐"};
JComboBox chSex = new JComboBox(sex);//性別
//pn2 包含了 chDep, chDr
JPanel pn2 = new JPanel();
String Dep[] = {"內科","外科","婦產科"};
JComboBox chDep = new JComboBox(Dep); //科別
String Dr[] = {"令狐沖","任盈盈"};
JComboBox chDr = new JComboBox(Dr); //醫師
//pn3 包含了 chWeekDay
JPanel pn3 = new JPanel();
String WeekDay[] = {"星期一早上","星期二晚上","星期三晚上","星期四下午","星期五晚上"};
JComboBox chWeekDay = new JComboBox(WeekDay); //門診時段
//pnMainB 包含了ta
JPanel pnMainB = new JPanel();
JTextArea ta = new JTextArea("尚未完成掛號", 4, 20);
//pnButton 包含了b1, b2
JPanel pnButton = new JPanel();
JButton b1 = new JButton("確定");
JButton b2 = new JButton("離開");
public TQC303()
{
super("門診掛號系統");
addWindowListener(new WinListener());
}
public static void main(String[] args)
{
TQC303 test = new TQC303();
test.setup();
test.setSize(250,270);
test.setVisible(true);
}
//建立畫面
void setup()
{
//將視覺元件放入 JPanel 中
chDep.addItemListener(this);
chDr.addItemListener(this);
pn1.add(new JLabel("姓名:"));
pn1.add(name);
pn1.add(new JLabel("性別:"));
pn1.add(chSex);
pn2.add(new JLabel("科別:"));
pn2.add(chDep);
pn2.add(new JLabel("醫師:"));
pn2.add(chDr);
pn3.add(new JLabel("門診時段:"));
pn3.add(chWeekDay);
pnMainA.setLayout(new GridLayout(0,1));
pnMainA.add(pn1);
pnMainA.add(pn2);
pnMainA.add(pn3);
pnMainB.add(ta);
pnMain.add(pnMainA);
pnMain.add(pnMainB);
b1.addActionListener(new act());
b2.addActionListener(new act());
pnButton.add(b1);
pnButton.add(b2);
Container contentPane = getContentPane();
contentPane.add(pnMain);
contentPane.add(pnButton,BorderLayout.SOUTH);
}
//依第一個下拉式選單的選項動態更改第二個下拉式選單的中的內容
public void itemStateChanged(ItemEvent e)
{
/*
* 請同學將這一部份的程式碼撰寫出來
* (1)科別有更動時醫師的下拉式選單中的內容也要隨著動態更動。
* (2)醫師有更動時門診時段的下拉式選單中的內容也要隨著動態更動。
*/
if(e.getSource() == chDep){
String s = (String)e.getItem();
chDr.removeAllItems();
if(s.equals("內科")){
chDr.addItem("令狐沖");
chDr.addItem("任盈盈");
}else if(s.equals("外科")){
chDr.addItem("楊過");
chDr.addItem("小龍女");
}else if(s.equals("婦產科")){
chDr.addItem("郭靖");
chDr.addItem("黃蓉");
}
}
else if(e.getSource() == chDr){
String s = (String)e.getItem();
chWeekDay.removeAllItems();
if( s.equals("令狐沖")){
String weekDay[]={"星期一早上","星期二晚上","星期三晚上","星期四下午","星期五晚上"};
for(int i=0 ; i<weekDay.length ; i++ ) chWeekDay.addItem(weekDay);
}else if( s.equals("任盈盈")){
String weekDay[]={"星期一下午","星期二早上","星期三下午","星期四早上","星期五下午"};
for(int i=0 ; i<weekDay.length ; i++ ) chWeekDay.addItem(weekDay);
}else if( s.equals("楊過")){
String weekDay[]={"星期一晚上","星期二晚上","星期三早上","星期四下午","星期五晚上"};
for(int i=0 ; i<weekDay.length ; i++ ) chWeekDay.addItem(weekDay);
}else if( s.equals("小龍女")){
String weekDay[]={"星期一下午","星期二早上","星期三晚上","星期四早上","星期五下午"};
for(int i=0 ; i<weekDay.length ; i++ ) chWeekDay.addItem(weekDay);
}else if( s.equals("郭靖")){
String weekDay[]={"星期一晚上","星期二下午","星期三晚上","星期四下午","星期五晚上"};
for(int i=0 ; i<weekDay.length ; i++ ) chWeekDay.addItem(weekDay);
}else if( s.equals("黃蓉")){
String weekDay[]={"星期一下午","星期二早上","星期三早上","星期四早上","星期五早上"};
for(int i=0 ; i<weekDay.length ; i++ ) chWeekDay.addItem(weekDay);
}
}
}
//建立一個 ActionListener 的 inner class
class act implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b2)
{
System.exit(0);
}
else if(e.getSource() == b1)
{
/*
* 請同學將這一部份的程式碼撰寫出來
* 讓程式可以顯示下列內容:
*
* ***先生您好
* 您掛的門診為:內科
* 門診時段為:星期一早上
* 為您看診的是:令狐沖醫師
*
*/
}
}
}
//讓視窗能夠正常關閉
class WinListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
System.exit(0);
}
}
} |