📄 lx.txt
字号:
例4.1 设计一个Applet小程序,使其可以进行简单的加法运算。(没有main方法)
// 源程序名:Addition.java
import java.awt.*; //引入awt包(界面设计)中的所有类
import java.awt.event.*;型//引入了awt的event包的所有类
import java.applet.Applet; //引入了applet包中的Applet类
public class Addition 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.2 下面这个Applet程序将在页面上输出两个矩形,并伴有文字输出。见图:
import java.awt.*;
import java.applet.Applet;
public class Class1 extends Applet {
private int x, y, width, height;
public void init() {
width=60;
height=60;
}
public void setPosition(int xPos, int yPos) {
x=xPos;
y=yPos;
}
public void paint(Graphics g) {
setPosition(20,20);
g.drawRect(x, y, width, height);
g.drawString("矩形 1 的 X 位置: "+x, 20,100);
g.drawString("矩形 1 的 Y 位置: "+y, 20,120);
setPosition(170,20);
g.drawRect(x, y, width, height);
g.drawString("矩形 2 的 X 位置: "+x, 170,100);
g.drawString("矩形 2 的 Y 位置: "+y, 170,120);
}
}
例4.3 下面这个程序是改写后的例4.2,输出结果与图4.2一样。
import java.awt.*;
import java.applet.Applet;
public class Class2 extends Applet {
MyBox b1=new MyBox();
MyBox b2=new MyBox(170,20,60,60);
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);
'
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);
}
}
例4.3 第二种方法。
import java.awt.*;
import java.applet.Applet;
public class Class2 extends Applet {
MyBox b1=new MyBox(20,20,60,60);
MyBox b2=new MyBox(170,20,60,60);
public void paint(Graphics g) {
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(int xPos, int yPos, int w, int h) {
x=xPos;
y=yPos;
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);
}
}
例4.4 显示当前日期和时间,运行结果见图:
import java.awt.Graphics;
import java.applet.Applet;
import java.util.Calendar;
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 ShowTime extends Applet {
Time t=new Time();
public void paint(Graphics g) {
g.drawString("当前日期:"+t.getDate(),50,40);
g.drawString("当前时间:"+t.getTime(),50,80);
}
}
例4.5 静态变量的使用。
class StaticDemo {
static int x;
int y;
static public int getX() {
return x;
}
static public void setX(int newX) {
x = newX;
}
public int getY() {
return y;
}
public void setY(int newY) {
y = newY;
}
}
public class ShowDemo {
public static void main(String[] args) {
System.out.println("静态变量x="+StaticDemo.getX());
System.out.println("实例变量y="+StaticDemo.getY()); // 非法,编译时将出错
StaticDemo a= new StaticDemo();
StaticDemo b= new StaticDemo();
a.setX(1);
a.setY(2);
b.setX(3);
b.setY(4);
System.out.println("静态变量a.x="+a.getX());
System.out.println("实例变量a.y="+a.getY());
System.out.println("静态变量b.x="+b.getX());
System.out.println("实例变量b.y="+b.getY());
}
}
例4.6 方法对对象行为的影响,运行结果见图
import java.awt.*;
import java.applet.Applet;
class DrawShape {
private int x, y, shape;
public void setPos(int xPos, int yPos) {
x=xPos;
y=yPos;
}
public void setShape(int choice) {
shape=choice;
}
public void draw(Graphics g) {
if (shape ==1)
g.drawRect(x, y, 60, 60);
else if (shape ==2)
g.drawOval(x, y, 60, 60);
else
g.drawString("形状参数不对!", 20, 120);
}
}
public class M1 extends Applet {
final int BOX=1, OVAL=2;
DrawShape a=new DrawShape();
public void paint(Graphics g) {
a.setPos(40,20);
a.setShape(BOX);
a.draw(g);
a.setPos(200,60);
a.setShape(OVAL);
a.draw(g);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -