1 / 16

第十七章 AWT 的其它元件

// app17_1, 建立 選擇表單 import java.awt.*; class app17_1 extends Frame { static app17_1 frm=new app17_1(); static List lst=new List(); // 建立 選擇表單物件 lst public static void main(String args[]) { frm.setLayout(new FlowLayout(FlowLayout.CENTER,10,25));

grover
Download Presentation

第十七章 AWT 的其它元件

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. // app17_1, 建立選擇表單 import java.awt.*; class app17_1 extends Frame { static app17_1 frm=new app17_1(); static List lst=new List(); // 建立選擇表單物件lst public static void main(String args[]) { frm.setLayout(new FlowLayout(FlowLayout.CENTER,10,25)); frm.setTitle("List class"); for(int i=0;i<=10;i++) // 利用for迴圈加入選項 lst.add("List Item "+i); lst.select(2); // 選取索引值為2的選項 frm.setSize(200,150); frm.add(lst); frm.setBackground(Color.yellow); frm.setVisible(true); System.out.println("lst.getRows()= "+lst.getRows()); System.out.println("lst.getItemCount()= "+lst.getItemCount()); } } 第十七章 AWT的其它元件

  2. // app17_2, 選擇表單的事件處理範例 import java.awt.*; import java.awt.event.*; public class app17_2 extends Frame implements ItemListener { static app17_2 frm=new app17_2( ); static List lst=new List(); // 建立選擇表單物件lst public static void main(String args[]) { frm.setLayout(new FlowLayout(FlowLayout.CENTER,10,25)); frm.setTitle("Select a color"); lst.add("yellow"); // 加入選項到lst內 lst.add("orange"); lst.add("pink"); lst.add("cyan"); frm.setSize(200,150); frm.add(lst); lst.addItemListener(frm); //設定frm為lst的傾聽者 frm.setVisible(true); }

  3. public void itemStateChanged(ItemEvent e) //事件處理的程式碼 { String color=lst.getSelectedItem(); //取得被選取之選項名稱 if(color=="yellow") // 如果選項名稱為yellow frm.setBackground(Color.yellow); else if(color=="orange") //如果選項名稱為orange frm.setBackground(Color.orange); else if(color=="pink") //如果選項名稱為pink frm.setBackground(Color.pink); else if(color=="cyan") //如果選項名稱為cyan frm.setBackground(Color.cyan); frm.setTitle("you select "+color); // 設定視窗frm的標題 } }

  4. // app17_3, 建立選擇表單 import java.awt.*; public class app17_3 extends Frame { static app17_3 frm=new app17_3(); static Choice chc=new Choice( ); // 建立下拉選單物件chc public static void main(String args[]) { frm.setLayout(new FlowLayout(FlowLayout.CENTER,10,10)); frm.setTitle("Choice class"); for(int i=0;i<=4;i++) // 利用for迴圈加入選項 chc.add("Choice Item "+i); frm.setSize(200,150); frm.add(chc); frm.setBackground(Color.yellow); frm.setVisible(true); } }

  5. // app17_4, 下拉選單的事件處理範例 import java.awt.*; import java.awt.event.*; class app17_4 extends Frame implements ItemListener { static app17_4 frm=new app17_4(); static Choice chc=new Choice(); // 建立下拉表單物件chc public static void main(String args[]) { BorderLayout br=new BorderLayout(); frm.setLayout(br); frm.setTitle("Select a color"); chc.add("yellow"); // 加入選項到下拉表單物件chc裡 chc.add("orange"); chc.add("pink"); chc.add("cyan"); frm.setSize(200,150); frm.add(chc,br.NORTH); chc.addItemListener(frm); // 設定frm為chc的事件傾聽者 frm.setVisible(true); }

  6. public void itemStateChanged(ItemEvent e) { String color=chc.getSelectedItem(); if(color=="yellow") // 選擇了yellow選項 frm.setBackground(Color.yellow); else if(color=="orange") // 選擇了orange選項 frm.setBackground(Color.orange); else if(color=="pink") // 選擇了pink選項 frm.setBackground(Color.pink); else if(color=="cyan") // 選擇了cyan選項 frm.setBackground(Color.cyan); frm.setTitle("you select "+color); } }

  7. // app17_5, 建立功能表 import java.awt.*; public class app17_5 { static Frame frm=new Frame("Menu Demo"); static MenuBar mb=new MenuBar(); // 建立MenuBar物件 static Menu menu1=new Menu("Color"); // 建立Menu物件 static Menu menu2=new Menu("Exit"); static MenuItem mi1=new MenuItem("Yellow"); // 建立MenuItem物件 static MenuItem mi2=new MenuItem("Orange"); static MenuItem mi3=new MenuItem("Pink"); static MenuItem mi4=new MenuItem("Close window");

  8. public static void main(String args[]) { mb.add(menu1); // 將menu1加入mb中 mb.add(menu2); // 將menu2加入mb中 menu1.add(mi1); // 將mi1加入menu1中 menu1.add(mi2); // 將mi2加入menu1中 menu1.add(mi3); // 將mi3加入menu1中 menu2.add(mi4); // 將mi4加入menu2中 frm.setSize(200,150); frm.setMenuBar(mb); // 設定frm的功能表為mb frm.setVisible(true); } }

  9. // app17_6, 功能表的事件處理範例 import java.awt.*; import java.awt.event.*; public class app17_6 extends Frame implements ActionListener { static app17_6 frm=new app17_6(); static Label lab=new Label("Hello Java",Label.CENTER); static MenuBar mb=new MenuBar(); // 建立MenuBar物件 static Menu menu1=new Menu("Style"); static Menu menu2=new Menu("Exit"); static MenuItem mi1=new MenuItem("Plane"); static MenuItem mi2=new MenuItem("Bold"); static MenuItem mi3=new MenuItem("Italic"); static MenuItem mi4=new MenuItem("Close window"); public static void main(String args[]) { frm.setTitle("Menu Demo"); mb.add(menu1); mb.add(menu2); menu1.add(mi1); menu1.add(mi2); menu1.add(mi3); menu2.add(mi4);

  10. mi1.addActionListener(frm); // 設定frm為mi1的事件傾聽者 mi2.addActionListener(frm); // 設定frm為mi2的事件傾聽者 mi3.addActionListener(frm); // 設定frm為mi3的事件傾聽者 mi4.addActionListener(frm); // 設定frm為mi4的事件傾聽者 lab.setFont(new Font("Dialog",Font.PLAIN,24)); frm.add(lab); frm.setSize(280,150); frm.setMenuBar(mb); frm.setVisible(true); } public void actionPerformed(ActionEvent e) // 事件處理的程式碼 { MenuItem mi=(MenuItem) e.getSource(); // 取得觸發事件的物件 if(mi==mi1) // mi1觸發事件 lab.setFont(new Font("Dialog",Font.PLAIN,24)); else if(mi==mi2) // mi2觸發事件 lab.setFont(new Font("Dialog",Font.BOLD,24)); else if(mi==mi3) // mi3觸發事件 lab.setFont(new Font("Dialog",Font.ITALIC,24)); else if(mi==mi4) // mi4觸發事件 frm.dispose(); // 關閉視窗 } }

  11. // app17_7, 捲軸的實例應用 import java.awt.*; import java.awt.event.*; public class app17_7 extends Frame implements AdjustmentListener { static app17_7 frm=new app17_7(); static Scrollbar scr=new Scrollbar(); // 建立垂直捲軸scr static Label lab1=new Label("Hello Java",Label.CENTER); static Label lab2=new Label("size=20",Label.CENTER); public static void main(String args[]) { BorderLayout br=new BorderLayout(5,5); frm.setTitle("Scrollbar Demo"); frm.setSize(200,150); scr.addAdjustmentListener(frm); // 以frm當成scr的傾聽者 scr.setValues(20,4,12,40); // 設定scr的相關數值

  12. frm.add(scr,br.EAST); frm.add(lab1,br.CENTER); frm.add(lab2,br.SOUTH); lab1.setFont(new Font("Dialog",Font.PLAIN,20)); lab2.setBackground(Color.orange); frm.setVisible(true); } public void adjustmentValueChanged(AdjustmentEvent e) { int size=scr.getValue(); // 取得scr的數值 lab1.setFont(new Font("Dialog",Font.PLAIN,size));// 設定字型樣式 lab2.setText("size="+size); // 顯示字體大小 } }

  13. // app17_8, 對話方塊的實例應用 import java.awt.*; import java.awt.event.*; public class app17_8 extends Frame implements ActionListener { static app17_8 frm=new app17_8(); static Dialog dlg=new Dialog(frm); // 建立Dialog物件dlg static Button Close_btn=new Button("Close"); // Close 按鈕 static Button Cancel_btn=new Button("Cancel"); // Cancel按鈕 static WinLis wlis=new WinLis(); // 建立傾聽者物件wlis public static void main(String args[]) { frm.setTitle("Dialog Demo"); frm.setSize(200,150); dlg.setTitle("Are you sure?"); // 設定對話方塊的標題 dlg.setSize(140,100); //設定對話方塊的大小 dlg.setLayout(new FlowLayout(FlowLayout.CENTER,5,30)); dlg.add(Close_btn); // 將Close_btn加入對話方塊中 dlg.add(Cancel_btn); // 將Cancel_btn加入對話方塊中

  14. Cancel_btn.addActionListener(frm); // 設定Cancel_btn的傾聽者 Close_btn.addActionListener(frm); // 設定Close_btn的傾聽者 frm.addWindowListener(wlis); // 設定frm的傾聽者 frm.setVisible(true); } static class WinLis extends WindowAdapter { public void windowClosing(WindowEvent e) // 按下視窗關閉鈕時 { dlg.setLocation(80,30); // 設定對話方塊的位置 dlg.show(); // 顯示對話方塊 } } public void actionPerformed(ActionEvent e) // 按下對話方塊上的按鈕時 { Button btn=(Button) e.getSource(); // 取得被按下的按鈕 if(btn==Close_btn) // 如果是Close按鈕被按下 { dlg.dispose(); // 關閉對話方塊 frm.dispose(); // 關閉視窗 } else if (btn==Cancel_btn) // 如果是Cancel按鈕被按下 dlg.hide(); // 隱藏對話方塊 } }

  15. // app17_9, FileDialog類別的使用 import java.awt.*; import java.awt.event.*; import java.io.*; public class app17_9 extends Frame implements ActionListener { static app17_9 frm=new app17_9(); static FileDialog fdlg=new FileDialog(frm,"Open"); // 建立fdlg物件 static Button btn=new Button("Open"); static TextArea txa=new TextArea(); public static void main(String args[]) { BorderLayout br=new BorderLayout(); frm.setTitle("Dialog Demo"); frm.setLayout(br); frm.setSize(200,150); frm.add(txa,br.CENTER); frm.add(btn,br.SOUTH); btn.addActionListener(frm); // 設定frm為btn的事件傾聽者 frm.setVisible(true); }

  16. // 當Open按鈕按下時,執行下列的程式碼 public void actionPerformed(ActionEvent e) { fdlg.setVisible(true); // 顯示檔案對話方塊 // 以下的程式碼是按下檔案對話方塊內的「開啟」鈕才會執行 String fname=fdlg.getDirectory()+fdlg.getFile();// 取得路徑與名稱 try{ FileInputStream fi=new FileInputStream(fname); byte ba[]=new byte[fi.available()]; fi.read(ba); // 讀入檔案內容到byte陣列裡 txa.setText(new String(ba)); // 將byte陣列的內容寫到txa元件裡 fi.close(); } catch(IOException ioe){ }; } }

More Related