next up previous
Next: 3.2 チェックボックスなどの処理 ItemListener Up: 3 イベント処理 Previous: 3 イベント処理

3.1 ボタンなどのイベント処理ActionListener

/*<applet code='ButtonView'
  width='150' height='100'>
  </applet>*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonView
  extends Applet implements ActionListener{
  Button button;
  public void init() {
    button = new Button("Button");
    button.addActionListener(this);
    add(button);
  }
  
  public void 
    actionPerformed(ActionEvent event) {
    button.setLabel("Pressed");
  }
}
図 4: ボタンの例
\includegraphics[width=4cm]{/home/inaba/eps/lecture/fig/ButtonViewPage.eps} \includegraphics[width=4cm]{/home/inaba/eps/lecture/fig/ButtonViewPressed.eps}
ボタンを押すとラベル表示を行う例を示す.
/*<applet code='LabelView'
  width='150' height='100'>
  </applet>*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class LabelView
    extends Applet
    implements ActionListener{
    Button button;
    Label label;
    public void init() {
        button = new Button("変更");
        button.addActionListener(this);
        add(button);
        label = new Label("変更前です");
        add(label);
    }
    
    public void
    actionPerformed(ActionEvent event) {
        label.setText("変更しました");
    }
}
図 5: ラベルの例
\includegraphics[width=4cm]{/home/inaba/eps/lecture/fig/LabelViewPage.eps} \includegraphics[width=4cm]{/home/inaba/eps/lecture/fig/LabelViewPage2.eps}
一行の文字列を入力するテキストフィールド(TextField)と, 複数行のテキストを入力できるテキストエリア(TextArea)の例を示す.
/*<applet code='TextAreaView'
  width='300' height='200'>
  </applet>*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 
public class TextAreaView
    extends Applet
    implements ActionListener{
    TextField field;
    TextArea area;
    public void init() {
        field = new TextField(30);
        area = new TextArea(6, 30);
        field.addActionListener(this);
        add(field);
        add(area);
    }
    
    public void
    actionPerformed(ActionEvent event) {
        area.append(field.getText() + "\n");
        field.setText("");
    }
}
図 6: テキストエリアの例
\includegraphics[width=6cm]{/home/inaba/eps/lecture/fig/TextAreaView.eps}


generated through LaTeX2HTML. M.Inaba 平成18年5月7日