試驗(yàn)九-圖形用戶界面與對話框_第1頁
試驗(yàn)九-圖形用戶界面與對話框_第2頁
試驗(yàn)九-圖形用戶界面與對話框_第3頁
試驗(yàn)九-圖形用戶界面與對話框_第4頁
試驗(yàn)九-圖形用戶界面與對話框_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、實(shí)驗(yàn)九 圖形用戶界面與對話框1.實(shí)驗(yàn)?zāi)康?、學(xué)會(huì)處理 ActionEvent事件2、學(xué)會(huì)使用布局類3、學(xué)習(xí)焦點(diǎn)、鼠標(biāo)和鍵盤事件4、學(xué)習(xí)使用輸入和消息對話框2.實(shí)驗(yàn)內(nèi)容1、根據(jù)附錄里的源代碼,按照注釋要求,完成代碼填空,使程序能夠運(yùn)行 得出結(jié)果。1)實(shí)驗(yàn)1算術(shù)測試2)實(shí)驗(yàn)2布局與日歷3)實(shí)驗(yàn)3華容道4)實(shí)驗(yàn)4字體對話框5)實(shí)驗(yàn)5計(jì)算平方根6)實(shí)驗(yàn)6簡易計(jì)算器2、設(shè)計(jì)編寫程序完成以下任務(wù)。1 )修改實(shí)驗(yàn)1的代碼,再增加“小學(xué)生”級(jí)別,并增加測試乘、除法的 功能。2)編寫一個(gè)應(yīng)用程序,用戶可以在一個(gè)文本框里輸入數(shù)字字符,按Enter 鍵后將數(shù)字放入一個(gè)文本區(qū)。當(dāng)輸入的數(shù)字大于1000時(shí),彈出一個(gè)有模

2、式的對話框,提示用戶數(shù)字已經(jīng)大于1000,是否繼續(xù)將該數(shù)字放入文本區(qū)。3)編寫應(yīng)用程序,有一個(gè)標(biāo)題為“移動(dòng)”的窗口,窗口布局為 null , 在窗口中有兩個(gè)按鈕,單擊一個(gè)按鈕讓另一個(gè)按鈕移動(dòng)。4)仿照操作系統(tǒng)中的簡易計(jì)算機(jī),自行設(shè)計(jì)一個(gè)能進(jìn)行加減乘除運(yùn)算的 計(jì)算器。需要考慮先進(jìn)性乘除運(yùn)算再進(jìn)行加減運(yùn)算。.實(shí)驗(yàn)步驟略.評分標(biāo)準(zhǔn)A內(nèi)容功能完善,編程風(fēng)格好,人機(jī)接口界面好;B內(nèi)容功能完善,編程風(fēng)格良好,人機(jī)接口界面良好;C完成必做內(nèi)容;D能完成必做內(nèi)容;E未按時(shí)完成必做內(nèi)容,或者抄襲(雷同者全部為E).參照書上實(shí)驗(yàn)按模版要求,將【代碼】替換為 Java程序代碼,編寫好完整 的程序文檔,最后運(yùn)行得到的

3、相關(guān)文件,把實(shí)驗(yàn)所得文件一起打包上交。(壓縮包的文件名為:學(xué)號(hào)后三位和名字開頭字母,如 109zhh. RAR|ZIP)附錄:實(shí)驗(yàn)1算術(shù)測試模板代碼Teacher.javaimport java.util.Random;import java.awt.event.*;import javax.swing.*;public class Teacher implements ActionListenerint numberOne,numberTwo;String operator=;boolean isRight;Random random;int maxInteger;JTextField te

4、xtOne,textTwo,textResult;JLabel operatorLabel,message;Teacher()random=new Random();public void setMaxInteger(int n) maxInteger=n;public void actionPerformed(ActionEvent e)String str=e.getActionCommand();if(str.equals(getProblem)numberOne=random.nextInt(maxInteger)+1;numberTwo=random.nextInt(maxInteg

5、er)+1; double d=Math.random();if(d=0.5)operator=+;else operator=-;textOne.setText(+numberOne);textTwo.setText(+numberTwo);operatorLabel.setText(operator); message.setText(情回答);textResult.setText(null);else if(str.equals(answer)String answer=textResult.getText();tryint result=Integer.parseInt(answer)

6、;if(operator.equals(+)if(result=numberOne+numberTwo) message.setText(你回答正確);else message.setText。你回答錯(cuò)誤);else if(operator.equals(-)if(result=numberOne-numberTwo) message.setText(你回答正確);else message.setText(你回答錯(cuò)誤);catch(NumberFormatException ex) message.setText(情輸入數(shù)字字符);public void setJTextField(JText

7、Field.t)textOne =t0;textTwo =t1;textResult =t2;public void setJLabel(JLabel.label)operatorLabel=label0;message=label1;ComputerFrame.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class ComputerFrame extends JFrameJMenuBar menubar;JMenu choiceGrade;JMenultem grade1,grade2;J

8、TextField textOne,textTwo,textResult;JButton getProblem,giveAnswer;JLabel operatorLabel,message;Teacher teacherZhang;ComputerFrame()teacherZhang=new Teacher();teacherZhang.setMaxInteger(20);setLayout(new FlowLayout();menubar=new JMenuBar();choiceGrade=new JMenu(選擇級(jí)另U );grade1=new JMenuItem(幼兒級(jí)另 U);g

9、rade2=new JMenuItem(兒童級(jí)另 U);grade1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) teacherZhang.setMaxInteger(10););grade2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) teacherZhang.setMaxInteger(50););choiceGrade.add(grade1);choice

