![試驗四Java事件處理汽院含答案講解_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-6/13/48daff27-5eda-4aae-af9c-c7bfe0d3bc3c/48daff27-5eda-4aae-af9c-c7bfe0d3bc3c1.gif)
![試驗四Java事件處理汽院含答案講解_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-6/13/48daff27-5eda-4aae-af9c-c7bfe0d3bc3c/48daff27-5eda-4aae-af9c-c7bfe0d3bc3c2.gif)
![試驗四Java事件處理汽院含答案講解_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-6/13/48daff27-5eda-4aae-af9c-c7bfe0d3bc3c/48daff27-5eda-4aae-af9c-c7bfe0d3bc3c3.gif)
![試驗四Java事件處理汽院含答案講解_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-6/13/48daff27-5eda-4aae-af9c-c7bfe0d3bc3c/48daff27-5eda-4aae-af9c-c7bfe0d3bc3c4.gif)
![試驗四Java事件處理汽院含答案講解_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-6/13/48daff27-5eda-4aae-af9c-c7bfe0d3bc3c/48daff27-5eda-4aae-af9c-c7bfe0d3bc3c5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實驗四 Java事件處理實驗目的1 掌握 Java語言中的事件處理方法2 掌握 Java語言中事件源、監(jiān)視器和處理事件的接口的概念實驗導讀1 Java 事件處理簡介 學習組件除了了解組件的屬性和功能外, 學習組件除了了解組件的屬性和功能外, 一個 更重要的方面是學習怎樣處理組件上發(fā)生的界面事件。 當用戶在文本框中輸入文本后按 Enter 鍵、單擊按鈕、在一個下拉列表框中選擇一個條目等操作時,都發(fā)生界面事件。在學 習處理事件時,必須很好地掌握事件源、監(jiān)視器、處理事件的接口這三個概念。事件源: 能夠產(chǎn)生事件的對象都可以成為事件源,如文本框、按鈕、下拉式列表等。也就是說, 事件源必須是一個對象,而且
2、這個對象必須是Java認為能夠發(fā)生事件的對象。監(jiān)視器:需要一個對象對事件源進行監(jiān)視, 以便對發(fā)生的事件作出處理。 事件源通過調(diào)用相應的 方法將某個對象作為自己的監(jiān)視器。例如,對于文本框,這個方法是:addActionListener( 監(jiān)視器 );對于獲取了監(jiān)視器的文本框,當文本框獲得輸入焦點之后,如果用戶按 Enter 鍵, Java 運行系統(tǒng)就自動用 ActionEvent 類創(chuàng)建一個對象,即發(fā)生了 ActionEvent 事件。也就是說,事 件源獲得監(jiān)視器后, 相應的操作就會導致事件的發(fā)生, 并通知監(jiān)視器, 監(jiān)視器就會做出相應 的處理。處理事件的接口:監(jiān)視器負責處理事件源發(fā)生的事件。 監(jiān)
3、視器是一個對象, 為了讓監(jiān)視器這個對象能對事 件源發(fā)生的事件進行處理, 創(chuàng)建該監(jiān)視器對象的類必須聲明實現(xiàn)相應的接口, 即必須在類體 中給出該接口中所有方法的方法體, 那么當事件源發(fā)生事件時, 監(jiān)視器就自動調(diào)用類實現(xiàn)的 某個接口中的方法。2 文本框上的 ActionEvent 事件java.awt.event 包中提供了許多事件類和處理各種事件的接口。對于文本框,這個接口 的名字是 ActionListener ,這個接口中只有一個方法:public void actionPerformed(ActionEvent e)當在文本框中輸入字符并按 Enter 鍵時, java.awt.event
4、包中的 ActionEvent 類自動創(chuàng)建 一個事件對象, 并將它傳遞給 actionPerformed(ActionEvent e) 方法中的參數(shù) e,監(jiān)視器自動調(diào) 用這個方法對發(fā)生的事件做出處理。所以,稱文本框這個事件源可以發(fā)生 ActionEvent 類型事件。為了能監(jiān)視到這種類型的 事件,事件源必須使用 addActionListener 方法獲得監(jiān)視器;創(chuàng)建監(jiān)視器的類必須實現(xiàn)接口 ActionListener 。只要學會了處理文本框這個組件上的事件, 其他事件源上的事件的處理也就 很容易學會,所不同的是事件源能發(fā)生的事件類型不同,所使用的接口不同而已。ActionEvent 類有如下
5、常用的方法:public Object getSource() ActionEvent 對象調(diào)用該方法可以獲取發(fā)生 ActionEvent 事 件的事件源對象的引用,即 getSource()方法將事件源上轉(zhuǎn)型為 Object 對象,并返 回這個上轉(zhuǎn)型對象的引用。public String getActionCommand() ActionEvent 對象調(diào)用該方法可以獲取發(fā)生ActionEvent 事件時,和該事件相關(guān)的一個命令字符串,對于文本框,當發(fā)生ActionEvent 事件時,文本框中的文本字符串就是和該事件相關(guān)的一個命令字符串。3 選擇框和下拉列表上的 ItemEvent 事件 選
6、擇框從未選中狀態(tài)變成選中狀態(tài)或從選中狀態(tài)變成未選中狀態(tài)時、 下拉列表選項列表 中選中某個選項時就發(fā)生 ItemEvent 事件,即 ItemEvent 類自動創(chuàng)建一個事件對象。發(fā)生 ItemEvent 事件的事件源獲得監(jiān)視器的方法是addItemListener( 監(jiān)視器 )。處理ItemEvent 事件的接口是 ItemListener ,創(chuàng)建監(jiān)視器的類必須實現(xiàn) ItemListener 接口,該接口 中只有一個方法。當選擇框發(fā)生 ItemEvent 事件時,監(jiān)視器將自動調(diào)用接口方法: itemStateChanged(ItemEvent e)對發(fā)生的事件進行處理。4 鼠標事件任何組件上都可
7、以發(fā)生鼠標事件, 如:鼠標進入組件、 退出組件、 在組件上方單擊鼠標、 拖動鼠標等都觸發(fā)組件發(fā)生鼠標事件,也就是說,組件可以成為發(fā)生鼠標事件的事件源。使用 MouseListener 接口可以處理 5 種操作觸發(fā)的鼠標事件:( 1)在事件源上按下鼠標鍵。( 2)在事件源上釋放鼠標鍵。( 3)在事件源上擊鼠標鍵。( 4)鼠標進入事件源。(5)鼠標退出事件源。鼠標事件的類型是 MouseEvent ,即當發(fā)生鼠標事件時, MouseEvent 類自動創(chuàng)建一個事 件對象。MouseListener 接口中的方法:( 1)mousePressed(MouseEvent) 負責處理在組件上按下鼠標觸發(fā)的
8、鼠標事件, 當在組 件上按下鼠標時,監(jiān)視器將自動調(diào)用接口中的這個方法對事件作出處理。( 2) mouseReleased(MouseEvent) 負責處理在組件上釋放鼠標觸發(fā)的鼠標事件,當在組件上釋放鼠標時,監(jiān)視器將自動調(diào)用接口中的這個方法對事件作出處理。(3) mouseEntered(MouseEvent) 負責處理鼠進入組件觸發(fā)的鼠標事件,當鼠標進入 組件上方時,監(jiān)視器將自動調(diào)用接口中的這個方法對事件作出處理。( 4) mouseExited(MouseEvent) 負責處理鼠標離開組件觸發(fā)的鼠標事件,當鼠標離開 組件時,監(jiān)視器自動調(diào)用接口中的這個方法對事件作出處理。( 5) mouse
9、Clicked(MouseEvent) 負責處理在組件上單擊或連擊鼠標觸發(fā)的鼠標事件, 當單擊或連擊鼠標時,監(jiān)視器自動調(diào)用接口中的這個方法對事件作出處理。MouseMotionListener 接口:使用 MouseMotionListener 接口可以處理以下兩種操作觸發(fā)的鼠標事件:( 1)在事件源上拖動鼠標。( 2)在事件源上移動鼠標。MouseMotionListener 接口中有如下方法:( 1) mouseDragged(MouseEvent) 負責處理在組件上拖動鼠標觸發(fā)的鼠標事件,當在 組件上拖動鼠標時,監(jiān)視器調(diào)用接口中的這個方法對事件作出處理。( 2) mouseMoved(M
10、ouseEvent) 負責處理在組件上運動鼠標觸發(fā)的鼠標事件,當在組 件上運動鼠標時,監(jiān)視器調(diào)用接口中的這個方法對事件作出處理5 焦點事件組件可以觸發(fā)焦點事件。組件可以使用public void addFocusListener(FocusListener listener) 增加焦點事件監(jiān)視器。當組件獲得焦點監(jiān)視器后, 如果組件從無輸入焦點變成有輸入焦點或從有輸入焦點變成 無輸入焦點都會觸發(fā) FocusEvent 事件。創(chuàng)建監(jiān)視器的類必須要實現(xiàn)FocusListener 接口,該接口有兩個方法:public void focusGained(FocusEvent e) public void
11、 focusLost(FocusEvent e)當組件從無輸入焦點變成有輸入焦點觸發(fā) FocusEvent 事件時,監(jiān)視器調(diào)用類實現(xiàn)的接 口方法: focusGained(FocusEvent e);當組件從有輸入焦點變成無輸入焦點觸發(fā) FocusEvent 事件時,監(jiān)視器調(diào)用類實現(xiàn)的接口方法: focusLost(FocusEvent e)。用戶通過單擊組件可以使得該組件有輸入焦點,同時也使得其他組件變成無輸入焦點。 一個組件也可以調(diào)用public boolean requestFocusInWindow()方法獲得輸入焦點。Java1.2 事件模式中,必須6 鍵盤事件 當按下、釋放或敲擊鍵
12、盤上一個鍵時就發(fā)生了鍵盤事件,在要有發(fā)生事件的事件源。 當一個組件處于激活狀態(tài)時, 敲擊鍵盤上一個鍵就導致這個組件上 發(fā)生了鍵盤事件。事件源使用 addKeyListener 方法獲得監(jiān)視器。使用 KeyListener 接口處理鍵盤事件 .接口 KeyListener 中有 3 個方法:public void keyPressed(KeyEvent e),public void keyTyped(KeyEvent e),public void KeyReleased(KeyEvent e)。當按下鍵盤上某個鍵時,監(jiān)視器就會發(fā)現(xiàn),然后 keyPressed 方法會自動執(zhí)行,并且 KeyEven
13、t類自動創(chuàng)建一個對象傳遞給 keyPressed方法中的參數(shù) e。keyTyped 方法是 keyPressed 和 KeyReleased 方法的組合,當鍵被按下又釋放時, keyTyped 方法被調(diào)用。用 KeyEvent 類的 public int getKeyCode() 方法,可以判斷哪個鍵被按下、敲擊或釋放, getKeyCode方法返回一個鍵碼值。也可以用 KeyEvent類的 public char getKeyChar()判斷哪個 鍵被按下、敲擊或釋放, getKeyChar 方法返回鍵上的字符。鍵 盤 事 件 KeyEvent 對 象 調(diào) 用 getModifiers()
14、方 法, 可 以 返 回 整 數(shù) 值 ALT_MASK、 CTRL_MASK、 SHIFT_MASK,它們分別是 InputEvent 類的類常量。程序可以通過 getModifiers() 方法返回的值處理組合鍵事件。 例如,對于 KeyEvent 對象 e, 當按下 CTRL+X組合鍵時,下面的邏輯表達式為 true :e. getModifiers() = InputEvent.CTRL_MASK&e.getKeyCode() = keyEvent.VK_XJava 事 件處理 就是基于這種授 權(quán)模式 ,即發(fā)生 相應事 件的事件源對 象,比如 sourceObject,通過調(diào)用相應的方法
15、:sourceObject. addXXXListener(監(jiān)視器 );將某個對象作為自己的監(jiān)視器。創(chuàng)建監(jiān)視器對象的類必須聲明實現(xiàn)相應的事件接口:class A implements XXXListener當事件源發(fā)生事件時, 監(jiān)視器將調(diào)用接口中相應的方法做出處理。 Java 使用了接口回調(diào) 技術(shù)設計了它的處理事件模式,注意到addXXXListener(XXXListener listener) 方法中的參數(shù)是一個接口類型, listener 可以引用任何實現(xiàn)了 XXXListener 接口的類所創(chuàng) 建的對象,當事件源發(fā)生事件時,接口 listener 立刻回調(diào)類實現(xiàn)的接口中的某個方法。事件
16、的處理過程如下圖所示:發(fā)生 XXX事件監(jiān)視器回調(diào)接口方法圖 事件處理示意圖內(nèi)部類實例做監(jiān)視器:Java支持在一個類中聲明另一個類, 這樣的類成為內(nèi)部類, 而包含內(nèi)部類的類成為內(nèi)部 類的外嵌類, 外嵌類的成員變量在內(nèi)部類中仍然有效, 內(nèi)部類中的方法也可以調(diào)用外嵌類中 的方法。匿名類是內(nèi)部類的特殊情形,即省略類聲明,可以直接用類體創(chuàng)建對象,因此,可 以使用匿名內(nèi)部類的對象做監(jiān)視器。例如:xxx .addActionListener( new ActionListener() Overridepublic void actionPerformed(ActionEvent e) );實驗內(nèi)容1. 圖形
17、用戶界面設計程序 (ArtFont.java)在實驗三第 1題的基礎上, 添加事件處理機制, 并逐步完善程序功能。 分別用 ArtFont 類的對象做監(jiān)視器和匿名內(nèi)部類的對象做監(jiān)視器實現(xiàn)。要求實現(xiàn)如下功能:當在文本框中輸入文字后回車,在文本域中顯示輸入的文字。當分別選擇粗體和斜體復選框時,文本域中的文字分別顯示粗體和斜體樣式。 當點擊顏色按鈕時,出現(xiàn)顏色選擇對話框,選擇需要的顏色,按確定按鈕后, 按鈕的前景色和文本域的前景色設置為選定的顏色。當選擇字體樣式下拉框中的某一字體樣式時, 文本域中的文字設置為指定的字 體樣式。當選擇字體大小下拉框中的某一字體大小時, 文本域中的文字設置為指定的字 體
18、大小。當選擇窗體樣式下拉框中的某一窗體效果時,窗體外觀改變?yōu)橹付ǖ拇绑w外 觀。代碼提示:1) 用 ArtFont 類的對象做監(jiān)視器implementspublic class CopyOfArtFont extends JFrameActionListener, ItemListener JComboBox windowStyle; / 窗體樣式下拉框粗體按鈕顏色按鈕; / 文字輸入框JCheckBox boldBx ; / JButton colorBtn ; / JTextField inputTextprivate JPanel getNorthPanel() JPanel panel
19、=new JPanel();inputText .addActionListener( this ); colorBtn .addActionListener( this ); return panel;private JPanel getCenterPanel() JPanel panel =new JPanel();this );boldBx.addItemListener( return panel; private JPanel getSouthPanel() JPanel panel =new JPanel();windowStyle .addItemListener( this )
20、; return panel;Override public void itemStateChanged(ItemEvent e) if (e.getSource() = windowStyle ) String s = (String) e.getItem(); String className = ; if (s.equals( Windows 顯示效果 ) className =com.sun.java.swing.plaf.windows.WindowsLookAndFeel ; else if (s.equals( Unix 顯示效果 )className = com.sun.jav
21、a.swing.plaf.motif.MotifLookAndFeel ;else if (s.equals( 默認顯示效果 ) className =UIManager. getCrossPlatformLookAndFeelClassName (); try UIManager. setLookAndFeel (className); SwingUtilities. updateComponentTreeUI ( this ); catch (Exception de) System. out .println( Exception happened! );Override public
22、void actionPerformed(ActionEvent e) 2) 用匿名內(nèi)部類的對象做監(jiān)視器 public class CopyOfArtFont extends JFrame JComboBox fontType ; / 字體樣式下拉框 , JCheckBox boldBx ; / 粗體按鈕 JButton colorBtn ; / 顏色按鈕; JTextField inputText ; / 文字輸入框; private JPanel getNorthPanel() JPanel panel =new JPanel();inputText .addActionListener(
23、new ActionListener() Overridepublic void actionPerformed(ActionEvent e) ); return panel;private JPanel getCenterPanel() JPanel panel =new JPanel();boldBx .addItemListener( new ItemListener() Overridepublic void itemStateChanged(ItemEvent e) ); return panel;private JPanel getSouthPanel() JPanel panel
24、 = new JPanel();fontType .addItemListener( new ItemListener() Overridepublic void itemStateChanged(ItemEvent e) );return panel;答案代碼:import javax.swing.*;import java.awt.*;import javax.swing.event.*;import java.awt.event.*;public class ArtFont extends JFrame implements KeyListener, ChangeListener,Ite
25、mListener,ActionListenerJComboBox fontType ; / 字體樣式下拉框 ,JComboBox fontSize ; / 字體大小下拉框JComboBox windowStyle ; / 窗體樣式下拉框JCheckBox boldBx ; / 粗體按鈕JCheckBox italicBx ; / 斜體按鈕JButton colorBtn ; / 顏色按鈕; StringfontNames ; / 字體名稱 ;StringfontSizes ; / 字體大?。籎Label label ; / 輸入提示標簽; JTextField inputText ; / 文
26、字輸入框;JTextArea txtArea; / 文字顯示區(qū)JPanelJPanelJPanelnorthPanel ; / 字體設置; centerPanel ; / 顯示效果區(qū) southPanel ; / 樣式設置Font font ;int boldStyle , italicStyle , underlineStyle ;int fontSizeStyle ;String fontNameStyle ;Color colorStyle = Color. black ; / 設置字體的默認顏色為黑色 ; String style = 默認顯示效果 , Windows 顯示效果 , U
27、nix 顯示效果 ;public ArtFont() super ( 字體設置 );/ 設置默認字體 boldStyle = 0; italicStyle = 0; underlineStyle = 0; fontSizeStyle= 10;fontNameStyle = 宋體 ; font = new Font( fontNameStyle , boldStyle + italicStyle , fontSizeStyle );northPanel = getNorthPanel(); centerPanel = getCenterPanel(); southPanel = getSouth
28、Panel();/ 設置容器 ;NORTH); / / 【補充代碼】 / 將CENTER); / 【補充代碼】SOUTH);/ 【補充代碼】 /Container container = getContentPane(); container.setLayout( new BorderLayout(); container.add( northPanel ,BorderLayout. northPanel 添加到窗體的北部container.add( centerPanel ,BorderLayout. / 將 centerPanel 添加到窗體的北部container.add( southP
29、anel ,BorderLayout. 將 southPanel 添加到窗體的北部setSize(500, 300);/ 【補充代碼】 / 將窗體位于屏幕的中央setLocationRelativeTo( null ); setVisible( true ); private JPanel getNorthPanel() JPanel panel =new JPanel();label = new JLabel( 輸入 ,JLabel./ 設置輸入提示標簽 panel.add( label );/ 設置文本輸入框; inputText =new JTextField(10); panel.ad
30、d( inputText );/ inputText.addActionListener(this); boldBx =new JCheckBox( 粗體 ); panel.add( boldBx );italicBx =new JCheckBox( 斜體); panel.add( italicBx );colorBtn =new JButton( 顏色 ); panel.add( colorBtn );return panel;private JPanel getCenterPanel() JPanel panel =new JPanel();panel.setLayout(new Bord
31、erLayout();txtArea =new JTextArea();panel.add( txtArea ,BorderLayout. CENTER); return panel;private JPanel getSouthPanel() JPanel panel =new JPanel();GraphicsEnvironmentge=GraphicsEnvironment. getLocalGraphicsEnvironment();fontNames =ge.getAvailableFontFamilyNames();/ 獲得系統(tǒng)中所有字體的名字;fontType =new JCom
32、boBox( fontNames ); fontType .setEditable( false ); fontType .setMaximumRowCount(10);panel.add( fontType );fontSizes =new String63;for ( int i=0;i63;i+) fontSizes i=Integer. toString (i+10); fontSize =new JComboBox( fontSizes ); fontSize .setEditable( false ); fontSize .setMaximumRowCount(10); panel
33、.add( fontSize );windowStyle = new JComboBox( style ); panel.add( windowStyle );return panel;public static void main(String args) ArtFont artFont = new ArtFont(); artFont.action();artFont.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); public void action() inputText .addKeyListener(this);boldBx .ad
34、dChangeListener(this);italicBx .addChangeListener( this ); fontType .addItemListener(this);fontSize .addItemListener(this);windowStyle .addItemListener( this ); colorBtn .addActionListener( this ); Override public void keyTyped(KeyEvent e) Overridepublic void keyPressed(KeyEvent e) if (e.getSource()
35、= inputText ) txtArea .append( inputText .getText(); Override public void keyReleased(KeyEvent e) Override public void stateChanged(ChangeEvent e) if (e.getSource()= boldBx ) if ( boldBx .isSelected() boldStyle =Font. BOLD; else boldStyle =0; else if (e.getSource()= italicBx ) if ( italicBx .isSelec
36、ted() italicStyle =Font. ITALIC ; else italicStyle =0;font =new Font( fontNameStyle , boldStyle +italicStyle , fontSizeStyle );txtArea .setFont( font ); Override public void itemStateChanged(ItemEvent e)if (e.getSource()= fontType ) fontNameStyle =fontNames fontType .getSelectedIndex();else if (e.ge
37、tSource()= fontSize ) fontSizeStyle =fontSize .getSelectedIndex()+1;else if (e.getSource()= windowStyle ) underlineStyle =windowStyle .getSelectedIndex();font =new Font( fontNameStyle , boldStyle +italicStyle , fontSizeStyle );txtArea .setFont( font ); Override public void actionPerformed(ActionEven
38、t e)JColorChooser colorChooser=new JColorChooser();colorStyle =colorChooser. showDialog ( null , 字體顏色 , Color. BLACK);txtArea .setForeground( colorStyle );2. 日歷應用程序設計在實驗三第 2 題的基礎上,添加事件處理機制,并逐步完善程序功能。分別用CalendarFrame 類的對象做監(jiān)視器和匿名內(nèi)部類的對象做監(jiān)視器實現(xiàn)。 要求實現(xiàn)如下功能:在文本框 inputYear 中輸入年份,驗證年份的有效性;按回車鍵后,顯示輸入 年份的正確日歷單擊
39、 previousMonth 按鈕可以顯示當前月的上一月的日歷;如果月份小于1,則顯示上一年的 12 月1,則單擊 nextMonth 按鈕,可以顯示當前月的下一月的日歷;如果月份大于顯示下一年的 1 月答案代碼:CalendarBean.javaimport java.util.Calendar;public class CalendarBean String day ;int year = 2013, month = 0;public void setYear( int year) this . year = year;public int getYear() return year ;p
40、ublic void setMonth( int month) this . month = month;public int getMonth() return month ;/ 返回某年某月 1號開始的日期數(shù)組public String getCalendar() String a =new String42;Calendar 日歷 = Calendar. getInstance ();/ 注意: 1月份是從 0開始,所以要減 1日歷.set( year , month - 1, 1);int 星期幾 = 日歷.get(Calendar.DAY_OF_WEE)K - 1;int day =
41、 0;if ( month = 1 | month = 3 | month = 5 | month = 7 | month = 8 | month = 10 | month = 12) day = 31;if ( month = 4 | month = 6 |month = 9 |month = 11) day = 30;if ( month = 2) if ( year % 4 = 0) & ( year % 100 != 0) | (year % 400 =0) day = 29; else day = 28;星期幾 + day; i+) for ( int i = 星期幾 , n = 1
42、; i ai = String. valueOf (n); n+;return a;CalendarFrame.java import import import publicjava.awt.*; java.awt.event.*; javax.swing.*; class CalendarFrame extends JFrame implements ActionListener,KeyListener/*/ private Label Label Button Button Labelstatic final labelDay = labelYear ;titleName = nextM
43、onth , previousMonth showMessage ;inputYear ; calendar ;日,long serialVersionUID new Label42;new Button7;= 1L;TextField CalendarBean String name = int year = 2013, String days ; 四 , 五 , 六 ;month = 1;public CalendarFrame() calendar calendar calendar days = new CalendarBean(); .setYear( year ); .setMon
44、th( month ); calendar .getCalendar();ScrollPane scrollPane = new ScrollPane(); scrollPane.add(getCenterPanel();add( Center ,scrollPane); / 【補充代碼】 心區(qū)域add(getNorthPanel(),BorderLayout.加pNorth 在北面區(qū)域/ 窗口添加 scrollPane 在中NORTH); / 【補充代碼】 / 窗口添add(getSouthPanel(),BorderLayout. 加 pSouth 在南區(qū)域。SOUTH);private
45、Panel getNorthPanel() Panel panel = new Panel();/ 【補充代碼 】panel.setLayout( new FlowLayout(FlowLayout.showMessage =new Label( 請輸入年份: inputYear =new TextField(8); previousMonth =new Button( nextMonth);/ 【補充代碼】 / 窗口添CENTER,10,5); 上月 );=new Button( 下月 );panel.add( panel.add( panel.add( panel.add(showMess
46、age ); inputYear ); previousMonth ); nextMonth );return panel;private Panel getCenterPanel() Panel panel = new Panel(); panel.setLayout(new GridLayout(7,7);布局設置為 7行7列的 GridLayout 布局 ( int i = 0; i 7; i+) titleName i =fornew Button(/panel.add( titleName i);/namei); 【補充代碼】補充代碼】 / 將 panel 的/ panel 添加組件
47、titleNameifor( int i = 0; i 42; i+) labelDay i =new Label(panel.add( labelDay i);/, Label. CENTER); 補充代碼】 / panel 添加組件labelDayifor( int i = 0; i 42; i+) labelDay i.setText( days i);return panel; private Panel getSouthPanel() Panel panel = new Panel();/panel.setLayout(new FlowLayout(FlowLayout.CENTER
48、);年labelYear =new Label( 月份: +calendar .getYear()+ +calendar .getMonth()+ 月);panel.add( labelYear ); / 【補充代碼 】 return panel;public void displayCalender() inputYear .addKeyListener( this ); nextMonth .addActionListener( this ); previousMonth .addActionListener( this );/public void updateCalender() /f
49、or (int i = 0; i 42; i+) /labelDayi.setText();/for (int i = 0; i 11) year =year +1;month =month -11;else month =month +1;else if (e.getSource()= previousMonth )if ( month =1) year =year -1; month =month +11; else month =month -1;calendar .setYear( year );calendar .setMonth( month );labelYear .setTex
50、t( 月份: +calendar .getYear()+ 年 +calendar .getMonth()+ 月);days = calendar .getCalendar(); /updateCalender();Override public void keyTyped(KeyEvent e) Overridepublic void keyPressed(KeyEvent e)if (e.getSource()= inputYear )/if (e.getKeyCode()=KeyEvent.VK_ENTER)/year =Integer. parseInt ( inputYear .get
51、Text(); calendar .setYear( year );年labelYear .setText( 月份: +calendar .getYear()+ +calendar .getMonth()+ 月);days = calendar .getCalendar(); /updateCalender();/ Override public void keyReleased(KeyEvent e) CalendarMainClass.java public class CalendarMainClass public static void main(String args) Calen
52、darFrame frame =new CalendarFrame();frame.setTitle( 日歷應用程序 ); frame.setBounds(100, 100, 360, 300); frame.setVisible( true ); frame.displayCalender(); frame.validate();3. 英語單詞拼寫訓練程序設計編寫一個應用程序,要求如下:窗口中有一個 TextField 對象和一個按鈕對象,將這兩個對象添加到一個面板,然 后將該面板添加到窗口的北面。用戶在 TextField 對象中輸入一個英文單詞,然后回車或單擊按鈕,程序?qū)?chuàng)建若 干個標簽
53、, 其個數(shù)剛好等于英文單詞所包含的字母的標簽, 而且每個標簽上的名字剛好 是英文單詞中的一個字母。 要求將這些標簽按一行添加到一個面板中, 然后將該面板添 加到窗口的中心。用戶用鼠標單擊一個標簽后,然后按下鍵盤上的“” 、“” 鍵交換相鄰標簽上的 字母,使得這些標簽上字母的排列順序和英文單詞中字母的順序相同。請按模板要求,將代碼補充完整。程序運行效果如下:1) RandomString.javapublic class RandomString String str = ;public String getRandomString(String s) StringBuffer strBuffer =new StringBuffer(s);int m = strBuffer.length();for ( int k = 0; k m; k+) int index
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 房地產(chǎn)中介加盟合同模板
- 鋼材銷售運輸合同范本
- 辦學合同協(xié)議
- 針對個人自行采購合同模板
- 農(nóng)機買賣合同協(xié)議書樣本
- 項目承包合同協(xié)議書
- 口譯翻譯合同-純?nèi)斯しg
- 醫(yī)療器械三方合作合同協(xié)議書范本
- 進口貨物運輸預約保險合同
- 水電材料購銷簡單合同范本
- 九年級上冊-備戰(zhàn)2024年中考歷史總復習核心考點與重難點練習(統(tǒng)部編版)
- 健康指南如何正確護理蠶豆病學會這些技巧保持身體健康
- 老客戶的開發(fā)與技巧課件
- 2024建設工程人工材料設備機械數(shù)據(jù)分類和編碼規(guī)范
- 26個英文字母書寫(手寫體)Word版
- GB/T 13813-2023煤礦用金屬材料摩擦火花安全性試驗方法和判定規(guī)則
- DB31 SW-Z 017-2021 上海市排水檢測井圖集
- 日語專八分類詞匯
- GB/T 707-1988熱軋槽鋼尺寸、外形、重量及允許偏差
- GB/T 33084-2016大型合金結(jié)構(gòu)鋼鍛件技術(shù)條件
- 高考英語課外積累:Hello,China《你好中國》1-20詞塊摘錄課件
評論
0/150
提交評論