?? 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 + -