?? 第九章例子.txt
字號:
9-例子1
import java.applet.*;
import java.awt.*;
public class Example9_1 extends Applet
{ Button button1,button2;
public void init()
{ button1=new Button();
button2=new Button("確定");
add(button1);add(button2);
}
}
9-例子2
import java.applet.*;
import java.awt.*;
import java.awt.event.*; //需要該包中的ActionEvent類和接口ActionListener.
public class Example9_2 extends Applet implements ActionListener
{ TextField text1,text2,text3;
Button button1,button2,button3,button4;
public void init()
{ text1=new TextField(10);
text2=new TextField(10);
text3=new TextField(10);
add(text1);add(text2);add(text3);
button1=new Button("加");
button2=new Button("減");
button3=new Button("乘");
button4=new Button("除");
add(button1);add(button2);add(button3);add(button4);
button1.addActionListener(this); //將主類的實例作為button1的監視器,為了處
// 理按鈕事件,主類必須實現接口ActionListener。
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
}
public void paint(Graphics g)
{ g.drawString("在左邊第一、二文本框輸入數字,再單擊相應的按鈕",10,100);
g.drawString("第三個文本框顯示運算結果",10,120);
}
public void actionPerformed(ActionEvent e)//接口方法的實現.
{ double n;
if(e.getSource()==button1)
{ double n1,n2;
n1=Double.valueOf(text1.getText()).doubleValue();//將text1中的文本轉化為
//double型數據。
n2=Double.valueOf(text2.getText()).doubleValue();
n=n1+n2; text3.setText(String.valueOf(n)); //將n轉化為字符串。
}
else if(e.getSource()==button2)
{ double n1,n2;
n1=Double.valueOf(text1.getText()).doubleValue();
n2=Double.valueOf(text2.getText()).doubleValue();
n=n1-n2; text3.setText(String.valueOf(n)); //將n轉化為字符串。
}
else if(e.getSource()==button3)
{ double n1,n2;
n1=Double.valueOf(text1.getText()).doubleValue();
n2=Double.valueOf(text2.getText()).doubleValue();
n=n1*n2; text3.setText(String.valueOf(n)); //將n轉化為字符串。
}
else if(e.getSource()==button4)
{ double n1,n2;
n1=Double.valueOf(text1.getText()).doubleValue();
n2=Double.valueOf(text2.getText()).doubleValue();
n=n1/n2; text3.setText(String.valueOf(n)); //將n轉化為字符串。
}
}
}
9-例子3
import java.applet.*;import java.awt.*;
import java.awt.event.*; //需要該包中的TextEvent類
// 和接口TextListener,ActionListener。
public class Example9_3 extends Applet implements TextListener,ActionListener
{ TextArea text1,text2;Button button;
public void init()
{ text1=new TextArea(" ",10,10);
text2=new TextArea(" ",10,10);
button=new Button("確定");
add(text1);add(text2); add(button);
text1.addTextListener(this); //將主類的實例作為text1,button的監視器,
button.addActionListener(this); //為了處理相應的事件,主類必須實現相應的接口
//ActionListener和TextListener。
}
public void textValueChanged(TextEvent e)
{ if(e.getSource()==text1)
{ text2.setText(text1.getText());
}
else
{}
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button)
text2.setText("我按了確定按鈕");
else
{ }
}
}
9-例子4
import java.awt.*;import java.applet.*;
import java.awt.event.*;
//寫一個按扭類的子類,增加一些新的功能:
class MyButton extends Button implements ActionListener,TextListener
{ TextArea text1,text2; //類的成員變量。
MyButton(String s)
{ super(s); //調用父類的構造方法。
text1=new TextArea(8,8);
text2=new TextArea(10,10);
text1.addTextListener(this);//創建的按扭監視其中一個文本區。
this.addActionListener(this); //創建的按扭自己監視自己。
}
public void textValueChanged(TextEvent e)//實現接口。
{ text2.setText(text1.getText());
}
public void actionPerformed(ActionEvent e)//實現接口。
{ text1.setText(null);
}
}
public class E9_4 extends Applet implements ActionListener
{ MyButton button;
public void init()
{ button=new MyButton("點擊我看能發生什么?");
button.addActionListener(this);//小程序也監視按扭。
add(button.text1);add(button.text2);add(button);
}
public void actionPerformed(ActionEvent e) //實現接口。
{ button.text1.setText(button.getLabel());
button.text2.setText("ok");
}
}
9-例子5
import java.awt.*;import java.applet.*;
public class Example9_5 extends Applet
{public void init()
{MyButton_1 mybutton_1=new MyButton_1();
MyButton_2 mybutton_2=new MyButton_2(40,80);
setLayout(null);
add(mybutton_1); add(mybutton_2);
mybutton_1.setLocation(12,12);
mybutton_2.setLocation(60,12);
}
}
class MyButton_1 extends Button
{
MyButton_1()
{setSize(25,160);
}
public void paint(Graphics g)
{g.drawString("我",6,14);
g.drawString("是",6,34);
g.drawString("一",6,54);
g.drawString("個",6,74);
g.drawString("豎",6,94);
g.drawString("按",6,114);
g.drawString("鈕",6,134);
g.drawString("!",8,154);
}
}
class MyButton_2 extends Button
{ int a,b;
MyButton_2(int a,int b)
{this.a=a;this.b=b;
setSize(a,b);
}
public void paint(Graphics g)
{g.setColor(Color.red);
g.fillOval(a/4,3,a/2,a/2 ); //在按扭上畫圓,見17章。
g.setColor(Color.yellow);
g.fillOval(a/4,28,a/2,a/2 );
g.setColor(Color.green);
g.fillOval(a/4,53,a/2,a/2 );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -