實(shí)驗(yàn)五Java事件處理_第1頁(yè)
實(shí)驗(yàn)五Java事件處理_第2頁(yè)
實(shí)驗(yàn)五Java事件處理_第3頁(yè)
實(shí)驗(yàn)五Java事件處理_第4頁(yè)
實(shí)驗(yàn)五Java事件處理_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、實(shí)驗(yàn)五 Java事件處理實(shí)驗(yàn)?zāi)康? 掌握J(rèn)ava語(yǔ)言中的事件處理方法2 掌握J(rèn)ava語(yǔ)言中事件源、監(jiān)視器和處理事件的接口的概念實(shí)驗(yàn)內(nèi)容1. 圖形用戶(hù)界面設(shè)計(jì)程序(ArtFont.java)在實(shí)驗(yàn)三第1題的基礎(chǔ)上,添加事件處理機(jī)制,并逐步完善程序功能。分別用ArtFont類(lèi)的對(duì)象做監(jiān)視器和匿名內(nèi)部類(lèi)的對(duì)象做監(jiān)視器實(shí)現(xiàn)。要求實(shí)現(xiàn)如下功能:l 當(dāng)在文本框中輸入文字后回車(chē),在文本域中顯示輸入的文字。l 當(dāng)分別選擇粗體和斜體復(fù)選框時(shí),文本域中的文字分別顯示粗體和斜體樣式。l 當(dāng)點(diǎn)擊顏色按鈕時(shí),出現(xiàn)顏色選擇對(duì)話(huà)框,選擇需要的顏色,按確定按鈕后,按鈕的前景色和文本域的前景色設(shè)置為選定的顏色。l 當(dāng)選擇字體樣

2、式下拉框中的某一字體樣式時(shí),文本域中的文字設(shè)置為指定的字體樣式。l 當(dāng)選擇字體大小下拉框中的某一字體大小時(shí),文本域中的文字設(shè)置為指定的字體大小。l 當(dāng)選擇窗體樣式下拉框中的某一窗體效果時(shí),窗體外觀改變?yōu)橹付ǖ拇绑w外觀。圖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 fontSize;

3、/字體大小下拉框JComboBox windowStyle;/窗體樣式下拉框JCheckBox boldBx;/ 粗體按鈕JCheckBox italicBx;/ 斜體按鈕JButton colorBtn;/ 顏色按鈕String fontNames;/ 字體名稱(chēng)String fontSizes;/ 字體大小JLabel label;/ 輸入提示標(biāo)簽JTextField inputText;/ 文字輸入框JTextArea txtArea;/ 文字顯示區(qū)JPanel northPanel;/ 字體設(shè)置JPanel centerPanel;/ 顯示效果區(qū)JPanel southPanel;/樣式

4、設(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;fontSizeStyle =

5、10;fontNameStyle = "宋體"font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);northPanel = getNorthPanel();centerPanel = getCenterPanel();southPanel = getSouthPanel();/ 設(shè)置容器Container container = getContentPane();container.setLayout(new BorderLayout();add(northPanel , Border

6、Layout.NORTH);/將northPanel添加到窗體的北部add(centerPanel , BorderLayout.CENTER);/將centerPanel添加到窗體的中部add(southPanel , BorderLayout.SOUTH);/將southPanel添加到窗體的南部setSize(500, 300);setLocationRelativeTo(null);/將窗體位于屏幕的中央setVisible(true);private JPanel getNorthPanel() JPanel panel = new JPanel();label = new JLab

7、el("輸入");inputText = new JTextField(10);boldBx = new JCheckBox("粗體");italicBx = new JCheckBox("斜體");colorBtn = new JButton("顏色");inputText.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / 文本輸入txtArea.setText(inputText

8、.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.setFont(font););italicBx.addItemListe

9、ner(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.addActionListener(new ActionListener() O

10、verridepublic void actionPerformed(ActionEvent e) / 改變顏色colorStyle = JColorChooser.showDialog(null, "請(qǐng)選擇一種顏色", colorStyle); colorBtn.setForeground(colorStyle); txtArea.setForeground(colorStyle); font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);txtArea.setFont(font););

11、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) , BorderLayout.CENTER);return panel

12、;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 i = 0; i < fontSizes.length; i+

