Java Appletの解説

<11.テキストフィールドの使い方(その1)>

数字や文字の入力に使われるテキストフィールドの使い方を解説します。
テキストフィールドもボタン表示と同じように大きくは、2つの方法があります。anonymous内部クラスでの方法と独立したリスナークラスの方法です。

1.独立したリスナークラスの方法の例(テキストフィールドとボタンの併用)

テキストフィールドは独立したリスナークラスの方法、ボタンはanonymous内部クラスでの方法で作ってます。
1.<Javaファイル>
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;

public class tetext1 extends Applet{
String val,val1,val2,val3;
TextField tf1,tf2,tf3;
int s,s1,s2;
Button bt;
public void init(){
setBackground(Color.yellow);
add(tf1=new TextField("",6));
add(new Label("+"));
add(tf2=new TextField("",6));
add(bt=new Button("="));
add(tf3=new TextField("",10));
tf1.addActionListener(new MyListener());
tf2.addActionListener(new MyListener());

bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s1=Integer.parseInt(val1);
s2=Integer.parseInt(val2);
s=s1+s2;
val3=Integer.toString(s);
tf3.setText(val3);}
});}

class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
TextField t=(TextField)e.getSource();
val=t.getText();
if(t==tf1){val1=val;}
else if(t==tf2){val2=val;}
}}}
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
例1.ここをクリックして下さい。
2.<解説>
1)TextField tf1,tf2,tf3;
tf1,tf2,tf3のテキストフィールド宣言
2)add(tf1=new TextField("",6));
テキストフィールドtf1を初期値””(null)、文字数6で登録します。
3)tf1.addActionListener(new MyListener());
テキストフィールドtf1をリスナークラスとして外部に定義する。
4)bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ボタンbtをanonymous内部クラスで使用する。ボタンが押されれば以下を実行する。
1.s1=Integer.parseInt(val1);
テキストフィールドtf1に入力された文字列val1を整数に変換する。
2.val3=Integer.toString(s);
整数sを文字列に変換する。
3.tf3.setText(val3);
文字列val3をテキストフィールドtf3に設定(表示)する。
5)class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
テキストフィールドの処理。
1.TextField t=(TextField)e.getSource();
val=t.getText();
テキストフィールドに入力された文字の取得。
2.if(t==tf1){val1=val;}
入力されたテキストフィールドがtf1であれば、val1に入力された文字列valを代入する。
3.else if(t==tf2){val2=val;}
入力されたテキストフィールドがtf2であれば、val2に入力された文字列valを代入する。

2.anonymous内部クラスでの方法(テキストフィールドとボタンの併用)

テキストフィールドはanonymous内部クラスでの方法、ボタンは独立したリスナークラスの方法で作ってます。
1.<Javaファイル>
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
public void init(){

bt.addActionListener(new MyListener());
tf1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
TextField t=(TextField)e.getSource();
val1=t.getText();}
});
tf2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
TextField t=(TextField)e.getSource();
val2=t.getText();}
}); }

class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
s1=Integer.parseInt(val1);
s2=Integer.parseInt(val2);
s=s1+s2;
val3=Integer.toString(s);
tf3.setText(val3);
}}}
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
プログラムは違いますが、結果は同じですので省略します。
2.<解説>
1)bt.addActionListener(new MyListener());
ボタンをリスナークラスとして外部に定義する。
2)tf1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
テキストフィールドはanonymous内部クラスでの方法です。
3)TextField t=(TextField)e.getSource();
val1=t.getText();}
テキストフィールドtf1に入力された文字列をval1に代入する。
以下は1例の方法と同じです。

3.テキストフィールドの大きさ、色、場所指定(ボタンの併用)

テキストフィールドの大きさ、色、表示する場所を指定する方法です。
テキストフィールドは独立したリスナークラスの方法、ボタンはanonymous内部クラスでの方法。
1.<Javaファイル>
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;

