Java³ÌÐòÉè¼Æ - ÊÔÑé6£¨Ñ§Éú°æ£© ÏÂÔØ±¾ÎÄ

»ª±±µçÁ¦´óѧ

| || |

ʵ Ñé ±¨ ¸æ

ʵÑéÃû³Æ Application¼°Applet³ÌÐòÉè¼Æ ¿Î³ÌÃû³Æ Java³ÌÐòÉè¼Æ רҵ°à¼¶£º ѧÉúÐÕÃû£º ѧ ºÅ£º

³É ¼¨£º

Ö¸µ¼½Ìʦ£ºÕÅѧ±ó ʵÑéÈÕÆÚ£º

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

Ò»¡¢ÊµÑéÄ¿µÄºÍÒªÇó 1ÕÆÎÕJava Swing ×é¼þµÄʹÓ÷½·¨£» 2 Àí½âίÍдúÀíʼþ´¦ÀíÄ£ÐÍÕÆÎÕ£» 3 ÕÆÎÕ´°¿Ú²Ëµ¥ºÍ¿ì½Ý²Ëµ¥Éè¼Æ·½Ê½£» 4 ÕÆÎÕÔÚ×é¼þÉÏ»æÍ¼µÄ·½·¨£» 5 ÕÆÎÕAppletÓ¦ÓóÌÐòµÄÉè¼Æ·½·¨ ¶þ¡¢ÊµÑé»·¾³ Windows2000/Windows XP£¬JDK 1.2~1.6 Èý¡¢ÊµÑéÄÚÈݺͲ½Öè ÒªÇó°´ÕÕʵÑéÄÚÈÝ£¬Ð´³öÏêϸʵÑé¹ý³ÌºÍ²½Ö裬±ØÒªÊ±½ØÍ¼¡£ ʵÑé1 ËãÊõ²âÊÔ 1.ʵÑéÒªÇó ±àдһ¸öËãÊõ²âÊÔСÈí¼þ£¬ÓÃÀ´ÑµÁ·Ð¡Ñ§ÉúµÄËãÊõÄÜÁ¦¡£³ÌÐòÓÐÈý¸öÀà×é³É£¬ÆäÖÐTeacher¶ÔÏó³äµ±¼àÊÓÆ÷£¬¸ºÔð¸ø³öËãÊõÌâÄ¿£¬²¢ÅжϻشðÕߵĴð°¸ÊÇ·ñÕýÈ·£»ComputerFrame¶ÔÏó¸ºÔðΪËãÊõÌâÄ¿ÌṩÊÓͼ£¬±ÈÈçÓû§¿ÉÒÔͨ¹ýComputerFrame¶ÔÏóÌṩµÄGUI½çÃæ¿´µ½ÌâÄ¿£¬²¢Í¨¹ýGUI½çÃæ¸ø³öÌâÄ¿µÄ´ð°¸£»MailClassÊÇÈí¼þµÄÖ÷Àà¡£ 2.³ÌÐòÄ£°å MainClass.java public class MainClass { public static void main(String args[]) { ComputerFrame frame; frame=new ComputerFrame(); frame.setTitle(\ËãÊõ²âÊÔ\ frame.setBounds(100,100,650,180); } } ComputerFrame.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComputerFrame extends JFrame { JMenuBar menubar; JMenu choiceGrade; //Ñ¡Ôñ¼¶±ðµÄ²Ëµ¥ µÚ 2 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

JMenuItem grade1,grade2; JTextField textOne,textTwo,textResult; JButton getProblem,giveAnwser; JLabel operatorLabel,message; Teacher teacherZhang; ComputerFrame() { teacherZhang=new Teacher(); teacherZhang.setMaxInteger(20); setLayout(new FlowLayout()); menubar = new JMenuBar(); choiceGrade = new JMenu(\Ñ¡Ôñ¼¶±ð\ grade1 = new JMenuItem(\Ó×¶ù¼¶±ð\ grade2 = new JMenuItem(\¶ùͯ¼¶±ð\ 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); choiceGrade.add(grade2); menubar.add(choiceGrade); setJMenuBar(menubar); ¡¾´úÂë1¡¿ //´´½¨textOne,Æä¿É¼û×Ö·û³¤ÊÇ5 textTwo=new JTextField(5); textResult=new JTextField(5); operatorLabel=new JLabel(\ operatorLabel.setFont(new Font(\ message=new JLabel(\Ä㻹ûÓлشðÄØ\ getProblem=new JButton(\»ñÈ¡ÌâÄ¿\ giveAnwser=new JButton(\È·ÈÏ´ð°¸\ add(getProblem); add(textOne); add(operatorLabel); add(textTwo); add(new JLabel(\ add(textResult); add(giveAnwser); add(message); textResult.requestFocus(); textOne.setEditable(false); µÚ 3 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

textTwo.setEditable(false); getProblem.setActionCommand(\ textResult.setActionCommand(\ giveAnwser.setActionCommand(\ teacherZhang.setJTextField(textOne,textTwo,textResult); teacherZhang.setJLabel(operatorLabel,message); ¡¾´úÂë2¡¿//½«teacherZhang×¢²áΪgetProblemµÄActionEventʼþ¼àÊÓÆ÷ ¡¾´úÂë3¡¿//½«teacherZhang×¢²áΪgiveAnwserµÄActionEventʼþ¼àÊÓÆ÷ ¡¾´úÂë4¡¿//½«teacherZhang×¢²áΪtextResultµÄActionEventʼþ¼àÊÓÆ÷ setVisible(true); validate(); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } } Techaer.java import java.util.Random; import java.awt.event.*; import javax.swing.*; public class Teacher implements ActionListener { int numberOne,numberTwo; String operator=\ boolean isRight; Random random; //ÓÃÓÚ¸ø³öËæ»úÊý int maxInteger; //ÌâÄ¿ÖÐ×î´óµÄÕûÊý JTextField textOne,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(\ numberOne = random.nextInt(maxInteger)+1;//1ÖÁmaxIntegerÖ®¼äµÄËæ»úÊý; numberTwo=random.nextInt(maxInteger)+1; double d=Math.random(); // »ñÈ¡(0,1)Ö®¼äµÄËæ»úÊý if(d>=0.5) operator=\ else operator=\ textOne.setText(\ textTwo.setText(\ operatorLabel.setText(operator); µÚ 4 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

message.setText(\Çë»Ø´ð\ textResult.setText(null); } else if(str.equals(\ String answer=textResult.getText(); try{ int result=Integer.parseInt(answer); if(operator.equals(\ if(result==numberOne+numberTwo) message.setText(\Äã»Ø´ðÕýÈ·\ else message.setText(\Äã»Ø´ð´íÎó\ } else if(operator.equals(\ if(result==numberOne-numberTwo) message.setText(\Äã»Ø´ðÕýÈ·\ else message.setText(\Äã»Ø´ð´íÎó\ } } catch(NumberFormatException ex) { message.setText(\ÇëÊäÈëÊý×Ö×Ö·û\ } } } public void setJTextField(JTextField ... t) { textOne=t[0]; textTwo=t[1]; textResult=t[2]; } public void setJLabel(JLabel ...label) { operatorLabel=label[0]; message=label[1]; } } 3.ʵÑéÖ¸µ¼ Jbutton¶ÔÏó¿É´¥·¢ActionEventʼþ¡£ÎªÁËÄܼàÊÓµ½´ËÀàʼþ£¬Ê¼þÔ´±ØÐëʹÓÃaddActionListener·½·¨»ñµÃ¼àÊÓÆ÷£¬´´½¨¼àÊÓÆ÷µÄÀà±ØÐëʵÏÖ½Ó¿ÚActionListener¡£ 4.ʵÑéÀ©Õ¹ £¨1£©Ä£·Â±¾ÊµÑé´úÂ룬ÔÙÔö¼Ó¡°Ð¡Ñ§Éú¡±¼¶±ð¡£ £¨2£©¸ø³öÉÏÊö³ÌÐòÔö¼Ó²âÊԳ˷¨µÄ¹¦ÄÜ¡£ µÚ 5 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

ʵÑé2 ²¼¾ÖÓëÈÕÀú 1. ʵÑéÒªÇó ±àдһ¸öÓ¦ÓóÌÐò£¬ÓÐÒ»¸ö´°¿Ú£¬¸Ã´°¿ÚµÄ²¼¾ÖΪBorderLayout²¼¾Ö¡£´°¿ÚµÄÖÐÐÄÌí¼ÓÒ»¸öJPanelÈÝÆ÷pCenter, pCenterµÄ²¼¾ÖÊÇ7ÐÐ7ÁеÄGriderLayout²¼¾Ö£¬pCenterÖзÅÖÃ49¸ö±êÇ©£¬ÓÃÀ´ÏÔʾÈÕÀú¡£´°¿ÚµÄ±±ÃæÌí¼ÓÒ»¸öJPanelÈÝÆ÷pNorth£¬Æä²¼¾ÖÊÇFlowLayout²¼¾Ö£¬pNorth·ÅÖÃÁ½¸ö°´Å¥£ºnextMonthºÍpreviousMonth¡£µ¥»÷nextMonth°´Å¥£¬¿ÉÒÔÏÔʾµ±Ç°ÔµÄÏÂÒ»ÔµÄÈÕÀú£»µ¥»÷previousMonth°´Å¥£¬¿ÉÒÔÏÔʾµ±Ç°ÔµÄÉÏÒ»ÔµÄÈÕÀú£»´°¿ÚµÄÄÏÃæÌí¼ÓÒ»¸öJpanelÈÝÆ÷pSouth£¬Æä²¼¾ÖÊÇFlowLayout²¼¾Ö£¬pSouthÖзÅÖÃÒ»¸ö±êÇ©ÓÃÀ´ÏÔʾһЩÐÅÏ¢¡£ 2.³ÌÐòÄ£°å CalendarMainClass.java public class CalendarMainClass { public static void main(String args[]) { CalendarFrame frame=new CalendarFrame(); frame.setBounds(100,100,360,300); frame.setVisible(true); frame.setYearAndMonth(2015,5); } } CalendarBean.java import java.util.Calendar; public class CalendarBean { String day[]; int year=2005,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; } public String[] getCalendar() { String a[]=new String[42]; Calendar ÈÕÀú=Calendar.getInstance(); ÈÕÀú.set(year,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; µÚ 6 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

if(month==4||month==6||month==9||month==11) day=30; if(month==2) { if(((year%4==0)&&(year0!=0))||(year@0==0)) day=29; else day=28; } for(int i=ÐÇÆÚ¼¸,n=1;i<ÐÇÆÚ¼¸+day;i++) { a[i]=String.valueOf(n) ; n++; } return a; } } CalendarFrame.java import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class CalendarFrame extends JFrame implements ActionListener { JLabel labelDay[]=new JLabel[42]; JButton titleName[]=new JButton[7]; String name[]={\ÈÕ\Ò»\¶þ\Èý\ËÄ\Îå\Áù\ JButton nextMonth,previousMonth; CalendarBean calendar; JLabel showMessage=new JLabel(\ int year=2011,month=2; public CalendarFrame() { JPanel pCenter=new JPanel(); ¡¾´úÂë1¡¿ //½«pCenterµÄ²¼¾ÖÉèÖÃΪ7ÐÐ7ÁеÄGridLayout ²¼¾Ö¡£ for(int i=0;i<7;i++) { titleName[i]=new JButton(name[i]); titleName[i].setBorder(new SoftBevelBorder(BevelBorder.RAISED)); pCenter.add(titleName[i]); } for(int i=0;i<42;i++) { labelDay[i]=new JLabel(\ labelDay[i].setBorder(new SoftBevelBorder(BevelBorder.LOWERED)); ¡¾´úÂë2¡¿//pCenterÌí¼Ó×é¼þlabelDay[i]¡£ } calendar=new CalendarBean(); nextMonth=new JButton(\ÏÂÔÂ\ previousMonth=new JButton(\ÉÏÔÂ\µÚ 7 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

nextMonth.addActionListener(this); previousMonth.addActionListener(this); JPanel pNorth=new JPanel(), pSouth=new JPanel(); pNorth.add(previousMonth); pNorth.add(nextMonth); pSouth.add(showMessage); add(pCenter,BorderLayout.CENTER); ¡¾´úÂë3¡¿ // ´°¿ÚÌí¼ÓpNorth ÔÚ±±ÃæÇøÓò ¡¾´úÂë4¡¿ // ´°¿ÚÌí¼ÓpSouth ÔÚÄÏÇøÓò¡£ setYearAndMonth(year,month); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } public void setYearAndMonth(int y,int m) { calendar.setYear(y); calendar.setMonth(m); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) labelDay[i].setText(day[i]); showMessage.setText(\ÈÕÀú£º\Äê\ÔÂ\ } public void actionPerformed(ActionEvent e) { if(e.getSource()==nextMonth) { month=month+1; if(month>12) month=1; calendar.setMonth(month); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) { labelDay[i].setText(day[i]); } } else if(e.getSource()==previousMonth) { month=month-1; if(month<1) month=12; calendar.setMonth(month); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++) labelDay[i].setText(day[i]); } showMessage.setText(\ÈÕÀú£º\Äê\ÔÂ\ } } µÚ 8 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

3.ʵÑéÀ©Õ¹ ÔÚCalendarFrameÀàÖÐÔö¼ÓÒ»¸öJTextFieldÎı¾¿ò£¬Óû§¿ÉÒÔͨ¹ýÔÚÎı¾¿òÖÐÊäÈëÄê·ÝÀ´ÐÞ¸Äcalendar¶ÔÏóµÄint³ÉÔ±year ʵÑé3 Ó¢Óïµ¥´ÊƴдѵÁ· 1.ʵÑéÒªÇó ±àдһ¸öÓ¦ÓóÌÐò£¬ÒªÇóÈçÏ£º ? ´°¿ÚÓÐÒ»¸öTextField¶ÔÏóºÍÒ»¸ö°´Å¥¶ÔÏ󣬽«ÕâÁ½¸ö¶ÔÏóÌí¼Óµ½Ò»¸öÃæ°åÖУ¬È»ºó½«¸ÃÃæ°åÌí¼Óµ½´°¿ÚµÄÉÏÃæ¡£ ? Óû§ÔÚTextField¶ÔÏóÖÐÊäÈëÒ»¸öÓ¢Îĵ¥´Ê£¬È»ºó°´Enter»òµ¥»÷¡°È·¶¨¡±°´Å¥£¬³ÌÐò½«´´½¨Èô¸É¸ö²»¿É±à¼­µÄÎı¾¿ò£¬Ã¿¸öÎı¾¿òËæ»úÏÔʾӢÎĵ¥´ÊÖеÄÒ»¸ö×Öĸ¡£ÒªÇó½«ÕâЩÎı¾¿ò°´Ò»ÐÐÌí¼Óµ½Ò»¸öÃæ°åÖУ¬È»ºó½«¸ÃÃæ°åÌí¼Óµ½´°¿ÚµÄÖÐÐÄ¡£ ? Óû§ÓÃÊó±êµ¥»÷Ò»¸öÎı¾¿òºó£¬Í¨¹ý°´Ï¼üÅÌÉϵġ°¡û¡±ºÍ¡°¡ú¡±¼ü½»»»ÏàÁÚÎı¾¿òÖеÄ×Öĸ£¬Ê¹µÃÕâЩÎı¾¿òÖеÄ×ÖĸµÄÅÅÁÐ˳ÐòºÍÓ¢Îĵ¥´ÊÖÐ×ÖĸµÄ˳ÐòÏàͬ¡£ 2.³ÌÐòÄ£°å WordMainClass.java public class WordMainClass { public static void main(String args[]) { new SpellingWordFrame(); } } ------------------------------------------------------------------------------------------------- RondomString.java public class RondomString { //¸ºÔðËæ»úÅÅÁе¥´ÊÖеÄ×Öĸ String str=\ public String getRondomString(String s) { StringBuffer strBuffer=new StringBuffer(s); int m=strBuffer.length(); for(int k=0;k

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

public class LetterLabel extends JTextField implements FocusListener { LetterLabel() { setEditable(false); addFocusListener(this);//¡¾´úÂë1¡¿ //½«µ±Ç°¶ÔÏó×¢²áΪ×ÔÉíµÄ½¹µãÊÓÆ÷ setBackground(Color.white); setFont(new Font(\ } public static LetterLabel[] getLetterLabel(int n) { LetterLabel a[]=new LetterLabel[n]; for(int k=0;k

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

northP.add(button); centerP=new JPanel(); wordBox=Box.createHorizontalBox(); centerP.add(wordBox); add(northP,BorderLayout.NORTH); add(centerP,BorderLayout.CENTER); add(messaageLabel,BorderLayout.SOUTH); setBounds(100,100,350,180); setVisible(true); validate(); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent e) { word=inputWord.getText(); int n=word.length(); RondomString rondom=new RondomString(); String randomWord=rondom.getRondomString(word); wordBox.removeAll(); messaageLabel.setText(hintMessage); if(n>0) { label=LetterLabel.getLetterLabel(n); for(int k=0;k

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

label[index-1].requestFocus(); } } else if(¡¾´úÂë3¡¿) { //Åжϰ´ÏµÄÊÇ·ñÊÇ¡ú¼ü for(int k=0;k

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

ʵÑé4 ×ÖÌå¶Ô»°¿ò 1.ʵÑéÒªÇó ±àдһ¸öFontFamilyÀ࣬¸ÃÀà¶ÔÏó¿ÉÒÔ»ñÈ¡µ±Ç°»úÆ÷¿ÉÓõÄÈ«²¿×ÖÌåÃû³Æ¡£ ±àдһ¸öJDialogµÄ×ÓÀàFontDialog£¬¸ÃÀàΪFontFamily¶ÔÏóά»¤µÄÊý¾ÝÌṩÊÓͼ£¬ÒªÇóFontDialog¶ÔÏóʹÓÃÏÂÀ­ÁбíÏÔʾFontFamily¶ÔÏóά»¤µÄÈ«²¿×ÖÌåµÄÃû³Æ£¬µ±Ñ¡ÔñÏÂÀ­ÁбíÖÐij¸ö×ÖÌåÃû³Æºó£¬FontDialog¶ÔÏóÓñêÇ©ÏÔʾ¸Ã×ÖÌåµÄЧ¹û¡£ÒªÇó¶Ô»°¿òÌṩ·µ»ØÏÂÀ­ÁбíÖÐËùÑ¡ÔñµÄ×ÖÌåÃû³ÆµÄ·½·¨¡£ ±àдһ¸ö´°¿Ú£¬¸Ã´°¿ÚÖÐÓС°ÉèÖÃ×ÖÌ塱°´Å¥ºÍÒ»Îı¾Çø¶ÔÏ󣬵±µ¥»÷¸Ã°´Å¥Ê±£¬µ¯³öÒ»¸öFontDialog´´½¨µÄ¶Ô»°¿ò£¬È»ºó¸ù¾ÝÓû§ÔÚ¶Ô»°¿òÏÂÀ­ÁбíÖÐÑ¡ÔñµÄ×ÖÌåÀ´ÏÔʾÎı¾ÇøÖеÄÎı¾¡£ 2.³ÌÐòÄ£°å FontDialogMainClass.java public class FontDialogMainClass { public static void main(String args[]) { FrameHaveDialog win=new FrameHaveDialog(); } } -------------------------------------------------------------------------- FontFamilyNames.java import java.awt.GraphicsEnvironment; public class FontFamilyNames { String allFontNames[]; public String [] getFontName() { GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); allFontNames=ge.getAvailableFontFamilyNames(); return allFontNames; } } ----------------------------------------------------------------------------------- FontDialog.java import java.awt.event.*; import java.awt.*; import javax.swing.*; public class FontDialog extends JDialog implements ItemListener,ActionListener { FontFamilyNames fontFamilyNames; int fontSize=38; String fontName; JComboBox fontNameList,fontSizeList; JLabel label; Font font; JButton yes,cancel; static int YES=1,NO=0; int state=-1; FontDialog(JFrame f) { µÚ 13 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

super(f); setTitle(\×ÖÌå¶Ô»°¿ò\ font=new Font(\ËÎÌå\ fontFamilyNames=new FontFamilyNames(); ¡¾´úÂë1¡¿ //µ±Ç°¶Ô»°¿òµ÷ÓÃsetModal(boolean b)ÉèÖÃΪÓÐģʽ yes=new JButton(\ cancel=new JButton(\ yes.addActionListener(this); cancel.addActionListener(this); label=new JLabel(\°ÂÔË\ fontNameList=new JComboBox(); fontSizeList=new JComboBox(); String name[]=fontFamilyNames.getFontName(); fontNameList.addItem(\×ÖÌå\ for(int k=0;k

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

label.repaint(); validate(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==yes) { state=YES; ¡¾´úÂë2¡¿ //¶Ô»°¿òÉèÖÃΪ²»¿É¼û } else if(e.getSource()==cancel) { state=NO; ¡¾´úÂë3¡¿ //¶Ô»°¿òÉèÖÃΪ²»¿É¼û } } public int getState() { return state; } public Font getFont() { return font; } } ------------------------------------------------------------------ FrameHaveDialog.java import java.awt.event.*; import java.awt.*; import javax.swing.*; public class FrameHaveDialog extends JFrame implements ActionListener { JTextArea text; JButton buttonFont; FrameHaveDialog() { buttonFont=new JButton(\ÉèÖÃ×ÖÌå\ text=new JTextArea(\ʵÓý̳̣¨µÚËİ棩\ buttonFont.addActionListener(this); add(buttonFont,BorderLayout.NORTH); add(text); setBounds(60,60,300,300); setVisible(true); validate(); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if(e.getSource()==buttonFont) { FontDialog dialog=new FontDialog(this); dialog.setVisible(true); if(dialog.getState()==FontDialog.YES) { text.setFont(dialog.getFont()); µÚ 15 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

text.repaint(); } if(dialog.getState()==FontDialog.NO) { text.repaint(); } } } } ʵÑé5 »ªÈݵÀÓÎÏ· 1. ʵÑéÒªÇó Ê×ÏÈ£¬±àдһ¸ö°´Å¥µÄ×ÓÀ࣬¸Ã×ÓÀà´´½¨µÄ¶ÔÏó´ú±í»ªÈݵÀÖеÄÈÎÎñ¡£Í¨¹ý½¹µãʼþ¿ØÖÆÈËÎïµÄÑÕÉ«£¬µ±ÈËÎï»ñµÃ½¹µãʱÑÕɫΪÀ¶É«£¬µ±Ê§È¥½¹µãʱÑÕɫΪ»ÒÉ«¡£ÎÒÃÇͨ¹ý¼üÅÌʼþºÍÊó±êʼþÀ´ÊµÏֲܲ٣¬¹ØÓðµÈÈËÎïµÄÒÆ¶¯¡£µ±ÈËÎïÉÏ·¢ÉúÊó±êʼþ»ò¼üÅÌʼþʱ£¬Èç¹ûÊó±êÖ¸ÕëµÄλÖÃÊÇÔÚÈËÎïµÄÏ·½£¨Ò²¾ÍÊÇ×é¼þµÄϰ벿·Ö£©»ò°´¼üÅÌÉϵġ°¡ý¡±¼ü£¬ÈËÎïÏòÏÂÒÆ¶¯¡£Ïò×ó£¬ÏòÓÒºÍÏòÉÏÒÆ¶¯Ô­ÀíÀàËÆ¡£ 2. ³ÌÐòÄ£°å MoveExample.java import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; public class MoveExample { public static void main(String args[]) { new Hua_Rong_Road(); } } class Person extends JButton implements FocusListener { int number; Color c; Person(int number,String s) { super(s); this.number=number; c=getBackground(); setFont(new Font(\ËÎÌå\ ¡¾´úÂë1¡¿ // µ±Ç°°´Å¥×¢²áΪ±¾ÉíµÄ¼àÊÓÆ÷ } public void focusGained(FocusEvent e) { setBackground(Color.cyan); } public void focusLost(FocusEvent e) { setBackground(c); } } µÚ 16 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

class Hua_Rong_Road extends JFrame implements KeyListener,MouseListener,ActionListener { Person person[]=new Person[10]; JButton left,right,above,below; JButton restart=new JButton(\ÖØÐ¿ªÊ¼\ Container con; public Hua_Rong_Road() { init(); setBounds(100,100,320,360); setVisible(true); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void init() { con=getContentPane(); con.setLayout(null); con.add(restart); restart.setBounds(100,5,120,25); restart.addActionListener(this); String name[]={\²Ü²Ù\¹ØÓð\ÕÅ\Áõ\Âí\Ðí\±ø\±ø\±ø\±ø\ for(int i=0;i

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

right.setBounds(254,49,5,260); above.setBounds(49,49,210,5); below.setBounds(49,304,210,5); con.validate(); } public void keyPressed(KeyEvent e) { Person man=¡¾´úÂë5¡¿ // ·µ»ØÊ¼þÔ´ if(¡¾´úÂë6¡¿) { // ÅжÏÊÇ·ñ°´ÁË¡°¡ý¡±¼ü goDown(man); } if(¡¾´úÂë7¡¿){ // ÅжÏÊÇ·ñ°´ÁË¡°¡ü¡±¼ü goUp(man); } if(¡¾´úÂë8¡¿) { // ÅжÏÊÇ·ñ°´ÁË¡°¡û¡±¼ü goLeft(man); } if(¡¾´úÂë9¡¿) { // ÅжÏÊÇ·ñ°´ÁË¡°¡ú¡±¼ü goRight(man); } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} public void mousePressed(MouseEvent e) { Person man=¡¾´úÂë10¡¿ // ·µ»ØÊ¼þÔ´ int x=1,y=1; x=e.getX(); y=e.getY(); int w=man.getBounds().width; int h=man.getBounds().height; if(y>h/2) goDown(man); if(yw/2) goRight(man); } public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void goDown(Person man) { boolean move=true; Rectangle manRect=man.getBounds(); µÚ 18 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

int x=man.getBounds().x; int y=man.getBounds().y; y=y+50; manRect.setLocation(x,y); Rectangle belowRect=below.getBounds(); for(int i=0;i<10;i++) { Rectangle personRect=person[i].getBounds(); if((manRect.intersects(personRect))&&(man.number!=i)) move=false; } if(manRect.intersects(belowRect)) move=false; if(move==true) man.setLocation(x,y); } public void goUp(Person man) { boolean move=true; Rectangle manRect=man.getBounds(); int x=man.getBounds().x; int y=man.getBounds().y; y=y50; manRect.setLocation(x,y); Rectangle aboveRect=above.getBounds(); for(int i=0;i<10;i++) { Rectangle personRect=person[i].getBounds(); if((manRect.intersects(personRect))&&(man.number!=i)) move=false; } if(manRect.intersects(aboveRect)) move=false; if(move==true) man.setLocation(x,y); } public void goLeft(Person man) { boolean move=true; Rectangle manRect=man.getBounds(); int x=man.getBounds().x; int y=man.getBounds().y; x=x-50; manRect.setLocation(x,y); Rectangle leftRect=left.getBounds(); for(int i=0;i<10;i++) { Rectangle personRect=person[i].getBounds(); if((manRect.intersects(personRect))&&(man.number!=i)) move=false; µÚ 19 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

} if(manRect.intersects(leftRect)) move=false; if(move==true) man.setLocation(x,y); } public void goRight(Person man) { boolean move=true; Rectangle manRect=man.getBounds(); int x=man.getBounds().x; int y=man.getBounds().y; x=x+50; manRect.setLocation(x,y); Rectangle rightRect=right.getBounds(); for(int i=0;i<10;i++) { Rectangle personRect=person[i].getBounds(); if((manRect.intersects(personRect))&&(man.number!=i)) move=false; } if(manRect.intersects(rightRect)) move=false; if(move==true) man.setLocation(x,y); } public void actionPerformed(ActionEvent e) { con.removeAll(); init(); validate(); repaint(); } } ʵÑé6 ¶þÊ®ËĵãÅÆÏ·ÎÊÌâ 1. ʵÑéÒªÇó Ò»¸±ÆË¿ËÅÆ£¬È¥µôÍõÅÆ£¬J¡¢Q¡¢K¡¢A·Ö±ðËã×÷11£¬12£¬13ºÍ1µã¡£Ã¿´Î´Ó52ÕÅÖÐËæ»ú³é³ö4ÕÅÅÆ¡£ÒªÇó½«Õâ4ÕÅÅÆµÄÅÆµãͨ¹ý¼Ó£¬¼õ£¬³Ë£¬³ýÔËËãµÄ×éºÏ£¬´Õ³É24µã¡£¹æÔòÊÇ£ºËù³é³öµÄ4ÕÅÅÆ£¬Ã¿ÕÅÅÆ¶¼±ØÐëʹÓã¬ÇÒÿÕÅÅÆÖ»ÄÜʹÓÃÒ»´Î¡£ ÒªÉè¼Æ³öͼÐνçÃæ¡£Ö½ÅƵÄͼ°¸×ÔÐÐÌí¼Ó¡£ 2. ³ÌÐò½á¹û µÚ 20 Ò³ ¹² 21 Ò³

»ª ±± µç Á¦ ´ó ѧ ʵ Ñé ±¨ ¸æ

µÚ 21 Ò³ ¹² 21 Ò³