




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗五 Java事件處理實(shí)驗?zāi)康? .掌握J(rèn)ava語言中的事件處理方法2 .掌握J(rèn)ava語言中事件源、監(jiān)視器和處理事件的接口的概念實(shí)驗內(nèi)容1 .圖形用戶界面設(shè)計程序(ArtFont.java)在實(shí)驗三第1題的基礎(chǔ)上,添加事件處理機(jī)制,并逐步完善程序功能。分別用ArtFont 類的對象做監(jiān)視器和匿名內(nèi)部類的對象做監(jiān)視器實(shí)現(xiàn)。要求實(shí)現(xiàn)如下功能:當(dāng)在文本框中輸入文字后回車,在文本域中顯示輸入的文字。當(dāng)分別選擇粗體和斜體復(fù)選框時,文本域中的文字分別顯示粗體和斜體樣式。當(dāng)點(diǎn)擊顏色按鈕時,出現(xiàn)顏色選擇對話框,選擇需要的顏色,按確定按鈕后, 按鈕的前景色和文本域的前景色設(shè)置為選定的顏色。當(dāng)選擇字體樣式下拉框
2、中的某一字體樣式時,文本域中的文字設(shè)置為指定的字體樣式。當(dāng)選擇字體大小下拉框中的某一字體大小時,文本域中的文字設(shè)置為指定的字體大小。當(dāng)選擇窗體樣式下拉框中的某一窗體效果時,窗體外觀改變?yōu)橹付ǖ拇绑w外觀。國字囂談s箱體 制體顯示效果,圖1程序界面運(yùn)行效果package Sy;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ArtFont extends JFrame static ArtFont artFont;JComboBox fontType;/ 字體樣式下拉框JComboBox fon
3、tSize;/ 字體大小下拉框JComboBox windowStyle;/ 窗體樣式下拉框JCheckBox boldBx;/ 粗體按鈕JCheckBox italicBx;/ 斜體按鈕JButton colorBtn;/ 顏色按鈕String fontNames;/ 字體名稱String fontSizes;/ 字體大小JLabel label;/ 輸入提示標(biāo)簽JTextField inputText;/ 文字輸入框JTextArea txtArea;/ 文字顯示區(qū)JPanel northPanel;/ 字體設(shè)置JPanel centerPanel;/ 顯示效果區(qū)JPanel southP
4、anel;/樣式設(shè)置Font font;int boldStyle, italicStyle;int fontSizeStyle;String fontNameStyle;Color colorStyle = Color.black;/ 設(shè)置字體的默認(rèn)顏色為黑色String style = " 默認(rèn)顯示效果", "Windows 顯示效果 ", "Unix 顯示效果 " ;public ArtFont() super("字體設(shè)置");/ 設(shè)置默認(rèn)字體boldStyle = 0;italicStyle = 0;fon
5、tSizeStyle = 10;fontNameStyle = " 宋體 "font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);northPanel = getNorthPanel();centerPanel = getCenterPanel();southPanel = getSouthPanel();/ 設(shè)置容器Container container = getContentPane();container.setLayout(new BorderLayout();add(nort
6、hPanel , BorderLayout.NORTH);/ 將 northPanel 添加到窗體的北部add(centerPanel , BorderLayout.CENTER);/ 將 centerPanel 添力口至U窗體的中部add(southPanel , BorderLayout.SOUTH);/ 將 southPanel添力口至U窗體的南部setSize(500, 300);setLocationRelativeTo(null);/ 將窗體位于屏幕的中央setVisible(true);private JPanel getNorthPanel() JPanel panel = n
7、ew JPanel();label = new JLabel(" 輸入 ");inputText = new JTextField(10);boldBx = new JCheckBox(" 粗體 ");italicBx = new JCheckBox(" 斜體 ");colorBtn = new JButton(" 顏色 ");inputText.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEven
8、t e) / 文本輸入txtArea.setText(inputText.getText(););boldBx.addItemListener(new ItemListener() Overridepublic void itemStateChanged(ItemEvent e) / 加粗if(e.getStateChange() = ItemEvent.SELECTED)boldStyle = 1;elseboldStyle = 0;font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);txtArea.s
9、etFont(font););italicBx.addItemListener(new ItemListener() Overridepublic void itemStateChanged(ItemEvent e) / 斜體if(e.getStateChange() = ItemEvent.SELECTED)italicStyle = 1;elseitalicStyle = 0;font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);txtArea.setFont(font););colorBtn.addA
10、ctionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / 改變顏色colorStyle = JColorChooser.showDialog(null, " 請選擇一種顏色", colorStyle);colorBtn.setForeground(colorStyle);txtArea.setForeground(colorStyle);font = new Font(fontNameStyle, boldStyle + italicStyle, fontS
11、izeStyle);txtArea.setFont(font););panel.add(label);panel.add(inputText);panel.add(boldBx);panel.add(italicBx);panel.add(colorBtn);return panel;private JPanel getCenterPanel() JPanel panel = new JPanel();panel.setLayout(new BorderLayout();txtArea = new JTextArea();panel.add(new JScrollPane(txtArea) ,
12、 BorderLayout.CENTER);return panel;private JPanel getSouthPanel() JPanel panel = new JPanel();/獲得系統(tǒng)默認(rèn)字體GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();fontNames = ge.getAvailableFontFamilyNames();fontType = new JComboBox(fontNames);/設(shè)置字體大小fontSizes = new String63;for (int
13、i = 0; i < fontSizes.length; i+) fontSizesi = Integer.toString(i+10);fontSize = new JComboBox(fontSizes);windowStyle = new JComboBox(style);fontType.addItemListener(new ItemListener() Overridepublic void itemStateChanged(ItemEvent e) / 字體的類型fontNameStyle = (String) e.getItem();font = new Font(fon
14、tNameStyle, boldStyle + italicStyle, fontSizeStyle); txtArea.setFont(font););fontSize.addItemListener(new ItemListener() Overridepublic void itemStateChanged(ItemEvent e) / 字體的大小String s = (String) e.getItem();fontSizeStyle = Integer.parseInt(s);font = new Font(fontNameStyle, boldStyle + italicStyle
15、, fontSizeStyle); txtArea.setFont(font););windowStyle.addItemListener(new ItemListener() Overridepublic void itemStateChanged(ItemEvent e) / 改變窗口String s = (String) e.getItem();String className = ""if (s.equals("Windows 顯示效果 ")className = "com.sun.java.swing.plaf.windows.Win
16、dowsLookAndFeel"else if (s.equals("Unix 顯示效果 ")className = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"else if (s.equals("默認(rèn)顯示效果")className = UIManager.getCrossPlatformLookAndFeelClassName();try UIManager.setLookAndFeel(className);SwingUtilities.updateComponent
17、TreeUI(artFont); catch (Exception de) System.out.println("Exception happened!"););panel.add(fontType);panel.add(fontSize);panel.add(windowStyle);return panel;public static void main(String args) artFont = new ArtFont();artFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);2 . 日歷應(yīng)用程序設(shè)計在實(shí)驗三
18、第2 題的基礎(chǔ)上,添加事件處理機(jī)制,并逐步完善程序功能。分別用CalendarFrame 類的對象做監(jiān)視器和匿名內(nèi)部類的對象做監(jiān)視器實(shí)現(xiàn)。要求實(shí)現(xiàn)如下功能:在文本框inputYear 中輸入年份,驗證年份的有效性;按回車鍵后,顯示輸入年份的正確日歷單擊previousMonth 按鈕可以顯示當(dāng)前月的上一月的日歷;如果月份小于1 ,則顯示上一年的 12 月單擊nextMonth 按鈕,可以顯示當(dāng)前月的下一月的日歷;如果月份大于1,則顯示下一年的 1 月CalendarBean.javapackage Sy;import java.util.Calendar;public class Calend
19、arBean String day ;int year = 2013, month = 0;public void setYear( int year ) this . year = year ;public int getYear() return year ;public void setMonth( int month ) this . month = month ;public int getMonth() return month ;/ 返回某年某月1號開始的日期數(shù)組public String getCalendar() String a =new String42;Calendar
20、 日歷= Calendar.getInstance ();/ 注意:1月份是從0 開始,所以要減1日歷 .set( year , month - 1, 1);int 星期幾 = 日歷 .get(Calendar. DAY_OF_WEE)K- 1;int day = 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 (
21、 month = 2) if ( year % 4 = 0) && ( year % 100 != 0) | ( year % 400 = 0) day = 29; else day = 28;for ( int i = 星期幾 , n = 1; i < 星期幾 + day ; i +) a i = String. valueOf ( n);n+;return a;CalendarFrame.javapackage Sy;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class
22、CalendarFrame extends Frame Label labelDay = new Label42;Label labelYear;Button titleName = new Button7;Button nextMonth, previousMonth;Label showMessage;TextField inputYear;CalendarBean calendar;String name = " 日 ", " 一", " 二", " 三", " 四", " 五
23、", " 六" ;int year = 2013, month = 1;String days;public CalendarFrame() calendar = new CalendarBean();calendar.setYear(year);calendar.setMonth(month);days = calendar.getCalendar();ScrollPane scrollPane = new ScrollPane(); scrollPane.add(getCenterPanel(); add(scrollPane, BorderLayout.CE
24、NTER);/ 窗口添加 scrollPane 在中心區(qū)域 add(getNorthPanel(), BorderLayout.NORTH);/ 窗口添加 pNorth 在北面區(qū) 域 add(getSouthPanel(), BorderLayout.SOUTH);/窗口添加 pSouth 在南區(qū)域。 private Panel getNorthPanel() Panel panel = new Panel();labelYear = new Label(" 請輸入年份: ");inputYear = new TextField(10);previousMonth = ne
25、w Button(" 上月 ");nextMonth = new Button(" 下月 "); inputYear.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) / 文本輸入 try year = Integer.parseInt(inputYear.getText(); catch (NumberFormatException e1) JOptionPane.showMessageDialog(null," 您輸入的年份有誤
26、, 請重新輸入! "); inputYear.setFocusable(true); calendar.setYear(year); calendar.setMonth(month); days = calendar.getCalendar(); for (int i = 0; i < 42; i+) labelDayi.setText(daysi); showMessage.setText(" 日歷: " + calendar.getYear() + "年"+ calendar.getMonth() + " 月 ")
27、; ); previousMonth.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) / 上月按鈕month-;if(month < 1)year-;month=1;calendar.setYear(year);calendar.setMonth(month);days = calendar.getCalendar();for (int i = 0; i < 42; i+) labelDayi.setText(daysi);showMessage.setText(&q
28、uot; 日歷: " + calendar.getYear() + "年+ calendar.getMonth() + " 月 "););nextMonth.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) / 下月按鈕month+;if(month > 12)year+;month=1;calendar.setYear(year);calendar.setMonth(month);days = calendar.getCalendar
29、();for (int i = 0; i < 42; i+) labelDayi.setText(daysi);年"showMessage.setText(" 日歷: " + calendar.getYear() + calendar.getMonth() +月 "););panel.add(labelYear);panel.add(inputYear);panel.add(previousMonth);panel.add(nextMonth);return panel;private Panel getCenterPanel() Panel panel = new Panel();panel.setLayout(new GridLayout(7, 7)
溫馨提示
- 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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 租賃店鋪的殘疾人士服務(wù)考核試卷
- 鑄造過程中的質(zhì)量管理方法創(chuàng)新與實(shí)踐案例分析考核試卷
- 銀礦市場動態(tài)監(jiān)測與投資決策分析考核試卷
- 過敏性休克病人急救護(hù)理
- 呼吸道疾病預(yù)防及措施
- 院前急救的常見護(hù)理技術(shù)
- 機(jī)場應(yīng)急救援淺析課件
- 影像學(xué)呼吸系統(tǒng)概述
- 外科手部護(hù)理標(biāo)準(zhǔn)流程
- 感染控制管理規(guī)范實(shí)施框架
- 2025年高考英語全國二卷試題含答案
- 網(wǎng)絡(luò)服務(wù)器配置與管理(微課版) 教案 項目02 虛擬化技術(shù)和VMware-2
- 國家開放大學(xué)2025年《創(chuàng)業(yè)基礎(chǔ)》形考任務(wù)3答案
- SL631水利水電工程單元工程施工質(zhì)量驗收標(biāo)準(zhǔn)第1部分:土石方工程
- 江岸區(qū)2023-2024學(xué)年下學(xué)期期末七年級數(shù)學(xué)試卷(含答案)
- 《成本會計學(xué)(第10版)》課后參考答案 張敏
- LNG加氣站質(zhì)量管理手冊
- (正式版)HGT 22820-2024 化工安全儀表系統(tǒng)工程設(shè)計規(guī)范
- 企業(yè)運(yùn)營與模擬競爭智慧樹知到期末考試答案2024年
- 提升員工質(zhì)量意識培訓(xùn)課件
- 肝硬化基本知識ppt課件
評論
0/150
提交評論