public class tetext3 extends Applet{
String val,val1,val2,val3,cl="";
TextField tf1,tf2,tf3;
int s,s1,s2;
Button bt1,bt2;
Graphics g;
public void init(){
setBackground(Color.yellow);
setLayout(null);
tf1=new TextField("",10);
tf1.setBackground(Color.black);
tf1.setForeground(Color.yellow);
tf1.setFont(new Font("TimesRoman",Font.BOLD,24));
tf1.setSize(100,40);
tf1.setLocation(10,40);
add(tf1);
tf2=new TextField("",10);
tf2.setBackground(Color.black);
tf2.setForeground(Color.yellow);
tf2.setFont(new Font("TimesRoman",Font.BOLD,24));
tf2.setSize(100,40);
tf2.setLocation(180,40);
add(tf2);
tf3=new TextField("",10);
tf3.setBackground(Color.green);
tf3.setForeground(Color.blue);
tf3.setFont(new Font("TimesRoman",Font.BOLD,24));
tf3.setSize(100,40);
tf3.setLocation(350,40);
add(tf3);

bt1=new Button("=");
bt1.setBackground(Color.pink);
bt1.setForeground(Color.blue);
bt1.setFont(new Font("TimesRoman",Font.BOLD,24));
bt1.setSize(50,40);
bt1.setLocation(290,40);
add(bt1);
bt2=new Button("Clear");
bt2.setBackground(Color.white);
bt2.setForeground(Color.black);
bt2.setFont(new Font("TimesRoman",Font.BOLD,20));
bt2.setSize(60,40);
bt2.setLocation(460,40);
add(bt2);

tf1.addActionListener(new MyListener());
tf2.addActionListener(new MyListener());
bt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
s1=Integer.parseInt(val1);
s2=Integer.parseInt(val2);
s=s1+s2;
val3=Integer.toString(s);
tf3.setText(val3);}
});
bt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf1.setForeground(Color.yellow);tf1.setText(cl);
tf2.setForeground(Color.yellow);tf2.setText(cl);
tf3.setText(cl);val1="0";val2="0";
}}); }
class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
TextField t=(TextField)e.getSource();
val=t.getText();
if(t==tf1){val1=val;tf1.setForeground(Color.white);tf1.setText(val1);}
else if(t==tf2){val2=val;tf2.setForeground(Color.white);tf2.setText(val2);}
} }
public void paint(Graphics g){
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.BOLD,26));
g.drawString("+",130,70);
} }
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
例2.ここをクリックして下さい。
2.<解説>
1)setLayout(null);
これを宣言しないとレイアウト表示になるため任意の場所に表示することはできません。
2)テキストフィールドの大きさ、色、表示する場所を指定。
1.tf1.setBackground(Color.black);
テキストフィールドtf1の背景に色指定。この場合は黒になります。
2.tf1.setForeground(Color.yellow);
テキストフィールドの文字の色指定。この場合は黄色になります。
3.tf1.setFont(new Font("TimesRoman",Font.BOLD,24));
テキストフィールドの文字の大きさ、書体の指定。
4.tf1.setSize(100,40);
テキストフィールドの大きさ指定。(x軸、y軸)
5.tf1.setLocation(10,40);
テキストフィールドの場所指定。(x軸、y軸)
以下似たような感じで他のテキストフィールド、ボタンを指定します。
3)テキストフィールドをリスナークラスとして外部に定義する。
tf1.addActionListener(new MyListener());
テキストフィールドはMyListenerとして定義する。
4)ボタンはanonymous内部クラスでの方法
bt1は「=」ボタンで前の例と同じような処理しています。
bt2は「Clear」ボタンで初期化してます。
tf1.setForeground(Color.yellow);tf1.setText(cl);
文字の色を初期の黄色にもどして、テキストフィールドをcl(クリアー)します。
5)テキストフィールド
if(t==tf1){val1=val;tf1.setForeground(Color.white);tf1.setText(val1);}
テキストフィールドtf1に入力(リターンキーが押された時)された時、文字を白く表示する。



トップページ

趣味のトップページ

娯楽室の各部屋
Aplletの解説へ戻る

Scriptの解説 Scriptのゲーム Appletのゲーム Linuxの部屋 物置部屋