10、Grade.add(grade2);menubar.add(choiceGrade);setJMenuBar(menubar);【代碼11/創(chuàng)建textOne,其可見字符長是 5textTwo=new JTextField(5);textResult=new JTextField(5);operatorLabel=new JLabel(+);operatorLabel.setFont(new Font(Arial,Font.BOLD,20);message=new JLabel(你還沒有回答呢);getProblem=new JButton(獲取題目); giveAnswer=new JBut

11、ton(確認(rèn)答案);add(getProblem);add(textOne);add(operatorLabel);add(textTwo);add(new Label(=);add(textResult);add(giveAnswer);add(message);textResult.requestFocus();textOne.setEditable(false);textTwo.setEditable(false);getProblem.setActionCommand(getProblem);textResult.setActionCommand(answer);giveAnswer.

12、setActionCommand(answer);teacherZhang.setJTextField(textOne,textTwo,textResult);teacherZhang.setJLabel(operatorLabel,message);/【代碼2】將teacherZhang注冊為getProblem的ActionEvent事件監(jiān)視器 /【代碼3】將teacherZhang注冊為giveAnswer的ActionEvent事件監(jiān)視器 /【代碼4】將teacherZhang注冊為textResult的ActionEvent事件監(jiān)視器 setVisible(true);validat

13、e();setDefaultCloseOperation(DISPOSE_ON_CLOSE);MainClass.javapublic class MainClass public static void main(String args) ComputerFrame frame;frame=new ComputerFrame();frame.setTitle(算術(shù)測試);frame.setBounds(100,100,650,180);實(shí)驗(yàn)2布局與日歷模板代碼CalendarBean.javaimport java.util.Calendar;public class CalendarBea

14、n String 口 day;int year=2008,month=0;public int getYear() return year;public void setYear(int year) this.year = year;public int getMonth() return month;public void setMonth(int month) this.month = month;public String getCalendar() String a=new String42;Calendar 日歷=Calendar.getInstance();日歷.set(year,

15、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)|(year%400=0)day=29;elseday=28;for(int i=星期幾,n=1;i 星期幾 +day;i+) ai=String.valueORn);n+;retu

16、rn a;CalendarFrame.javaimport java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.awt.*;import java.util.*;public class CalendarFrame extends JFrame implements ActionListener JLabel labelDay口=new JLabel42;JButton titleName口=new JButton7;String name=日,一,二,三,四,五,六;JButton nex

17、tMonth,previousMonth;CalendarBean calendar;JLabel showMessage=new JLabel(,JLabel.CENTER);int year=2011,month=2;public CalendarFrame()JPanel pCenter=new JPanel();/【代碼1】/將pCenter的布局設(shè)置為7行7列的GridLayout布局.for(int i=0;i7;i+)titleNamei=new JButton(namei);titleNamei.setBorder(new SoftBevelBorder(BevelBorder

18、.RAISED);/【代碼2】/pCenter中添加組件titleNameifor(int i=0;i42;i+)labelDayi=new JLabel(,JLabel.CENTER);labelDayi.setBorder(new SoftBevelBorder(BevelBorder.LOWERED);/【代碼3】/pCenter中添加組件labelDayicalendar=new CalendarBean();nextMonth=new JButton(下月);previousMonth=new JButton(上月);nextMonth.addActionListener(this)

19、;previousMonth.addActionListener(this);JPanel pNoth=new JPanel(),pSouth=new JPanel();pNoth.add(previousMonth);pNoth.add(nextMonth);pSouth.add(showMessage);/【代碼4 /將窗口 pCenter添加到中央?yún)^(qū)域/【代碼5】/將窗口 pNoth添加到北面區(qū)域/【代碼6】/將窗口 pSouth添加到南面區(qū)域setYearAndMonth(year,month);setDefaultCloseOperation(DISPOSE_ON_CLOSE);pu

20、blic void setYearAndMonth(int y,int m)calendar.setYear(y);calendar.setMonth(m);String day= calendar.getCalendar();for(int i=0;i12)month=1;calendar.setMonth(month);String day=calendar.getCalendar();for(int i=0;i42;i+)labelDayi.setText(dayi);else if(e.getSource()=previousMonth)month=month-1;month=12;)

21、calendar.setMonth(month);String day口=calendar.getCalendar();for(int i=0;i42;i+)labelDayi.setText(dayi);)showMessage.setText(日歷:+calendar.getYear()+年+calendar.month+月);)CalendarMainClass.javapublic class CalendarMainClass public static void main(String args) CalendarFrame frame=new CalendarFrame();fr

