第10章 小程序.ppt_第1頁
第10章 小程序.ppt_第2頁
第10章 小程序.ppt_第3頁
第10章 小程序.ppt_第4頁
第10章 小程序.ppt_第5頁
已閱讀5頁,還剩58頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、1,第10章 小程序,本章主要講述如下內(nèi)容: applet程序的基本知識; applet程序的生命周期和常用方法; 輸出中的顏色控制; 組件和容器之間的關(guān)系; 采用Swing和AWT分別設(shè)計(jì)常用組件:按鈕、標(biāo)簽、文本行、文本域、選擇框、下拉列表和列表。,2,10.1.1 小程序與應(yīng)用程序的區(qū)別,applet程序至少要用到兩個(gè)包:java.awt和java.applet(或javax.swing)。 applet必須繼承類Applet或JApplet。繼承Applet或JApplet的類是程序主類,前面加public。 applet程序由瀏覽器在調(diào)用網(wǎng)頁時(shí)執(zhí)行,并以圖形方式輸出結(jié)果。且瀏覽器從主

2、類開始執(zhí)行。 每個(gè)applet程序必須有一個(gè)HTML文件,作為其標(biāo)簽。,3,10.1.2 小程序標(biāo)簽的語法格式,HTML文件的常用格式如下: VSPACE=pixels HSPACE=pixels ,import java.awt.*; import java.applet.*; public class sayhello1 extends Applet / 程序10-2 String str; public void paint(Graphics g) str=getParameter(who); / 獲得參數(shù)who if(str=null) str=; / 若獲取參數(shù)失敗 g.drawSt

3、ring(Hello Java !,30,30); g.drawString(str,30,50); str=getParameter(date); if(str=null) str=; g.drawString(str,60,70); ,HTML文件sayhello1.html,內(nèi)容如下: ,6,10.2 小程序的生命周期,小程序的生命周期是指一個(gè)applet程序從被下載起,到被系統(tǒng)回收所經(jīng)歷的過程。,7,10.2 小程序的生命周期(續(xù)),public void init( ):其任務(wù)是初始化,并且這個(gè)方法在小程序的生命周期中,僅被調(diào)用一次。例如修改sayhello1如下:,public c

4、lass exam extends Applet String str1,str2; public void init( ) / 比上例好 str1=getParameter(who); str2=getParameter(date); if(str1=null) str1=; if(str2=null) str2=; public void paint(Graphics g) g.drawString(Hello Java !,30,30); g.drawString(str1,30,50); g.drawString(str2,60,70); ,9,10.2 小程序的生命周期(續(xù)),2.

5、public void start( ):在init( )方法執(zhí)行后,就自動調(diào)用start( )方法。一般在start方法中實(shí)現(xiàn)線程的啟動工作。 3.public void stop( ):該方法假結(jié)束applet程序。 4.public void destroy( ):這是一個(gè)真正結(jié)束applet程序生命的方法,該方法釋放分配給applet的資源。,10,10.2 小程序的生命周期(續(xù)),5.public void paint( Graphics g ):在網(wǎng)頁上輸出applet程序的執(zhí)行結(jié)果。 6.public void update( Graphics g ):先用背景色填充Web頁面,

6、以達(dá)到清除畫面的目的,然后自動調(diào)用paint( )方法重新輸出。 7.public void repaint( ):強(qiáng)制小程序重新輸出,本質(zhì)上通過調(diào)用update( )方法實(shí)現(xiàn)。例如:,程序10-4是一個(gè)時(shí)鐘小程序,每秒更新一次。 import java.awt.*; import java.applet.*; import java.util.Date; / 獲取當(dāng)前時(shí)間 import java.text.DateFormat; / 將時(shí)間轉(zhuǎn)換為字符串 public class clock extends Applet implements Runnable DateFormat timeF

