¡¶java³ÌÐòÉè¼Æ¡·ÊµÑéÖ¸µ¼Ê飨ÍêÕû£©

XXѧԺjavaʵÑ鱨¸æ

} }

Èý¡¢ÊµÑéÒªÇó

1¡¢¸ù¾ÝÌâÄ¿ÒªÇóÍê³É³ÌÐòÖÐûÓÐÍê³ÉµÄÄ£¿é¡£ 2¡¢Ð´ºÃÉÏ»ú±¨¸æ¡£

ʵÑéÁù Òì³£´¦Àí

Ò»¡¢ÊµÑéÄ¿µÄ

1£®ÊìϤÒì³£µÄÅ׳öÓ벶»ñµÄº¬Ò壻 2£®ÕÆÎÕÒì³£²¶»ñÓë´¦ÀíµÄ·½·¨£» 3£®ÄÜ×Ô¶¨ÒåÒì³£¡£ ¶þ¡¢ÊµÑéÄÚÈÝ

ÏÖÔÚ¶àÊýѧУµÄ³É¼¨¹ÜÀí¶¼ÓɼÆËã»ú½øÐйÜÀí£¬Õâ¾ÍÐèÒªÓÐÏàÓ¦µÄÓ¦ÓóÌÐò¡£±àд³É¼¨¹ÜÀíÓ¦ÓóÌÐò£¬ÆäÖÐÓгɼ¨Â¼ÈëÄ£¿é£¬³É¼¨Â¼Èë¹ý³ÌÖУ¬ÄÑÃâ³öÏÖ¼ÈëÊý¾ÝµÄÀàÐͳöÏÖ´íÎ󣬻ò¼ÈëµÄ³É¼¨²»ÔÚºÏÀíµÄ·¶Î§¡£Ôڳɼ¨Â¼Èë¹ý³ÌÖУ¬Èô³öÏÖÉÏÊö´íÎ󣬳ÌÐòÓ¦¸ÃÈçºÎ´¦Àí¡£ ³ÌÐò¿ò¼ÜÈçÏ£¬½«´úÂë²¹³äÍêÕû£º

import javax.swing.JOptionPane;

class ¸º·ÖÒì³£ extends Exception //µ±Â¼ÈëÁ˸º·Öʱ¡£´Ë´¦Ó¦ÓÃÁË×Ô¶¨ÒåÒì³£ { }

class ¸ß·ÖÒì³£ extends Exception //µ±Â¼Èë·Ö³¬¹ý100ʱ¡£Ó¦ÓÃÁË×Ô¶¨ÒåÒì³£ { }

public class ExceptionDemo {

¸º·ÖÒì³£(int i){

System.out.println(\·ÖÊýΪ¸ºÊý\); }

¸ß·ÖÒì³£(int i ){

System.out.println(\·ÖÊý¹ý¸ß\); }

static final int number=2; int score[]=new int[number];

public void ¼ì²é·ÖÊý(int ·ÖÊý) throws ¸º·ÖÒì³£,¸ß·ÖÒì³£ //ÏÂÃæ·½·¨ÖÐÅжÏÈç¹û¼ÈëµÄ³É¼¨²»ºÏ{ if(·ÖÊý >100) throw new ¸ß·ÖÒì³£(·ÖÊý); //ͨ¹ýthrow£¬È˹¤Å׳öÒì³£ if(·ÖÊý <0) throw new ¸º·ÖÒì³£(·ÖÊý); }

public void ¼Èë³É¼¨()

13

Àí£¬ÔòÅ׳öÒì³££¬µ«±¾·½·¨²¢²»´¦ÀíÒì³££¬½öÉùÃ÷ÁËÒì³£

XXѧԺjavaʵÑ鱨¸æ

{ int i;

for(i=0;i

score[i]=Integer.parseInt(JOptionPane.showInputDialog(\ÇëÊäÈëµÚ\+(i+1)

+\¸öͬѧµÄ³É¼¨\));

}catch(NumberFormatException e) //ÔÚtry{¡­¡­.}catch¼äµÄÓï¾äÖÐÈô²úÉúÒì³££¬Ôò²¶»ñ

Òì³££¬Ö±½Ó½øÐÐÒì³£´¦Àí { } }

14

try{ ¼ì²é·ÖÊý(score[i]); //ÔÚ¼ì²é·ÖÊýÄ£¿éÖУ¬¿ÉÄܲúÉú¸ß·ÖÒì³£¶ÔÏó£¬Ò²¿ÉÄܲúÉúµÍ·ÖÒì}catch(¸ß·ÖÒì³£ e){

System.out.println(e);

³£¶ÔÏ󣬸ù¾Ý²»Í¬Çé¿ö£¬½øÐв»Í¬´¦Àí

}catch(¸º·ÖÒì³£ e){

System.out.println(e);

}

}

}

public void Êä³ö³É¼¨() {

System.out.println(score[0]); System.out.println(score[1]);

}

public static void main(String arg[]) {

ExceptionDemo demo = new ExceptionDemo(); demo.¼Èë³É¼¨();

demo.Êä³ö³É¼¨(); }

XXѧԺjavaʵÑ鱨¸æ

Èý¡¢ÊµÑéÒªÇó

1¡¢¸ù¾ÝÌâÄ¿ÒªÇóÍê³É³ÌÐòÖÐûÓÐÍê³ÉµÄÄ£¿é¡£ 2¡¢Ð´ºÃÉÏ»ú±¨¸æ¡£

ʵÑéÆߣºÍ¼ÐÎÓû§½çÃæÉè¼Æ£¨Ò»£©

Ò»¡¢ ʵÑéÄ¿µÄ£º

1£® ¹®¹ÌͼÐÎÓû§½çÃæÉè¼ÆµÄ·½·¨ 2£® ÕÆÎÕʼþ´¦ÀíµÄÉè¼Æ·½·¨ ¶þ¡¢ ʵÑéÄÚÈÝ£º

1£®»æÖÆÈçÏÂÐÎʽµÄͼÐνçÃ棬ҪÇ󣺴°Ìå±³¾°ÎªÀ¶É«£¬ÖмäΪ»ÆÉ«·½¸ñ¡£

import java.awt.*; public class Test{ }

class F extends Frame{

public static void main(String args[]){ }

new F();

F(){

int x,y,w,h;

x = 200;y = 200;w = 200;h = 200; setBounds(x,y,w,h);

setBackground(Color.yellow); Panel p = new Panel();

p.setBounds(x/4,y/4,w/2,h/2); p.setBackground(Color.blue); setLayout(null);

15

XXѧԺjavaʵÑ鱨¸æ

} }

add(p);

setVisible(true);

2. ±àд³ÌÐò£¬»æÖÆÈçϸñʽµÄ½çÃ棺

import java.awt.*;

public class Test{

public static void main(String args[]){

Frame f = new Frame();

f.setLayout(new GridLayout(2,1)); f.setBounds(300,300,300,300);

Panel p1 = new Panel(new BorderLayout()); Panel p2 = new Panel(new BorderLayout()); Panel p11 = new Panel(new GridLayout(2,1)); Panel p21 = new Panel(new GridLayout(2,2)); p1.add(new Button(\),BorderLayout.WEST); p1.add(new Button(\),BorderLayout.EAST); p11.add(new Button(\)); p11.add(new Button(\)); p1.add(p11,BorderLayout.CENTER);

p2.add(new Button(\),BorderLayout.WEST); p2.add(new Button(\),BorderLayout.EAST); for(int i = 0;i<4;i++){

p21.add(new Button(\));

16

ÁªÏµ¿Í·þ£º779662525#qq.com(#Ì滻Ϊ@)