/************************************************** WZ ASUST 2016 备注: 必要的配置 运行java文件后产生 class文件最后运行浏览器 需要删除原有的class文件 而且浏览器具有记忆功能 需要关闭再来 等待3秒就能在基本页面加载后加载APPLET java控制台 然后必要时请点击运行。 **************************************************/ import java.applet.*; import java.awt.*; import java.net.*; import java.awt.event.*; import java.applet.Applet; import java.awt.*; import java.awt.Graphics; import java.applet.Applet; import java.util.Calendar; import java.awt.Graphics; import java.applet.Applet; import java.util.Calendar; import java.awt.Graphics; import java.applet.Applet; public class wz extends Applet{ /********* 10 ******************** public void paint(Graphics g) { int x=10, y=10; g.drawString("1:x="+x+", y="+y,30,30); ff1(x, y,g); } //static void ff1(int passX, int passY,Graphics g) { void ff1(int passX, int passY,Graphics g) { passX=passX*passX; passY=passY*passY; g.drawString(" 2: passX="+passX+", passY="+passY,30,60); } } /********* 9 *********** NO ERROR NO OUTPUT********* public class wz extends Applet{ Graphics g; public void paint() { int x=10, y=10; g.drawString("1:x="+x+", y="+y,30,30); //ff1(x, y); } //static void ff1(int passX, int passY) { void ff1(int passX, int passY) { passX=passX*passX; passY=passY*passY; g.drawString(" 2: passX="+passX+", passY="+passY,30,60); } } /********************* 8 *************** class StaticDemo { static int x; int y; public static int getX() { return x; } public static void setX(int newX) { x = newX; } public int getY() { return y; } public void setY(int newY) { y = newY; } } public class wz extends Applet{ public void paint(Graphics g) { g.drawString("1: 静态变量x="+StaticDemo.getX(),30,30); // g.drawString("2: 实例变量y="+StaticDemo.getY(),30,30); // 非法,编译出错 StaticDemo a= new StaticDemo(); StaticDemo b= new StaticDemo(); a.setX(1); a.setY(2); b.setX(3); b.setY(4); g.drawString("3:静态变量a.x="+a.getX(),30,60); g.drawString("3:实例变量a.y="+a.getY(),30,90); g.drawString("3:静态变量b.x="+b.getX(),30,120); g.drawString("3:实例变量b.y="+b.getY(),30,150); } } ********************* 7 **** error ************ class RunDemo extends Applet{ private String userName, password; RunDemo() { System.out.println("全部为空!"); } RunDemo(String name) { userName=name; } RunDemo(String name, String pwd) { this(name); password=pwd; check(); } void check() { String s=null; if (userName!=null) s="用户名:"+userName; else s="用户名不能为空!"; if (password!="12345678") s=s+" 口令无效!"; else s=s+" 口令:********"; System.out.println(s); } public void paint(Graphics g) { g.drawString("1:"+new RunDemo(),30,30); g.drawString("2:"+new RunDemo("刘新宇"),30,60); g.drawString("3:"+new RunDemo(null,"邵丽萍"),30,90); g.drawString("3:"+new RunDemo("张驰","12345678"),30,120); } } public class wz { RunDemo wz1=new RunDemo(); // wz.paint(); error need 标识 } /******************* 6 *************************** class RunDemo { private String userName, password; RunDemo() { System.out.println("全部为空!"); } RunDemo(String name) { userName=name; } RunDemo(String name, String pwd) { this(name); password=pwd; check(); } void check() { String s=null; if (userName!=null) s="用户名:"+userName; else s="用户名不能为空!"; if (password!="12345678") s=s+" 口令无效!"; else s=s+" 口令:********"; System.out.println(s); } } public class wz extends Applet{ public void paint(Graphics g) { g.drawString("1:"+new RunDemo(),30,30); g.drawString("2:"+new RunDemo("刘新宇"),30,60); g.drawString("3:"+new RunDemo(null,"邵丽萍"),30,90); g.drawString("3:"+new RunDemo("张驰","12345678"),30,120); } } ******************* 5 *************************** public class wz extends Applet implements ActionListener { Label label1=new Label("+"); Label label2=new Label("="); TextField field1=new TextField(6); TextField field2=new TextField(6); TextField field3=new TextField(6); Button button1=new Button("相加"); public void init() { // 初始化 add(field1); add(label1); add(field2); add(label2); add(field3); add(button1); button1.addActionListener(this); } public void actionPerformed(ActionEvent e) { // 处理按钮事件 int x=Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText()); field3.setText(Integer.toString(x)); // 数值转换为字符串 } } *************** 4 ********* public class wz extends Applet { MyBox b1=new MyBox();//创建对象 b1 MyBox b2=new MyBox(170,20,60,60); //创建对象 b2 public void paint(Graphics g) { b1.setPosition(20,20); b1.setSize(60,60); b1.draw(g); g.drawString("矩形 1 的 X 位置: "+b1.getX(), 20, 100); g.drawString("矩形 1 的 Y 位置: "+b1.getY(), 20, 120); b2.draw(g); g.drawString("矩形 2 的 X 位置: "+b2.getX(), b2.getX(), b2.getY()+80); g.drawString("矩形 2 的 Y 位置: "+b2.getY(), b2.getX(), b2.getY()+100); } } class MyBox { private int x, y, width, height; MyBox() { x=0; y=0; width=0; height=0; } MyBox(int xPos, int yPos, int w, int h) { x=xPos; y=yPos; width=w; height=h; } public void setPosition (int xPos, int yPos) { x=xPos; y=yPos; } public void setSize (int w, int h) { width=w; height=h; } public int getX() { return x; } public int getY() { return y; } public void draw(Graphics g) { g.drawRect(x, y, width, height); } } /********** 3 ********** class IntSort { public String sort(int a, int b) { if (a>b) return a+" "+b; else return b+" "+a; } public String sort(int a, int b, int c) { int swap; if (a<b) { swap=a; a=b; b=swap; } if (a<c) { swap=a; a=c; c=swap; } if (b<c) { swap=b; b=c; c=swap; } return a+" "+b+" "+c; } public String sort(int arr[]) { String s=" "; int swap; for (int i=0; i<arr.length; i++) for (int j=0; j<arr.length-1; j++) if (arr[j]>arr[j+1]) { swap=arr[j]; arr[j]=arr[j+1]; arr[j+1]=swap; } for (int i=0; i<arr.length; i++) s=s+arr[i]+" "; return s; } } public class wz extends Applet { IntSort s=new IntSort(); public void paint(Graphics g) { int a=30, b=12, c=40; int arr[]={34,8,12,67,44,98,52,23,16,16}; g.drawString("两个数的排序结果:"+s.sort(a,b),30,30); g.drawString("三个数的排序结果:"+s.sort(a,b,c),30,60); g.drawString("数组的排序结果:"+s.sort(arr),30,90); } } ********** 2 ********** class Time { private Calendar t; private int y, m, d, hh, mm, ss; Time (){ t=Calendar.getInstance(); y=t.get(t.YEAR); m=t.get(t.MONTH)+1; d=t.get(t.DATE); hh=t.get(t.HOUR_OF_DAY); mm=t.get(t.MINUTE); ss=t.get(t.SECOND); } public String getDate() { return y+" 年"+m+"月"+d+"日"; } public String getTime() { String s=hh+" 时"+mm+"分"+ss+"秒"; return s; } } public class wz extends Applet { Time t=new Time(); public void paint(Graphics g) { g.drawString("当前日期:"+t.getDate(),50,40); g.drawString("当前时间:"+t.getTime(),50,80); } } ********** 1 ********** public void paint(Graphics g) {g.drawRect(0,0,250,100);g.setColor(Color.blue); g.drawString("Look at me, I‘m a Java Applet!",10,50); } *********************/ HTML <HTML> <HEAD> <TITLE>My First Java Applet</title> </HEAD> <BODY> <a href="http://www.jb51.net/article/56449.htm">wzzx</a></br> <a href="http://yyjlinux.iteye.com/blog/1702514/">wzzx</a></br> Here‘s my first Java Applet: <applet code="wz.class" width="300" height ="300"> </BODY> </HTML>
时间: 2025-01-07 07:00:46