7、ormat; Thread timer; / 更新時(shí)間的線程 boolean running; / 停止線程的運(yùn)行,public void init( ) timeFormat=DateFormat.getDateTimeInstance( ); public void start( ) running=true; / 如果還沒有啟動線程,創(chuàng)建一個(gè)時(shí)間線程 if(timer=null) timer=new Thread(this); timer.start( ); ,public void run( ) while(running) showStatus(timeFormat.format(n

8、ew Date( ); try Thread.sleep(1000); / 睡眠1秒 catch(InterruptedException e) System.exit(0); timer=null; public void stop( ) running=false; timer.stop( ); / 終止線程 ,14,10.3 小程序常用方法,java.awt包中的Graphics類,提供的方法可以輸出字符、輸出圖形和圖像、設(shè)置字體和顏色。 Graphics類的特殊之處:該類對象有系統(tǒng)自動創(chuàng)建,通過該對象(常用的名稱g)直接調(diào)用Graphics類的各種方法。,15,10.3.1 常用的輸出

9、方法,輸出字符的方法: 1. drawString(String str, int x, int y) 2. drawChars(char data , int offset, int n, int x, int y) 3. drawBytes(byte data , int offset, int length, int x, int y),16,10.3.1 常用的輸出方法(續(xù)),輸出圖形方法: 1.public void drawLine( int x1, int y1, int x2, int y2 ) 2.public void drawRect( int x, int y, int

10、 width, int height ) 3.public void drawOval( int x, int y, int width, int height ) 4.public void fillRect( int x, int y, int width, int height ) 5.public void fillOval( int x, int y, int width, int height ) 6.public void clearRect( int x, int y, int width, int height ),17,10.3.1 常用的輸出方法(續(xù)),輸出圖形方法: 7

11、.public void drawRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight ) 8.public void fillRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight ) 9.public void draw3DRect( int x, int y, int width, int height, boolean raised ) 10.public void fill3DRect( in

12、t x, int y, int width, int height, boolean raised ),18,10.3.2 輸出中的顏色控制,直接使用Color類中提供的靜態(tài)顏色變量; 通過三元組的形式RGB值(紅綠藍(lán))來描述顏色。 13種顏色常量的RGB值如下:,Color.black (0,0,0) 黑色 Color.blue (0,0,255) 藍(lán)色 Color.cyan (0,255,255) 青色 Color.darkGray (64,64,64) 深灰色 Color.gray (128,128,128) 灰色 Color.green (0,255,0) 綠色 Color.light

13、Gray (192,192,192) 淺灰色 Color.magenta (255,0,255) 紫紅色 Color.orange (255,200,0) 橙色 Color.pink (255,175,175) 粉紅色 Color.red (255,0,0) 紅色 Color.white (255,255,255) 白色 Color.yellow (255,255,0) 黃色,20,10.3.2 輸出中的顏色控制(續(xù)),public Color(int r, int g, int b):參數(shù)為0255之間的整數(shù),分別代表紅、綠、藍(lán)顏色的含量。 public void setColor( Col

14、or c ):采用c設(shè)置顏色。 public Color getColor( ):返回當(dāng)前設(shè)置的顏色。 程序10-6采用顏色輸出圖形。,/ 程序10-6 import java.awt.*; import java.applet.*; public class drawGraphics extends Applet public void paint(Graphics g) for(int i=0;i6;i+) / 每次輸出一種圖形 showGraphics(i , g); ,public void showGraphics(int n,Graphics g) g.clearRect(0,0,

15、200,200); switch(n) case 0: / 輸出一個(gè)黑色直線 g.setColor(Color.black); g.drawLine(30,30,160,160); break; case 1: / 輸出一個(gè)藍(lán)色空心矩形 g.setColor(Color.blue); g.drawRect(30,30,160,160); break;,case 2: / 輸出一個(gè)深灰色圓角矩形 g.setColor(Color.darkGray); g.drawRoundRect(30,30,160,160,20,120); break; case 3: / 輸出一個(gè)綠色橢圓 g.setCol

16、or(Color.green); g.drawOval(30,30,160,160); break; case 4: / 以填充方式輸出一個(gè)橙色橢圓 g.setColor(Color.orange); g.fillOval(30,30,160,160); break;,case 5: / 輸出一個(gè)凸起的三維矩形 g.setColor(Color.magenta); g.draw3DRect(30,30,160,160,true); break; try / 為了便于觀察,讓程序睡眠2秒 Thread.sleep(2000); catch(InterruptedException e) Syst

17、em.exit(0); ,25,10.4 常用組件,Java組件的類型:AWT和Swing。 對于應(yīng)用程序建議采用Swing組件,對于小程序采用AWT組件。,26,10.4.1 組件和容器的關(guān)系,組件是構(gòu)成GUI的基本元素:按鈕、標(biāo)簽、畫布、復(fù)選框 等; 容器是一種包含對象。組件必須放到容器對象中。容器可以容納其他組件和容器。常用容器:框架、窗口、面板等。 組件和容器之間的關(guān)系如圖10.2。,a,28,10.4.2 按鈕,AWT的Button類的構(gòu)造函數(shù): 1. public Button( ) 2. public Button( String label ) Button類的主要方法有: 1

