ソースプログラム( HTML )
<object type="application/x-java-applet" width="550" height="40"> <param name="code" value="Cal.class"> </object>
ソースプログラム( Java )
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Cal extends Applet implements ActionListener {
Button bt;
TextField tx1, tx2, tx3;
/************/
/* 初期設定 */
/************/
public void init()
{
// レイアウトの変更(フローレイアウト)
setLayout(new FlowLayout(FlowLayout.CENTER));
// フォントの設定
Font f = new Font("MS 明朝", Font.PLAIN, 16);
setFont(f);
setBackground(Color.white);
// テキストフィールドとボタンの追加
// テキストフィールド
tx1 = new TextField(10);
tx1.setBackground(new Color(238, 255, 238));
add(tx1);
// ラベル
Label lb = new Label(" + ");
add(lb);
// テキストフィールド
tx2 = new TextField(10);
tx2.setBackground(new Color(238, 255, 238));
add(tx2);
// ボタン
bt = new Button("=");
bt.addActionListener(this); // リスナー
add(new Label(" "));
add(bt);
add(new Label(" "));
// テキストフィールド
tx3 = new TextField(10);
tx3.setBackground(new Color(238, 255, 238));
add(tx3);
}
/******************************/
/* ボタンが押されたときの処理 */
/******************************/
public void actionPerformed(ActionEvent e)
{
int a, b;
if (e.getSource() == bt) {
a = Integer.parseInt(tx1.getText());
b = Integer.parseInt(tx2.getText());
tx3.setText(Integer.toString(a+b));
}
}
}