Date: 10/18
//============================================================//
常用的 Java 圖形 Lib (java 7.0 版以後,兩種可以混用):
AWT
Swing
一般功能表是由三種類別所建立:
MenuBar
Menu
MenuItem
java.lang.Object
java.awt.MenuComponent → java.awt.MenuBar
java.awt.MenuComponent → java.awt.MenuItem ─ java.awt.Menu
設計的參數及概念:
監聽事件 (event listener) 架構圖:
//============================================================//
//app_1.java import java.awt.*; import java.awt.event.*; public class app_1 extends Frame implements ActionListener{ static app_1 frm=new app_1(); static Button btn1=new Button("Yellow"); static Button btn2=new Button("Green"); static Button btn3=new Button("Exit"); public static void main(String args[]){ btn1.addActionListener(frm); btn2.addActionListener(frm); btn3.addActionListener(frm); frm.setTitle("ActionEvent"); frm.setLayout(new FlowLayout(FlowLayout.CENTER)); frm.setSize(300,200); frm.add(btn1); frm.add(btn2); frm.add(btn3); frm.setVisible(true); //一定要show出,不然無法顯示。 frm.addWindowListener(new WindowAdapter(){ //視窗右上角x功能 public void windowClosing(WindowEvent e){ System.exit(0); } }); } public void actionPerformed(ActionEvent e){ Button btn=(Button)e.getSource();//取得事件來源物件。 if(btn==btn1) frm.setBackground(Color.YELLOW); if(btn==btn2) frm.setBackground(Color.GREEN); if(btn==btn3) System.exit(0); } }//============================================================//
//app_2.java import java.awt.Checkbox; import java.awt.CheckboxGroup; import java.awt.*; import java.awt.event.*; public class app_2 extends Frame implements ItemListener{ static app_2 frm=new app_2(); static Checkbox ckb1=new Checkbox("Java"); static Checkbox ckb2=new Checkbox("PHP"); static Label lab=new Label("Select one"); public static void main(String args[]){ CheckboxGroup grp=new CheckboxGroup(); frm.setSize(300,200); frm.setTitle("app_2 Programmer"); frm.setLayout(new FlowLayout(FlowLayout.CENTER)); ckb1.setCheckboxGroup(grp); ckb2.setCheckboxGroup(grp); lab.setBackground(Color.orange); ckb1.addItemListener((ItemListener) frm); ckb2.addItemListener((ItemListener) frm); frm.add(ckb1); frm.add(ckb2); frm.add(lab); frm.setVisible(true); frm.addWindowListener(new WindowAdapter(){ //視窗右上角x功能 public void windowClosing(WindowEvent e){ System.exit(0); } }); } @Override public void itemStateChanged(ItemEvent e) { if(ckb1.getState()==true) lab.setText("Java"); else if(ckb2.getState()==true) lab.setText("PHP"); } }//============================================================//
//app3.java import java.awt.*; import java.awt.event.*; public class app3 extends Frame implements ActionListener{ static app3 frm=new app3(); static Label lab=new Label("Hellow JAVA",Label.CENTER); static MenuBar mb=new 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"); //Dialog --> 要呼叫 show() method 才可以顯示 static Button close_btn=new Button("Close"); static Button cancel_btn=new Button("Cancel"); static Dialog dlg=new Dialog(frm); static WinLis wlis=new WinLis(); public static void main(String args[]){ mb.add(menu1); mb.add(menu2); menu1.add(mi1); menu1.add(mi2); menu1.add(mi3); menu2.add(mi4); mi1.addActionListener(frm); mi2.addActionListener(frm); mi3.addActionListener(frm); mi4.addActionListener(frm); lab.setFont(new Font("Dialog",Font.PLAIN,24)); frm.add(lab); frm.setSize(280,150); frm.setTitle("app_3 Programmer"); frm.setMenuBar(mb); frm.setVisible(true); dlg.setTitle("Are you sure?"); dlg.setSize(140,100); dlg.setLayout(new FlowLayout(FlowLayout.CENTER,5,30)); dlg.add(close_btn); dlg.add(cancel_btn); close_btn.addActionListener(frm); cancel_btn.addActionListener(frm); frm.addWindowListener(wlis); /*frm.addWindowListener(new WindowAdapter(){ //視窗右上角x功能 public void windowClosing(WindowEvent e){ System.exit(0); } });*/ } static class WinLis extends WindowAdapter{ public void windowClosing(WindowEvent e){ dlg.setLocation(80, 30); dlg.show(); } } @Override public void actionPerformed(ActionEvent e) { //MenuItem mi=(MenuItem)e.getSource(); if(e.getSource()==mi1) lab.setFont(new Font("Dialog",Font.PLAIN,30)); else if(e.getSource()==mi2) lab.setFont(new Font("Dialog",Font.BOLD,30)); else if(e.getSource()==mi3) lab.setFont(new Font("Dialog",Font.ITALIC,30)); else if(e.getSource()==mi4) frm.dispose();//關閉式窗 //Button btn=(Button)e.getSource(); else if(e.getSource()==close_btn){ dlg.dispose(); frm.dispose(); } else if(e.getSource()==cancel_btn){ dlg.hide(); //隱藏 } } }//============================================================//