JAVA簡易講座 8

きょうからあなたもジャワ言人 99/11/15
by Kensaku Hattori


←前頁 次頁→

その8(GUI1)

 今度の例題は、GUI入出力のプログラムです。
見所はレイアウトマネージャの使い方です。
JAVAのGUIプログラム時には、レイアウトマネージャを使って 部品を配置します。
GridBagLayoutは、一番汎用性のあるレイアウトマネージャです。
ついでに、日付の文字列の編集方法もよくみておいてね。
今回のプログラムは、Appletではないので、java Caldate で起動して下さい。
ほんとは、JFCという最新のGUI部品を使いたかったん だけど、私のNoteパソコンではうまく動かなかったので、皆さんでチャレンジ して下さい。
Frame->JFrame のようにGUI部品のクラス名をJ付きにすれば、おおむね いけるはずです。

/**
    日付の計算

    @author 服部建作
    @version 1.0 1999/11/09
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;

public class Caldate extends Frame{
    TextField  intext, anstext;
    Button button;

    //コンストラクタで画面表示部品の配置を行う
    Caldate(){
        //親クラスのコンストラクタで外形を作成
        super("日付の計算");
        setBackground(new Color( 0xc08080));    //背景色
        //ウィンドウのイベント処理を登録
        addWindowListener( new WindowAdapter() {
            public void windowClosing( WindowEvent e){
                System.exit(0);
            }
        });
        //Grid(格子状)のレイアウトマネージャを使用する
        GridBagLayout gridbag = new GridBagLayout();
        //Gridbagレイアウトの制御にはGridBagConstraintsクラスが必要
        GridBagConstraints cons = new GridBagConstraints();
        //このフレームの標準フォントを設定
        this.setFont(new Font("Serif", Font.PLAIN, 14));
        //このフレームにレイアウトマネージャを設定
        this.setLayout( gridbag);
        //ラベルを配置
        cons.insets = new Insets(3,3,3,3);  //部品の周りの余白をピクセルで指定
        cons.anchor = GridBagConstraints.CENTER;    //セル内の配置
        cons.gridx = 0; cons.gridy = 0; //グリッドのX,Y座標を指定
        cons.gridheight = 1;    //グリッド上で占める行数
        cons.gridwidth = 1;     //グリッド上で占める列数
        cons.weightx = cons.weighty = 0.0;  //空きスペース配分の重み
        this.add(new Label("今日から"), cons);
        //テキストボックスを配置
        cons.gridx = 1; cons.gridy = 0; //グリッドのX,Y座標を指定
        intext = new TextField( "1", 3);
        this.add( intext, cons);
        //ラベルを配置
        cons.gridx = 2; cons.gridy = 0; //グリッドのX,Y座標を指定
        this.add(new Label("日後は"), cons);
        //テキストボックスを配置
        cons.gridx = 3; cons.gridy = 0; //グリッドのX,Y座標を指定
        anstext = new TextField( "", 12);
        this.add( anstext, cons);
        anstext.setEditable(false); //入力禁止にする
        anstext.setBackground(new Color( 0xc0c000));
        //ラベルを配置
        cons.gridx = 4; cons.gridy = 0; //グリッドのX,Y座標を指定
        this.add(new Label("です。"), cons);
        //次の行にボタンを配置
        cons.gridx = 3; cons.gridy = 1;
        cons.gridwidth = 2;		//2列分使用する
        button = new Button("計算実行");
        this.add( button, cons);
        //ボタンにイベントを登録
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ev){
                dispDate();
            }
        });
        //フレームの大きさを指定
        setSize( 400, 100);
        //フレーム表示
        setVisible(true);
    }
    public static void main(String[] args){
        //メインはコンストラクタを呼出すだけ
        Caldate form = new Caldate();
    }
    
    //指定日後の日付を表示
    void dispDate(){
        Calendar now = Calendar.getInstance();  //現在日時
        
        anstext.setText("");            //出力欄をクリア
        String hi = intext.getText();   //入力文字列の取得
        //入力がない時は即リターン
        if( hi.trim().equals("")){
            return;
        }
        int iday;
        try{
            iday = Integer.parseInt(hi);
        }catch( NumberFormatException e){
            anstext.setText("整数値を指定して下さい");
            return;
        }
        //日付の計算
        now.add(Calendar.DATE, iday);
        SimpleDateFormat form = new SimpleDateFormat("yyyy年MM月dd日");
        anstext.setText(form.format(now.getTime()));
    }
}

importについて

GUI部品の配置の仕方

GridBagLayoutの使いかた

日付の扱いかた

←前頁 次頁→

レイアウトマネージャ: JFC(Java Foundation Class):

ご意見ご感想をお寄せ下さい。 kensaku@hi-ho.ne.jp