22、ame.setBounds(100,100,360,300);frame.setVisible(true);frame.setYearAndMonth(2013,5);)實(shí)驗(yàn)3華容道模板代碼MainClassHRR.javapublic class MainClassHRR public static void main(String args) Hua_Rong_Road HRR=new Hua_Rong_Road();)Person.javaimport java.awt.event.*;import java.awt.*;import javax.swing.*;public class

23、 Person extends JButton implements FocusListener int number;Color c=new Color(255,245,170);Font font=new Font(宋體,Font.BOLD,12);Person(int number,String s) super(s);setBackground(c);setFont(font);this.number=number;c=getBackground();addFocusListener(this);public void focusGained(FocusEvent e) setBack

24、ground(Color.red);public void focusLost(FocusEvent e) setBackground(c);Hua_Rong_Road.javaimport java.awt.event.*;import java.awt.*;import javax.swing.*;public class Hua_Rong_Road extends JFrame implements MouseListener, KeyListener, ActionListener Person person口=new Person10;JButton left,right,above

25、,below;JButton restart=new JButton(重新開始);public Hua_Rong_Road() init();setBounds(100,100,320,360);setVisible(true);setDefaultCloseOperation(DISPOSE_ON_CLOSE);validate();public void init() setLayout(null);add(restart);restart.setBounds(100,320,120,25);restart.addActionListener(this);String name=曹操,關(guān)羽

26、,張,劉,周,黃,兵,兵,兵,兵;for(int k=0;kh/2) go(man,below);)if(yh/2) go(man,above);) if(xw/2) go(man,right); )public void mouseReleased(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mouseClicked(MouseEvent e) public void go(Person man,JButton direction)

27、 boolean move=true;Rectangle manRect=man.getBounds();int x=man.getBounds().x;int y=man.getBounds().y;if(direction=below)y=y+50;else if(direction=above) y=y-50;else if(direction=left) x=x-50;else if(direction=right) x=x+50;manRect.setLocation(x,y);Rectangle directionRect=direction.getBounds();for(int

28、 k=0;k10;k+) Rectangle personRect=personk.getBounds();if(manRersects(personRect)&(man.number!=k) move=false; if(manRersects(directionRect) move=false; if(move=true) man.setLocation(x,y); )public void actionPerformed(ActionEvent e) dispose();new Hua_Rong_Road();)實(shí)驗(yàn)4字體對話框模板代碼FontFamilyNa

29、mes.javaimport java.awt.GraphicsEnvironment;public class FontFamilyNames String allFontName口;public String getFontName() GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();allFontName=ge.getAvailableFontFamilyNames();return allFontName;)FontDialog.javaimport java.awt.event.*;im

30、port java.awt.*;import javax.swing.*;public class FontDialog extends JDialog implements ItemListener,ActionListenerFontFamilyNames fontFamilyNames;int fontSize=38;String fontName;JComboBox fontSizeList,fontNameList;JLabel label;Font font;JButton yes,cancel;static int YES=1,NO=0;int state=-1;FontDial

31、og(Frame f)super(f);setTitle(字體);font=new Font(宋體,Font.PLAIN,12);fontFamilyNames=new FontFamilyNames();/代碼1 當(dāng)前對話框調(diào)用setModal(boolean b)設(shè)置為有模式y(tǒng)es=new JButton(Yes);cancel=new JButton(cancel);yes.addActionListener(this);cancel.addActionListener(this);label=new JLabel(hello,奧運(yùn),JLabel.CENTER);fontSizeList

32、=new JComboBox();fontNameList=new JComboBox();String name口=fontFamilyNames.getFontName();fontNameList.addItem(字體);for(int k=0;kname.length;k+) fontNameList.addItem(namek);fontSizeList.addItem(大小);for(int k=8;k=0)inputComplete=true;)catch(NumberFormatException exp)/【代碼2】彈出消息對話框inputComplete=false;dou