18、. public void setLabel( String label ) 2. public String getLabel( ),29,10.4.2 按鈕(續(xù)),3. public void addActionListener( ActionListener l ) 將l對象指定為按鈕的監(jiān)聽者。 4. public void removeActionListener( ActionListener l ) 將參數(shù)l對象從監(jiān)聽者中去掉。 5. protected void processActionEvent( ActionEvent e ) 處理按鈕產(chǎn)生的ActionEvent類型的事件

19、。 6. protected void processEvent( AWTEvent e ) 處理按鈕產(chǎn)生的所有類型的事件。,30,10.4.2 按鈕(續(xù)),JButton是Swing提供的一個(gè)類,例如:,import java.applet.Applet; import java.awt.*; / 程序10-7 import javax.swing.*; public class showButton extends Applet Button b1; JButton b2; public void init( ) b1=new Button(Button1); b2=new JButton

20、(Button2); add(b1); add(b2); ,31,10.4.3 標(biāo)簽,標(biāo)簽是用來顯示一個(gè)單行文本的組件; AWT用Label表示,Swing 用Jlabel表示; Label類常用的方法: 1. public Label( ) 2. public Label(String S) 3. public Label(String S, int alignment)生成一個(gè)帶有指定文本和對齊方式的標(biāo)簽。,32,10.4.3 標(biāo)簽(續(xù)),對齊方式:左、右和居中,對應(yīng)的靜態(tài)常量Label.LEFT、Label.RIGHT、Label.CENTER; 4. public String get

