




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、實驗四 圖形用戶界面設計一、實驗目的2. 掌握圖形用戶界面程序設計的基本方法3. 掌握事件處理模型4. 使用Swing組件實現(xiàn)圖形用戶界面二、準備工作1. JDK的安裝設置:JDK/JRE/JVM;2. Eclipse集成開發(fā)環(huán)境的綠色安裝; 三、實驗描述1. 實驗類型:設計2. 實驗學時:2學時3. 實驗內(nèi)容:創(chuàng)建簡易聊天室(ChatRoom)的圖形用戶界面,并創(chuàng)建GUI 事件處理器4. 具體要求: 創(chuàng)建項目及類:類名稱:ChatClient項目:ChatRoomPrj包:默認包 組件構成:該JFrame中有六個組件:中間輸出文本域JTextArea組件,底部輸入文本域JTextField組
2、件,右邊有兩個JButton組件(Send和Quit),一個ComboBox組件用于選擇發(fā)送消息的人名(昵稱),以及一個菜單組件。 使用嵌套面板和布局管理器來幫助構造上圖所示的布局。具體如下所示: 提示:JFrame默認采用BorderLayout,JMenuBar放置在North區(qū),JTextArea放置在WEST區(qū),JTextField放置在SOUTH區(qū),CENTER區(qū)使用嵌套面板,該面板采用GridLayout(3行1列),分別添加兩個JButton以及一個JComboBox組件。 創(chuàng)建ActionListener,當按下Send按鈕時,將JComboBox組件選擇的人名,與JTextF
3、ield輸入文本域的文本一起復制到JTextArea輸出文本域。 創(chuàng)建ActionListener,在輸入文本域按下 Enter鍵時,將JComboBox組件選擇的人名(昵稱),與JTextField輸入文本域的文本復制到JTextArea輸出文本域。 提示:可以和上述 使用同一個監(jiān)聽器,以減少代碼冗余。 創(chuàng)建ActionListener,當按下Quit按鈕時,程序退出。 提示:監(jiān)聽器可以使用內(nèi)部類實現(xiàn),退出使用System.exit(0。 創(chuàng)建WindowListener,按下框架上的Close部件時,程序退出。 提示:和上述 使用同一個監(jiān)聽器,以減少代碼冗余。但是,該內(nèi)部類要同時exten
4、ds WindowAdapter 還要implements ActionListener ,以滿足兩個不同監(jiān)聽器的要求。 添加File菜單,該菜單必須包含Quit菜單項,選擇該菜單項即終止程序。 提示:和上述 和 使用同一個監(jiān)聽器,以減少代碼冗余。 添加Help菜單,該菜單必須包含About菜單項,可從該菜單項彈出一個消息對話框,顯示有關程序和開發(fā)人員的備注信息。四、實驗要求及總結1. 結合上課內(nèi)容,對上述程序先閱讀,然后上機并調試程序,并對實驗結果寫出你自己的分析結論。2. 整理上機步驟,總結經(jīng)驗和體會。3. 完成實驗報告和上交程序。實驗四:實驗結果:public class CHatrom
5、m extends JFrame implements ActionListener, ItemListener JMenuBar menubar;/ 增加組件JMenu File, Help, edit;JMenuItem send, quit, about, copy, cut, paste;JTextField text;JButton Send, Quit;JTextArea area;JComboBox choice, list;JPanel panel, panel2;/ panel1放send,quit等按鈕,panel2放菜單Color red = new Color(255,
6、 0, 0; / 使用color類創(chuàng)建color對象Color green = new Color(0, 255, 0;Color blue = new Color(0, 0, 255;Color yellow = new Color(255, 255, 0;public CHatromm( / 初始化JFramesetBounds(100, 100, 150, 150;setVisible(true;setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE;init(;public void init( / init(方法實現(xiàn)組件的創(chuàng)建和添加setTitle
7、(chating room;FlowLayout flow = new FlowLayout(;flow.setAlignment(FlowLayout.LEFT;/ 流式布局采用左對齊panel2 = new JPanel(flow;menubar = new JMenuBar(;File = new JMenu(FIle;Help = new JMenu(Help;edit = new JMenu(edit;send = new JMenuItem(send;quit = new JMenuItem(quit;about = new JMenuItem(about;copy = new J
8、MenuItem(copy;cut = new JMenuItem(cut;paste = new JMenuItem(paste;list = new JComboBox(;GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(;/ 獲取本地計算機所有可用的字體String fontmat = ge.getAvailableFontFamilyNames(;for (String str : fontmat list.addItem(str;send.setAccelerator(KeyStroke.
9、getKeyStroke(S;/ 為菜單項建立快捷鍵quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK;about.setAccelerator(KeyStroke.getKeyStroke(A;copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK;paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTR
10、L_MASK;cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK;panel2.setBackground(yellow;edit.add(cut;edit.add(copy;edit.add(paste;File.add(send;File.add(quit;File.add(edit;Help.add(about;menubar.add(File;menubar.add(Help;menubar.add(list;panel2.add(menubar;panel2.add(list;add
11、(panel2, BorderLayout.NORTH;/ 把panel2放在JFrame北面area = new JTextArea(90, 90;area.setLineWrap(true;area.setForeground(Color.black;add(new JScrollPane(area, BorderLayout.WEST;/ JTextArea放在JFrame西面text = new JTextField(Entry your chatting words., 10;text.setHorizontalAlignment(JTextField.CENTER;/ 設置JTex
12、tField文本居中顯示add(text, BorderLayout.SOUTH;/ JTextField放在JFrame南面panel = new JPanel(;panel.setLayout(new GridLayout(3, 1;/ panel1采用3行1列的布局Send = new JButton(SEND, new ImageIcon(F:/workspace/ChatRoom/src/5.jpg;/ 為按鈕增加圖片Quit = new JButton(QUIT, new ImageIcon(F:/workspace/ChatRoom/src/6.gif;Quit.setHoriz
13、ontalTextPosition(AbstractButton.CENTER;Quit.setBackground(red;/ 設置按鈕背景色Send.setBackground(green;Send.setHorizontalTextPosition(AbstractButton.CENTER;Quit.setMnemonic(q;/ 為button設置鍵盤激活方式Send.setMnemonic(s;choice = new JComboBox(;choice.setBackground(blue;choice.setForeground(yellow;choice.addItem(zh
14、aozhen;choice.addItem(zhaoyao;choice.addItem(zhangsan;choice.addItem(lisi;panel.add(Send;panel.add(Quit;panel.add(choice;add(panel, BorderLayout.CENTER;/ 注冊偵聽器Send.addActionListener(this;Quit.addActionListener(this;send.addActionListener(this;quit.addActionListener(this;text.addActionListener(this;a
15、bout.addActionListener(this;copy.addActionListener(this;cut.addActionListener(this;paste.addActionListener(this;list.addItemListener(this;public void actionPerformed(ActionEvent e Date now = new Date(;/ 定義時間對象,顯示聊天時間DateFormat d = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG;String
16、 str = d.format(now;if (e.getSource( = Send | e.getSource( = send| e.getSource( = text / 菜單項、按鈕send響應和text按entry鍵area.append(choice.getSelectedItem( + : + text.getText(+ + str + n;text.setText(null;if (e.getSource( = Quit | e.getSource( = quit / quit鍵System.exit(0;if (e.getSource( = about JOptionPane.showMessageDialog(File, write by zhaozhen,information, JOptionPane.INFORMATION_MESSAGE;if (e.getSource( = copy area.copy(;text.copy(;if (e.getSource( = cut area.cut(;text.cut(;if (e.getSource( = paste area.paste(;text.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 27《我堅持我成功》教學設計-2023-2024學年心理健康四年級下冊北師大版
- 18文言文二則《鐵杵成針》教學設計-2023-2024學年統(tǒng)編版語文四年級下冊
- 《冰融化了》教學設計-2024-2025學年科學三年級上冊教科版
- 七年級生物下冊 第三單元 第二章 人的生活需要空氣 第三節(jié) 呼吸保健與急救教學設計設計(新版)濟南版
- 2018春蘇科版八年級生物下冊第八單元第24章同步教學設計:8.24.1人體的免疫防線
- 行政工作總結課件
- 2023三年級英語上冊 Unit 4 I have a ball Lesson 20教學設計 人教精通版(三起)
- 9 端午粽 教學設計-2024-2025學年語文一年級下冊統(tǒng)編版
- Unit 2 No Rules No Order Section A(2a~2f)教學設計-2024-2025學年人教版英語七年級下冊
- Unit 9 Section B 2a-2e 教學設計2024-2025學年人教版八年級英語下冊
- 細胞課件 細胞死亡
- 嚴格執(zhí)法【知識 精講精研 】 高中政治統(tǒng)編版必修三政治與法治
- 內(nèi)科學肺炎(課件)
- 左拉精選課件
- 國際外貿(mào)模板:裝箱單
- LY/T 1831-2009人造板飾面專用裝飾紙
- 檢驗科標本采集手冊(新版)
- 人力資源開發(fā)與管理-自考課件
- 第7課《大雁歸來》課件(共41張PPT) 部編版語文八年級下冊
- 農(nóng)業(yè)面源污染進展課件
- DB44-T 2267-2021《公共機構能源資源消耗限額》-(高清現(xiàn)行)
評論
0/150
提交評論