//Sample23 import java.applet.Applet; import java.awt.Button; import java.awt.Graphics; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Sample23 extends Applet implements ActionListener { private static final String window_title = "ボタンクリックとテキストフィールド表示"; private static final int window_width = 420; private static final int window_height = 200; private static final String b01_title = "A to B"; private static final String b02_title = "B to A"; private static final String data1 = "ああああ"; private static final String data2 = "いいいいいいいいいい"; private static final int tf01_size = 60; private static final int tf02_size = 20; private static final long serialVersionUID = 1L; Button b01; Button b02; TextField tf01 = new TextField("", Sample23.tf01_size); TextField tf02 = new TextField("", Sample23.tf02_size); int flg_button = -1; // 初期処理 @Override public void init() { this.setSize(Sample23.window_width, Sample23.window_height); this.add(new Label(Sample23.window_title)); this.b01 = new Button(Sample23.b01_title); this.b02 = new Button(Sample23.b02_title); this.add(this.b01); this.add(this.b02); this.add(this.tf01); this.add(this.tf02); this.b01.addActionListener(this); this.b02.addActionListener(this); } // 描画 @Override public void paint(final Graphics g) { if (this.flg_button == -1) { this.tf01.setText(""); this.tf02.setText(""); } if (this.flg_button == 0) { this.tf01.setText(Sample23.data1); this.tf02.setText(Sample23.data2); } if (this.flg_button == 1) { this.tf01.setText(Sample23.data2); this.tf02.setText(Sample23.data1); } } // イベント public void actionPerformed(final ActionEvent ae) { if (ae.getSource() == this.b01) { this.flg_button = 0; this.repaint(); } if (ae.getSource() == this.b02) { this.flg_button = 1; this.repaint(); } } }