資源描述:
《馬俊-蘭大java實(shí)驗(yàn)-09》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、//AppletPrg.javaimportjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;publicclassAppletPrgextendsJAppletimplementsActionListener{JPanelcenterPanel,northPanel;JButtonb1,b2;CardLayoutcard;publicvoidinit(){setLayout(newBorderLayout());centerPa
2、nel=newJPanel();northPanel=newJPanel();card=newCardLayout();centerPanel.setLayout(card);Circlecircle=newCircle();Trangletrangle=newTrangle();centerPanel.add("trangle",trangle);centerPanel.add("circle",circle);b1=newJButton("計算圓面積");b2=newJButton("計算三角形面積");b1.addAct
3、ionListener(this);b2.addActionListener(this);northPanel.add(b1);northPanel.add(b2);add(centerPanel,BorderLayout.CENTER);add(northPanel,BorderLayout.NORTH);}publicvoidactionPerformed(ActionEvente){if(e.getSource()==b1){card.show(centerPanel,"circle");}elseif(e.getSou
4、rce()==b2){card.show(centerPanel,"trangle");}}}classCircleextendsJPanelimplementsActionListener{doubler,s;JTextFieldrad=null,area=null;JButtonb=null;Circle(){rad=newJTextField(10);area=newJTextField(10);b=newJButton("確定");add(newJLabel("輸入半徑"));add(rad);add(newJLabe
5、l("面積是"));add(area);add(b);b.addActionListener(this);}publicvoidactionPerformed(ActionEvente){try{r=Integer.parseInt(rad.getText());s=Math.PI*r*r;area.setText(""+s);}catch(Exceptionee){rad.setText("請輸入數(shù)字字符");}}}classTrangleextendsJPanelimplementsActionListener{doubl
6、ea=0,b=0,c=0,s;JTextFieldba=newJTextField(6),bb=newJTextField(6),bc=newJTextField(6),area=newJTextField(24);JButtonbutton=newJButton("確定");Trangle(){add(newJLabel("輸入三邊的長度"));add(ba);add(bb);add(bc);add(newJLabel("面積是"));add(area);add(button);button.addActionListene
7、r(this);}publicvoidactionPerformed(ActionEvente){try{a=Double.parseDouble(ba.getText());b=Double.parseDouble(bb.getText());c=Double.parseDouble(bc.getText());if(a+b>c&&b+c>a&&a+c>b){doublep=(a+b+c)/2;s=Math.sqrt(p*(p-a)*(p-b)*(p-c));area.setText(""+s);}else{area.set
8、Text("您輸入的數(shù)字不能形成三角形");}}catch(Exceptionee){area.setText("請輸入數(shù)字字符");}}}//Flower.javaimportjava.awt.*;importjava.awt.event.*;importjava.awt.