21、Text( ) 5. public void setText(String S) 6. public String getAlignment( ),33,10.4.3 標(biāo)簽(續(xù)),JLabel類的對齊方式同上,三個(gè)靜態(tài)常量JLabel.LEFT、JLabel.RIGHT、JLabel.CENTER。 例如程序10-8:,public class showLabel extends Applet / 程序10-8 private Label L1; private JLabel L2; public void init( ) L1=new Label( ); L1.setText(“AWT標(biāo)簽”

22、); L1.setAlignment(Label.LEFT); L2=new JLabel(Swing標(biāo)簽,JLabel.RIGHT); setLayout(new GridLayout(3,3); add(L1); add(L2); ,35,10.4.4 文本行,文本行是一個(gè)單行的文本域,可接受從鍵盤輸入的信息; TextField類的常用方法: 1. public TextField( ):3個(gè)字符的空文本行。 2. public TextField(int cols) 3. public TextField(String text) 4. public TextField(String

23、text, int cols),36,10.4.4 文本行(續(xù)),5. public void addActionListener( ActionListener l ) 6. public void removeActionListener( ActionListener l ) 7. public void setEchoChar( char c ):設(shè)置用戶輸入的響應(yīng)字符,防止他人偷看。 8. public char getEchoChar( ):獲取響應(yīng)字符。,37,10.4.4 文本行(續(xù)),JTextField類的構(gòu)造函數(shù): 1. public JTextField( ) 2. p

24、ublic JTextField(int cols) 3. public JTextField(String text) 4. public JTextField(String text, int cols),38,10.4.4 文本行(續(xù)),JTextField類的一個(gè)子類JPasswordField,提供了掩蓋輸入文本的方法。 例如程序10-9 :,/ 程序10-9 import java.awt.*; import java.applet.Applet; import javax.swing.*;,public class Applet_1 extends Applet private

25、TextField t1; private JPasswordField pass; public void init( ) t1=new TextField(20); t1.setEchoChar(*); / 設(shè)置t1的顯示字符 pass=new JPasswordField(20); pass.setEchoChar(#); / 設(shè)置pass的顯示字符 add(new TextField(20); add(t1); add(new JTextField(Hello,20); add(pass); ,40,10.4.5 文本域,AWT的TextArea類可生成一個(gè)多行的文本域,內(nèi)容超出顯示范

26、圍時(shí),具有滾動顯示的功能。 TextArea類的構(gòu)造函數(shù): 1. public TextArea( ) 2. public TextArea( String text ) 3. public TextArea( int rows, int columns ) 4. public TextArea( String text, int rows, int columns ) 5. public TextArea( String text, int rows, int columns, int scrollbars ),41,10.4.5 文本域(續(xù)),滾動方式采用四個(gè)靜態(tài)常量表示:SCROLLBA

27、RS_NONE、SCROLLBARS_VERTICAL_ONLY、SCROLLBARS_HORIZONTAL_ONLY 、SCROLLBARS_BOTH。 TextArea類的常用方法: 1. public void append( String str ) 2. public int getColumns( ),42,10.4.5 文本域(續(xù)),3. public int getRows( ) 4. public int getScrollbarVisibility( ) 返回滾動方式 5. public void insert( String str, int pos ) 6. publi

28、c void replaceRange( String str, int start, int end ) 7. public void setColumns( int columns ) 8. public void setRows( int rows ),43,10.4.5 文本域(續(xù)),JTextArea與TextField類似; JTextArea和JList組件一樣,必須放在JScrollpane中。 例如采用文本域顯示文本文件的內(nèi)容。,/ 程序10-10 import java.awt.*; import javax.swing.*; import javax.swing.text

29、.*; import java.io.*;,public class testJArea public static void main(String args ) testJArea obj=new testJArea ( ); obj.testArea( ); public void testArea( ) JFrame frame=new JFrame(Test); Container contentPane=frame.getContentPane( ); JTextArea ta=new JTextArea(5,30); / 支持滾動的容器組件 JScrollPane pane=ne

30、w JScrollPane(ta); contentPane.add(pane,BorderLayout.CENTER);,try / 打開文件 Reader reader=new FileReader(d:/ testJArea.java); ta.read(reader, null ); / null 是輔助性參數(shù) catch(Exception e) / 對應(yīng)文件處理失敗處理 System.exit(0); frame.setSize(200,200); / 設(shè)置框架大小 frame.show( ); / 顯示框架 ,程序運(yùn)行結(jié)果:,47,10.4.6 選擇框,復(fù)選框JCheckBox的

31、常用構(gòu)造函數(shù): 1. public JCheckBox( ) 初始化狀態(tài)是未選中。 2. public JCheckBox(Icon icon) 3. public JCheckBox(Icon icon,boolean selected) 4. public JCheckBox(String S) 5. public JCheckBox(String S,boolean selected),48,10.4.6 選擇框(續(xù)),單選框JRadioButton的構(gòu)造函數(shù): 1. public JRadioButton( ) 2. public JRadioButton(Icon icon) 3.

32、public JRadioButton(Icon icon,boolean selected) 4. public JRadioButton(String S) 5. public JRadioButton(String S,boolean selected),49,10.4.6 選擇框(續(xù)),ButtonGroup類:將相互獨(dú)立的單選框構(gòu)成一組,常用方法: 1. public ButtonGroup( ) 2. public void add( AbstractButton b):將一個(gè)按鈕組件添加到ButtonGroup組件中。 3. public int getButtoncount(

33、) 4. public void remove( AbstractButton b),程序10-11演示了幾種選擇框的用法。 import java.awt.*; import java.applet.Applet; import javax.swing.*; public class testBoxButton extends Applet JCheckBox jcb1,jcb2; / 復(fù)選框 JRadioButton jr1,jr2,jr3; / 單選框 ButtonGroup group;,public void init( ) jcb1=new JCheckBox(I love Jav

34、a,true); jcb2=new JCheckBox(I love C+,true); jr1=new JRadioButton(Lisp,true); jr2=new JRadioButton(C ,false); jr3=new JRadioButton(Ada,true); group=new ButtonGroup( ); group.add(jr2); group.add(jr3); / 不能將group加入到applet。 setLayout(new GridLayout(5,1); add(jcb1); add(jcb2); add(jr1); add(jr2); add(jr

35、3); ,程序運(yùn)行結(jié)果 :,53,10.4.7 下拉列表,AWT的下拉列表采用Chioce類實(shí)現(xiàn),常用方法: 1. public Choice( ) 2. public void addItem(String item) 3. public void addItemListener(ItemListener l) 4. public int countItems( ) 5. public String getItem(int index) 6. public int getSelectedIndex( ) 返回選中項(xiàng)的下標(biāo)。 7. public String getSelectedItem(

36、) 返回選中的項(xiàng)。,54,10.4.7 下拉列表(續(xù)),Swing的JComBox類的常用方法: 1. public JComBox( ) 2. public JComBox(Object object )創(chuàng)建一個(gè)下拉列表,采用對象數(shù)組初始化該列表。 3. 其余的方法與Chioce提供的類似,略。例如:,/ 程序10-12 import java.awt.*; import java.applet.Applet; import javax.swing.*; public class Applet_1 extends Applet Choice choice; / AWT提供的組件 JComboBox jbox; / Swing提供的組件 / String數(shù)組 String str =Ace,Deuce,Three,Four;,public void init( ) setLayout(new GridLayout(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論