Java 程式設計基礎班 (8) 莊坤達台大電信所網路資料庫研究室 Email: doug@arbor.ee.ntu.edu.tw Class 8 1 回顧 Java Data Structure Class 8 2
Java AWT package Component: 一些 GUI 元件, 如 : Button Label 等 Container: 用來放置 GUI 元件的地方 Container 可分為兩種供使用者使用 : Frame(Window): 一個開出來的視窗 Panel: 放在 Frame 裡的畫布, 一個 Frame 裡可以有多個 Panel Class 8 3 Java Swing package 修改自 awt 提供更強大的繪圖元件功能, 以及更穩定的函式庫 Swing Container 可分為兩種供使用者使用 : JFrame(Window): 一個開出來的視窗 JPanel: 放在 JFrame 裡的畫布, 一個 JFrame 裡可以有多個 JPanel Class 8 4
第一個小程式 JFrame 的使用 見 exercise8_1 Class 8 5 JPanel 的使用 見 exercise8_2 Class 8 6
Layout Managers 把元件放在我們希望放置的地方! FlowLayout:Panel 與 Applet 預設使用的 layout 方式 BorderLayout:Windows Dialogs 與 Frames 的預設使用的 layout 方式 GridLayout CardLayout GridBagLayout Null 不設定任何 Layout Class 8 7 FlowLayout Manager line by line basis 就是說元件一個接著一個的放在上面, 放滿了就會換行, 有點像打字超過一行字數會自動換行般 Class 8 8
import java.awt.*; public class MyFlow { private Frame f; private Button btn1, btn2, btn3; public static void main( String args[] ) { MyFlow aflow = new MyFlow(); aflow.go(); public void go() { f = new Frame( "FlowLayout Manager" ); // 使用 FlowLayout Manager, 對齊方式靠左,default 對齊方式為置中 f.setlayout( new FlowLayout( FlowLayout.LEFT ) ); btn1 = new Button( "Button1" ); // 產生三個按鈕 btn2 = new Button( "Button2" ); btn3 = new Button( "Button3" ); f.add( btn1 ); // 把 Button 加到 Frame 上 f.add( btn2 ); f.add( btn3 ); f.setsize( 100, 100 ); f.setvisible( true ); Class 8 9 Class 8 10
BorderLayout Manager BorderLayout 包含五種不同區域 : North,South,Ease,West,Center 元件的相對位置不會隨 window size 的改變而改變, 但元件的 size 將可能改變 看個例子 : Class 8 11 import java.awt.*; public class MyBorder { private Frame f; private Button btnnorth, btnsouth, btneast, btnwest, btncenter; public static void main( String args[] ) { MyBorder aborder = new MyBorder(); aborder.go(); [ next ] Class 8 12
public void go() { f = new Frame( "BorderLayout Manager" ); //Frame 預設使用 BorderLayout f.setlayout( new BorderLayout() ); // 採用 BorderLayout 的方式來排列元件 btnnorth = new Button( "ButtonNorth" ); btnsouth = new Button( "ButtonSouth" ); btneast = new Button( "ButtonEast" ); btnwest = new Button( "ButtonWest" ); btncenter = new Button( "ButtonCenter"); f.add( btnnorth, "North" ); f.add( btnsouth, "South" ); f.add( btneast, "East" ); f.add( btnwest, "West" ); f.add( btncenter, "Center" ); // 將五個按鈕分別加在五個位置上 f.setsize( 300, 100 ); f.setvisible( true ); Class 8 13 Class 8 14
GridLayout Manager 讓你在畫布上打格子, 元件就可以方便的放在特定的位置裡, 每個方格的大小是一樣的 加入順序 : 由左至右 / 由上至下 看個例子 : Class 8 15 import java.awt.*; public class MyGrid { private Frame f; private Button btn11, btn12, btn13; private Button btn21, btn22, btn23; public static void main( String args[] ) { MyGrid agrid = new MyGrid(); agrid.go(); public void go() { f = new Frame( "GridLayout Manager" ); // 使用 GridLayout 方式排列元件 // 將 Frame 分成 row=3, col=2 的區塊 f.setlayout( new GridLayout( 3, 2 ) ); [ next ] Class 8 16
btn11 = new Button( "Button(1,1)" ); btn12 = new Button( "Button(1,2)" ); btn13 = new Button( "Button(1,3)" ); btn21 = new Button( "Button(2,1)" ); btn22 = new Button( "Button(2,2)" ); btn23 = new Button( "Button(2,3)" ); // 將入六個按鈕, 加入順序 : 由左至右 / 由上至下 f.add( btn11 ); f.add( btn12 ); f.add( btn13 ); f.add( btn21 ); f.add( btn22 ); f.add( btn23 ); f.setsize( 300, 200 ); f.setvisible( true ); Class 8 17 Class 8 18
AWT/Swing Component Library Class 8 19 Button package AWT_Component; import java.awt.*; public class mybutton extends Frame { public static void main( String args[] ) { mybutton btnexam = new mybutton( "Button Example" ); Panel p = new Panel(); Button btn1 = new Button( "Sample" ); p.add( btn1 ); btnexam.add( p ); btnexam.setsize( 100, 100 ); btnexam.setvisible( true ); public mybutton( String title ) { super (title); Class 8 20
Class 8 21 Checkbox Class 8 22
import java.awt.*; public class mycheckbox extends Frame { public static void main( String args[] ) { mycheckbox chkboxexam = new mycheckbox( "CheckBox Example" ); Panel p = new Panel(); Checkbox chkbox1 = new Checkbox( "One", false ); Checkbox chkbox2 = new Checkbox( "Two", false ); Checkbox chkbox3 = new Checkbox( "Three", true ); p.add( chkbox1 ); p.add( chkbox2 ); p.add( chkbox3 ); chkboxexam.add( p ); chkboxexam.setsize( 300, 100 ); chkboxexam.setvisible( true ); public mycheckbox( String title ) { super (title); Class 8 23 true Class 8 24
CheckboxGroup - Radio Button Class 8 25 import java.awt.*; public class mycheckboxgroup extends Frame { public static void main( String args[] ) { mycheckboxgroup radiobtnexam = new mycheckboxgroup("checkbox Example"); Panel p = new Panel(); CheckboxGroup cbg = new CheckboxGroup(); Checkbox radiobtn1 = new Checkbox( "One", cbg, false ); Checkbox radiobtn2 = new Checkbox( "Two", cbg, false ); Checkbox radiobtn3 = new Checkbox( "Three", cbg, true ); p.add( radiobtn1 ); p.add( radiobtn2 ); p.add( radiobtn3 ); radiobtnexam.add( p ); radiobtnexam.setsize( 3100, 100 ); radiobtnexam.setvisible( true ); public mycheckboxgroup( String title ) { super (title); Class 8 26
Class 8 27 Choice:"select one from this list" Class 8 28
import java.awt.*; public class mychoice extends Frame { public static void main( String args[] ) { mychoice choiceexam = new mychoice( "Choice Example" ); Panel p = new Panel(); Choice c = new Choice(); c.additem( "First" ); c.additem( "Second" ); c.additem( "Three" ); p.add( c ); choiceexam.add( p ); choiceexam.setsize( 100, 100 ); choiceexam.setvisible( true ); public mychoice( String title ) { super (title); Class 8 29 Class 8 30
Label Class 8 31 import java.awt.*; public class mylabel extends Frame { public static void main( String args[] ) { mylabel labelexam = new mylabel( "Label Example" ); Panel p = new Panel(); Label label1 = new Label( "Sample" ); p.add( label1 ); labelexam.add( p ); labelexam.setsize( 100, 100 ); labelexam.setvisible( true ); public mylabel( String title ) { super (title); Class 8 32
Class 8 33 TextField Class 8 34
import java.awt.*; public class mytextfield extends Frame { public static void main( String args[] ) { mytextfield txtfieldexam = new mytextfield( "TextField Example" ); Panel p = new Panel(); TextField txtfield1 = new TextField( "Sample", 20 ); p.add( txtfield1 ); txtfieldexam.add( p ); txtfieldexam.setsize( 100, 100 ); txtfieldexam.setvisible( true ); public mytextfield( String title ) { super (title); Class 8 35 Class 8 36
TextArea Class 8 37 import java.awt.*; public class mytextarea extends Frame { public static void main( String args[] ) { mytextarea txtareaexam = new mytextarea( "TextArea Example" ); Panel p = new Panel(); TextArea txtarea1 = new TextArea( "Sample", 4, 20 ); p.add( txtarea1 ); txtareaexam.add( p ); txtareaexam.setsize( 100, 100 ); txtareaexam.setvisible( true ); public mytextarea( String title ) { super (title); Class 8 38
Class 8 39 List Class 8 40
import java.awt.*; public class mylist extends Frame { public static void main( String args[] ) { mylist listexam = new mylist( "List Example" ); Panel p = new Panel(); List c = new List( 3, true ); c.additem( "First" ); c.additem( "Second" ); c.additem( "Three" ); p.add( c ); listexam.add( p ); listexam.setsize( 100, 100 ); listexam.setvisible( true ); public mylist( String title ) { super (title); Class 8 41 Class 8 42
Dialog Class 8 43 import java.awt.*; public class mydialog extends Frame { public static void main( String args[] ) { mydialog dialogexam = new mydialog( "Dialog Example" ); Dialog dialog1 = new Dialog( dialogexam, "Sample", false ); dialog1.add( new Label( "Hello, Dialog..." ), "Center" ); dialog1.pack(); dialog1.setvisible( true ); dialogexam.setsize( 100, 100 ); dialogexam.setvisible( true ); public mydialog( String title ) { super (title); Class 8 44
Class 8 45 FileDialog Class 8 46
import java.awt.*; public class myfiledialog extends Frame { public static void main( String args[] ) { myfiledialog fdialogexam = new myfiledialog( "FileDialog Example" ); FileDialog fdialog1 = new FileDialog( fdialogexam, "Sample" ); fdialog1.setvisible( true ); String fname = fdialog1.getfile(); Panel p = new Panel(); Label l1 = new Label( fname ); p.add( l1 ); fdialogexam.add( p ); fdialogexam.setsize( 100, 100 ); fdialogexam.setvisible( true ); public myfiledialog( String title ) { super (title); Class 8 47 Class 8 48
ScrollPane Class 8 49 import java.awt.*; public class myscrollpane extends Frame{ public static void main( String args[] ) { myscrollpane spexam = new myscrollpane( "ScrollPane Example" ); Panel p = new Panel(); ScrollPane sp1 = new ScrollPane( ); p.setlayout( new GridLayout( 1, 1 ) ); Button b1 = new Button( "Btn1" ); Button b2 = new Button( "Btn2" ); Button b3 = new Button( "Btn3" ); Button b4 = new Button( "Btn4" ); p.add( b1 ); p.add( b2 ); p.add( b3 ); p.add( b4 ); sp1.add( p ); spexam.setlayout( new FlowLayout() ); spexam.add( sp1 ); spexam.setsize( 100, 100 ); spexam.setvisible( true ); public myscrollpane( String title ) { super (title); Class 8 50
Class 8 51 MenuBar Menu MenuItem Class 8 52
import java.awt.*; public class mymenu extends Frame{ public static void main( String args[] ) { mymenu menuexam = new mymenu( "Menu Bar/Menu/Menu Item" ); MenuBar mb = new MenuBar(); Menu m1 = new Menu( "File" ); Menu m2 = new Menu( "Edit" ); mb.add( m1 ); mb.add( m2 ); MenuItem m1_open = new MenuItem( "Open" ); MenuItem m1_save = new MenuItem( "Save" ); m1.add( m1_open ); m1.add( m1_save ); m1.addseparator(); m1.add( m1_exit ); menuexam.setmenubar( mb ); menuexam.setsize( 100, 100 ); menuexam.setvisible( true ); public mymenu( String title ) { super (title); Class 8 53 Class 8 54
PopupMenu Class 8 55 import java.awt.*; import java.awt.event.*; public class mypopupmenu extends Frame implements ActionListener { private Button b; private PopupMenu pop1; public static void main( String args[] ) { mypopupmenu popexam = new mypopupmenu( "PopupMenu Example" ); popexam.setsize( 100, 100 ); popexam.setvisible( true ); [ next page ] Class 8 56
public mypopupmenu( String title ) { super (title); pop1 = new PopupMenu( "Popup Sample" ); MenuItem s = new MenuItem( "Save" ); MenuItem l = new MenuItem( "Load" ); b = new Button( "Press me" ); add( b, "Center" ); b.addactionlistener( this ); pop1.add( s ); pop1.add( l ); add( pop1 ); public void actionperformed( ActionEvent ev ) { pop1.show( b, 10, 10 ); Class 8 57 Class 8 58
Color Controlling 設定 component 的顏色 setforeground(): 設定前景顏色 setbackgroud(): 設定背景顏色 自訂顏色 : RGB 三個參數 Red, Green, Blue, 範圍 :0 ~ 255 int r=255, g=255, b=0; Color c = new Color( r, g, b ); Class 8 59 import java.awt.*; public class mycolor extends Frame { public static void main( String args[] ) { mycolor btnexam = new mycolor( "Color Example" ); Panel p = new Panel(); Color newcolor = new Color( 200, 100, 0 ); Button btn1 = new Button( "Color ForeGround Green" ); Button btn2 = new Button( "Color BackGround Blue" ); Button btn3 = new Button( "Color BackGround User Define(200, 100, 0)" ); btn1.setforeground( Color.yellow ); btn2.setbackground( Color.blue ); btn3.setbackground( newcolor ); p.add( btn1 ); p.add( btn2 ); p.add( btn3 ); btnexam.add( p ); btnexam.setsize( 100, 100 ); btnexam.setvisible( true ); public mycolor( String title ) { super (title); Class 8 60
Class 8 61 Font Controlling 設定 component 上的字體, 包括 (Font Name/Style/Size) Class 8 62
import java.awt.*; public class myfont extends Frame { public static void main( String args[] ) { myfont btnexam = new myfont( "Font Example" ); Panel p = new Panel(); Font f1 = new Font( "Dialog", Font.BOLD, 25 ); Font f2 = new Font( "Courier", Font.ITALIC, 35 ); Font f3 = new Font( "TimesRoman", Font.PLAIN, 45 ); Button btn1 = new Button( "Font Dialog/BOLD/25 " ); Button btn2 = new Button( "Font Courier/ITALIC/35" ); Button btn3 = new Button( "Font TimesRoman/PLAIN/45" ); btn1.setfont( f1 ); btn2.setfont( f2 ); btn3.setfont( f3 ); p.add( btn1 ); p.add( btn2 ); p.add( btn3 ); btnexam.add( p ); btnexam.setsize( 100, 100 ); btnexam.setvisible( true ); public myfont( String title ) { super (title); Class 8 63 Class 8 64