资源描述:
《Java实验报告-4- 事件处理和图形化用户界面》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Java实验报告实验题目实验四事件处理和图形化用户界面importjava.awt.*;importjava.io.File;importjava.io.IOException;importjava.net.*;importjavax.imageio.ImageIO;importjavax.swing.*;importjava.awt.event.*;publicclassShowexpression{publicstaticvoidmain(String[]args){FaceFrameframe=newFaceFrame();frame.setDefaultClose
2、Operation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}}classFaceFrameextendsJFrame{FaceFrame.DrawButtonmyPanel=newFaceFrame.DrawButton();Stringchoice="";publicFaceFrame(){setTitle("显示表情");setSize(1200,600);add(myPanel);JPaneldown=newJPanel();JButtonbutton1=newJButton("开心");JButtonbutton
3、2=newJButton("生气");JButtonbutton3=newJButton("伤心");JButtonbutton4=newJButton("退出");button1.addActionListener(newFaceFrame.ButtonAction("happy"));6button2.addActionListener(newFaceFrame.ButtonAction("angry"));button3.addActionListener(newFaceFrame.ButtonAction("cry"));button4.addActionList
4、ener(newFaceFrame.ButtonAction("exit"));down.add(button1);down.add(button2);down.add(button3);down.add(button4);add(down,BorderLayout.SOUTH);}classButtonActionimplementsActionListener{privateStringstr;publicButtonAction(Stringstr){this.str=str;}publicvoidactionPerformed(ActionEvente){choi
5、ce=str;repaint();}}classDrawButtonextendsJPanel{Imageimg1,img2,img3;publicDrawButton(){}publicvoidpaintComponent(Graphicsg){super.paintComponent(g);Graphics2Dg2=(Graphics2D)g;if(choice.equals("happy")){g.drawOval(400,100,300,300);g.drawArc(380,200,200,250,60,60);g.drawArc(520,200,200,250,
6、60,60);6g.drawArc(470,250,150,100,200,140);}if(choice.equals("angry")){g.drawOval(400,100,300,300);g.drawLine(470,170,500,220);g.drawLine(500,220,530,170);g.drawLine(570,170,600,220);g.drawLine(600,220,630,170);g.drawLine(500,320,550,270);g.drawLine(550,270,600,320);}if(choice.equals("cry
7、")){g.drawOval(400,100,300,300);g.drawLine(450,200,520,200);g.drawLine(550,200,620,200);g.drawArc(470,300,150,100,20,140);}if(choice.equals("exit")){System.exit(0);}}}}实验结果666小结:6