33、ble sqrtRoot=Math.sqrt(result);System.out.println(result+平方根:+sqrtRoot);實(shí)驗(yàn)6簡易計(jì)算器模板代碼UserFrm.javaimport java.awt.*;import java.awt.event.*;public class UserFrm extends Frame implements ActionListener private MenuBar jmb = new MenuBar();private MenuItem item = new MenuItem( 退 出);public static Font fon

34、t = new Font(宋體, 1,16);public UserFrm(String title) throws HeadlessException super(title);/【代碼1】/設(shè)置該Frame位置與大小,具體值為(100,100, 250, 200) setVisible(true);Menu menu = new Menu(文 件);menu.add(item);item.setFont(font);Panel panelNorth = new Panel();/【代碼2】/把panelNorth加入窗體的北區(qū)add(new Caculator(), BorderLayou

35、t.CENTER);item.addActionListener(this);jmb.add(menu);jmb.setFont(font);setMenuBar(jmb);/【代碼3】用匿名類的方式設(shè)計(jì)完成窗體關(guān)閉的監(jiān)聽和實(shí)現(xiàn)關(guān)閉的方法 validate();public void actionPerformed(ActionEvent e) Object o = e.getSource();if (o = item)System.exit(1);public static void main(String args) new UserFrm(用戶界面);class Caculator ex

36、tends Panel implements ActionListener, KeyListener private TextField tf = new TextField();private float x = 0;private float y = 0;private int code = 0;private boolean enable;private boolean first;private String str =public Caculator() setLayout(new BorderLayout();enable = true;first = true;add(tf, B

37、orderLayout.NORTH);Panel panel = new Panel();/【代碼4】/把panel設(shè)置為GridLayout布局Button btn = null;【代碼5】/創(chuàng)建btn并把標(biāo)題設(shè)為1;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼6】/創(chuàng)建btn并把標(biāo)題設(shè)為2;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼7】/創(chuàng)建btn并把標(biāo)題設(shè)為3;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)

38、聽;設(shè)置鍵盤輸入監(jiān)聽【代碼8】/創(chuàng)建btn并把標(biāo)題設(shè)為+;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼9】/創(chuàng)建btn并把標(biāo)題設(shè)為4;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼10/創(chuàng)建btn并把標(biāo)題設(shè)為5;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼11/創(chuàng)建btn并把標(biāo)題設(shè)為6;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入

39、監(jiān)聽【代碼12/創(chuàng)建btn并把標(biāo)題設(shè)為-;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼13/創(chuàng)建btn并把標(biāo)題設(shè)為7;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼14/創(chuàng)建btn并把標(biāo)題設(shè)為8;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽/【代碼15/創(chuàng)建btn并把標(biāo)題設(shè)為9;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼16

40、/創(chuàng)建btn并把標(biāo)題設(shè)為*;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼17/創(chuàng)建btn并把標(biāo)題設(shè)為0;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼18/創(chuàng)建btn并把標(biāo)題設(shè)為.;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽【代碼19/創(chuàng)建btn并把標(biāo)題設(shè)為/;把btn加入panel中;設(shè)置該按鈕的字體格式為UserFrm.font;設(shè)置輸入確認(rèn)監(jiān)聽;設(shè)置鍵盤輸入監(jiān)聽btn = new But

41、ton(=);panel.add(btn);btn.setFont(UserFrm.font); btn.addActionListener(this);btn.addKeyListener(this);/【代碼20把panel加入Caculator的中部區(qū)域中)public void actionPerformed(ActionEvent e) if (e.getActionCommand() = +) 【代碼21把文本框里的數(shù)字轉(zhuǎn)成浮點(diǎn)型,賦給 xcode = 0;this.tf.setText();)if (e.getActionCommand() = -) 【代碼21把文本框里的數(shù)字轉(zhuǎn)

42、成浮點(diǎn)型,賦給 xcode = 1;this.tf.setText();)if (e.getActionCommand() = *) 【代碼21把文本框里的數(shù)字轉(zhuǎn)成浮點(diǎn)型,賦給 xcode = 2;this.tf.setText();)if (e.getActionCommand() = /) x = Float.parseFloat(tf.getText();code = 3;this.tf.setText();)if (e.getActionCommand() != + & e.getActionCommand() !=-& e.getActionCommand() != * & e.getActionCommand() != /& e.getActionCommand() != =) if (enable) if (first) tf.setText(e.getActionCommand();first = false; else tf.setText(tf.getText() + e.getActionCommand();else tf.setText(e.getActionCommand();enable = true;if (e.getActionComma

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(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

提交評論