1、 super.paint( g ); g.setColor(Color.blue); / call superclasss paint method g.drawLine(10, 10,100,10); g.drawLine(10, 20,100,20 ); g.drawLine(10, 30,100,30 ); g.drawLine(10, 40,100,40 ); g.drawLine(10, 50,100,50 ); g.drawLine(10, 60,100,60 ); g.drawLine(10, 10, 10,60 ); g.drawLine(20, 10, 20, 60 ); g
2、.drawLine(30, 10, 30, 60 ); g.drawLine(40, 10, 40, 60 ); g.drawLine(50, 10, 50, 60 ); g.drawLine(60, 10, 60, 60 ); g.drawLine(70, 10, 70, 60 ); g.drawLine(80, 10, 80, 60 ); g.drawLine(90, 10, 90, 60 ); g.drawLine(100,10, 100,60 ); 问题:如何改变窗口的大小?答:在编译的过程中提示applet标记需要宽度。但是定义了以后用appletviewer MyApplet.ht
3、ml时却并未显示,setSize();这个函数并不是applet的函数,通过XX查到用void reset();函数,这样就可以变化窗口的大小了。任务2.编写一个Java Applet小应用程序,该程序随机产生一个圆半径,然后显示该圆的半径、周长和面积。最后,说说你程序设计中体现的面向对象的编程思想。提示:使用方法Math.random()可以产生一个双精度型(0,1)的随机数。import java.awt.*;class yuan private int banjing; private int mian; private int zhouchang; public void yuan(i
4、nt x) banjing=x; public void mianji() mian=(int)3.14*banjing*banjing; public void suanzhouchang() zhouchang=2*(int)3.14*banjing; public int getbanjing() return banjing; public int getmianji() return mian; public int getzhouchang() return zhouchang; public void paint( Graphics g ) this.resize(1000,10
5、00); yuan yuan1=new yuan(); double r=Math.random()* 300; int a=(int)r; yuan1.yuan(a); yuan1.mianji(); yuan1.suanzhouchang(); int banjing=yuan1.getbanjing(); g.setColor(Color.red); g.drawString(半径 :+yuan1.getbanjing(),25,25);面积 :+yuan1.getmianji(),25,55);周长 :+yuan1.getzhouchang(),25,85); g.fillOval(1
6、20,55,banjing,banjing);任务3.建立Java Applet小应用程序,随机产生5个数,然后以条形图的形式来表示这5个数。public class GraphicsTester extends JApplet int shu1=(int)(Math.random()*500);/不要放进paint函数中否则移动就会 int shu2=(int)(Math.random()*500);/重新画图 int shu3=(int)(Math.random()*500); int shu4=(int)(Math.random()*500); int shu5=(int)(Math.r
7、andom()*500); String a=String.valueOf(shu1); String b=String.valueOf(shu2); String c=String.valueOf(shu3); String d=String.valueOf(shu4); String e=String.valueOf(shu5);/重载JApplet类的paint方法,参数是Graphics类的对象,是由浏览器传递过来的 public void paint(Graphics g) super.paint(g); /调用其超类JApplet类的paint方法 this.resize(500,
8、500); g.setFont( new Font( SansSerif, Font.BOLD, 12 ) ); g.drawString(a,0,500-shu1-20); g.fillRect(0,500-shu1,100,shu1); g.setColor(Color.yellow); g.drawString(b,100,500-shu2-20); g.fillRect(100,500-shu2,100,shu2); g.drawString(c,200,500-shu3-20); g.fillRect(200,500-shu3,100,shu3); g.setColor(Color.
9、green); g.drawString(d,300,500-shu4-20); g.fillRect(300,500-shu4,100,shu4); g.setColor(Color.black); g.drawString(e,400,500-shu5-20); g.fillRect(400,500-shu5,100,shu5); 任务4.(选作)使用JApplet实现某一门课程的分数录入系统,界面包括学号、姓名、班级、班号、成绩的输入域和一个录入按扭。当用户单击录入按扭时,程序检查各输入域中是否有非空的数据,若有空域则抛出一个Exception对象;同时程序检查成绩输入域,如果不是数值信
10、息,或是负的数值信息,也抛出相应的异常。在抛出异常之前应提供详细的信息说明,从而使使用该程序的用户了解所出现的问题,同时考虑上述异常出现时程序应该如何处理。import java.awt.event.*;import javax.swing.event.*;import java.lang.*;class ActionHandle public JButton but1 = new JButton(显示) ; public JButton but2 = new JButton(重置 public JTextField text1 = new JTextField() ; public JTex
11、tField text2 = new JTextField() ; public JTextField text3 = new JTextField() ; public JTextField text4 = new JTextField() ; / 声明一个按钮 public JLabel labnumber = new JLabel() ; public JLabel labclass = new JLabel() ; public JLabel labname = new JLabel() ; public JLabel labmark = new JLabel() ; public J
12、TextArea labinfo = new JTextArea() ; / 声明一个标签 public JPanel pan = new JPanel() ; private String word; public ActionHandle() Font fnt = new Font(Serief, Font.ITALIC + Font.BOLD, 12); labnumber.setFont(fnt) ; / 设置标签的显示字体 labnumber.setText(学号 / 设置默认显示文字 labclass.setFont(fnt) ; labclass.setText(班级 labna
13、me.setFont(fnt) ; labname.setText(姓名 labmark.setFont(fnt) ; labmark.setText(成绩 labinfo.setLineWrap(true); labinfo.setFont(fnt) ; labinfo.setText(分数系统欢迎您 but1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(e.getSource()=but1) String str1=text1.getText(); String s
14、tr2=text2.getText(); String str3=text3.getText(); String str4=text4.getText(); String str5=panduan(str1,str2,str3,str4); labinfo.setText(str5); ); but2.addActionListener(new ActionListener() if(e.getSource()=but2) text1.setText( text2.setText( text3.setText( text4.setText( labinfo.setText( public St
15、ring panduan(String a,String b,String c,String d) String word=; String word1= String word2= String word3= String word4= String word5= if(a.length()=0) word1=学号不能为空 else if(b.length() word2=班级不能为空 if(c.length() word3=姓名不能为空 if(d.length()=0;) if (!Character.isDigit(str.charAt(i) return true; return fa
16、lse;public class JAppleta extends JApplet public void init() this.setLayout(null); ActionHandle t1=new ActionHandle(); t1.labnumber.setBounds(5,5,60,20); t1.labclass.setBounds(5,30,60,20); t1.labname.setBounds(5,55,60,20); t1.labmark.setBounds(5,80,60,20); t1.text1.setBounds(65,5,100,20); t1.text2.s
17、etBounds(65,30,100,20); t1.text3.setBounds(65,55,100,20); t1.text4.setBounds(65,80,100,20); t1.but1.setBounds(10,110,60,20); t1.but2.setBounds(80,110,60,20); t1.labinfo.setBounds(5,130,200,100); this.add(t1.labnumber); this.add(t1.text1); this.add(t1.labclass); this.add(t1.text2); this.add(t1.labnam
18、e); this.add(t1.text3); this.add(t1.labmark); this.add(t1.text4); this.add(t1.but1); this.add(t1.but2); this.add(t1.labinfo); this.add(t1.pan);这个实验题目里面最难得部分就是原来运行JFrame现在变成了JApplet。原本都是向JFrame这个容器里面加东西,可是现在加上会出错,网页右上角会显示一个小红叉,大概意思是不能把JFrame里面添加容器。这样就出现了很多错误和改变,后来把void init()当成JFrame直接添加这些就没有出错。思考题:1. 什么是小应用程序?它与独立应用程序有什么区别?小应用程序必须配合html或者用appletviewer来查看,独立程序则不用。2. 简述小应用程序的生命周期。一开始也就是打开浏览器的时候下载用init()方法初始化,之后start进入运行,这时候比如最小化一类活动会stop();退出浏览器则会调用destory()方法。3.repaint()方法的功能是什么? repaint()通过调用线程再由线程去调用update()方法清除当前显示并再调用paint()方法进行绘制下一个需要显示的内容这样就起到了一种图片的交替显示从而在视角上形成了动画。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1