13、) fontSizesi = Integer.toString(i+10);fontSize = new JComboBox(fontSizes);windowStyle = new JComboBox(style);fontType.addItemListener(new ItemListener() Overridepublic void itemStateChanged(ItemEvent e) / 字體的類(lèi)型fontNameStyle = (String) e.getItem(); font = new Font(fontNameStyle, boldStyle + italicSty

14、le, 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, fontSizeStyle);txtArea.setFont(

15、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.WindowsLookAndFeel"else if (s.equa

16、ls("Unix顯示效果")className = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"else if (s.equals("默認(rèn)顯示效果")className = UIManager.getCrossPlatformLookAndFeelClassName();try UIManager.setLookAndFeel(className);SwingUtilities.updateComponentTreeUI(artFont); catch (Exception de)

17、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è)計(jì)在實(shí)驗(yàn)三第2題的基礎(chǔ)上,添加事件處理機(jī)制,并逐步完善程序功能。分別用CalendarF

18、rame類(lèi)的對(duì)象做監(jiān)視器和匿名內(nèi)部類(lèi)的對(duì)象做監(jiān)視器實(shí)現(xiàn)。要求實(shí)現(xiàn)如下功能:l 在文本框inputYear中輸入年份,驗(yàn)證年份的有效性;按回車(chē)鍵后,顯示輸入年份的正確日歷l 單擊previousMonth按鈕可以顯示當(dāng)前月的上一月的日歷;如果月份小于1,則顯示上一年的12月l 單擊nextMonth按鈕,可以顯示當(dāng)前月的下一月的日歷;如果月份大于1,則顯示下一年的1月CalendarBean.javapackage Sy;import java.util.Calendar;public class CalendarBean String day;int year = 2013, month =

19、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號(hào)開(kāi)始的日期數(shù)組public String getCalendar() String a = new String42;Calendar 日歷 = Calendar.getInstance();/注意:1月份是從0開(kāi)始,所以要減1日歷.set(ye

20、ar, month - 1, 1);int 星期幾 = 日歷.get(Calendar.DAY_OF_WEEK) - 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 (month = 2) if (year % 4 = 0) && (year % 100 != 0) | (yea

21、r % 400 = 0) day = 29; else day = 28;for (int i = 星期幾, n = 1; i < 星期幾 + day; i+) ai = String.valueOf(n);n+;return a;CalendarFrame.javapackage Sy;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class CalendarFrame extends Frame Label labelDay = new Label42;Label labelYear;But

22、ton titleName = new Button7;Button nextMonth, previousMonth;Label showMessage;TextField inputYear;CalendarBean calendar;String name = "日", "一", "二", "三", "四", "五", "六" ;int year = 2013, month = 1;String days;public CalendarFrame()

23、 calendar = new CalendarBean();calendar.setYear(year);calendar.setMonth(month);days = calendar.getCalendar();ScrollPane scrollPane = new ScrollPane();scrollPane.add(getCenterPanel();add(scrollPane, BorderLayout.CENTER);/ 窗口添加scrollPane在中心區(qū)域add(getNorthPanel(), BorderLayout.NORTH);/ 窗口添加pNorth 在北面區(qū)域a

24、dd(getSouthPanel(), BorderLayout.SOUTH);/ 窗口添加pSouth 在南區(qū)域。private Panel getNorthPanel() Panel panel = new Panel();labelYear = new Label("請(qǐng)輸入年份:");inputYear = new TextField(10);previousMonth = new Button("上月");nextMonth = new Button("下月");inputYear.addActionListener(new

25、ActionListener() public void actionPerformed(ActionEvent e) / 文本輸入try year = Integer.parseInt(inputYear.getText(); catch (NumberFormatException e1) JOptionPane.showMessageDialog(null, "您輸入的年份有誤,請(qǐng)重新輸入!");inputYear.setFocusable(true);calendar.setYear(year);calendar.setMonth(month);days = cal

26、endar.getCalendar();for (int i = 0; i < 42; i+) labelDayi.setText(daysi);showMessage.setText("日歷:" + calendar.getYear() + "年"+ calendar.getMonth() + "月"););previousMonth.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) /上月按鈕month-;if(

27、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("日歷:" + calendar.getYear() + "年"+ calendar.getMonth() + "月"););nextMonth.addActionListene

28、r(new ActionListener() public void actionPerformed(ActionEvent e) / 下月按鈕month+;if(month > 12)year+;month=1;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() + "月"););panel.add(labelYear);panel.add(inputYear);panel.add(previousMonth);panel.add(nextMonth);return panel;private Panel getCenterPanel() Panel panel = new Panel();panel.setLayout(new Gr

溫馨提示

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

評(píng)論

0/150

提交評(píng)論