プログラミング実習U

第7回:GUI部品による入出力

目標

7.1 GUIとGUI部品

7.2 GUIのデザイン

7.3 GUI部品に動作をつける

import java.awt.event.*;
import javax.swing.*;

public class Sample72 extends JPanel {
	JTextField f1 = new JTextField(); //テキストフィールドの生成
	JButton b1 = new JButton("素数か素数でないか判定"); //ボタンの生成
	JLabel l1 = new JLabel("整数を入力してボタンを押す"); //ラベルの生成

	public Sample72() {
		setLayout(null); //部品の自動配置機能をOFFにする.
		add(f1); //テキストフィールドを配置
		f1.setBounds(50, 50, 120, 30); //テキストフィールドの位置と大きさを指定
		add(b1); //ボタンを配置
		b1.setBounds(50, 100, 250, 30); //ボタンの位置と大きさを指定
		add(l1); //ラベルを配置
		l1.setBounds(50, 150, 250, 30); //ラベルの位置と大きさを指定
		b1.addActionListener(new ActionListener() { //ボタンにイベントハンドラを付加
			public void actionPerformed(ActionEvent evt) {
				int n = Integer.parseInt(f1.getText()); //テキストフィールドから数値を取り込む
				boolean prime = true;
				for (int i = 2; i < n; ++i) { //素数かどうかの判定
					if (n % i == 0) {
						prime = false;
					}
				}
				l1.setText(n + (prime ? "は素数。" : "は合成数。")); //ラベルに結果を表示
				f1.setText(""); //テキストフィールドをクリア
			}
		});
	}

	public static void main(String[] args) {
		JFrame app = new JFrame();
		app.add(new Sample72());
		app.setSize(400, 300);
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.setVisible(true);
	}
}

7.4 例外処理

import java.awt.event.*;
import javax.swing.*;

public class Sample72b extends JPanel {
	JTextField f1 = new JTextField();
	JButton b1 = new JButton("素数か素数でないか判定");
	JLabel l1 = new JLabel("整数を入力してボタンを押す");

	public Sample72b() {
		setLayout(null);
		add(f1);
		f1.setBounds(50, 50, 120, 30);
		add(b1);
		b1.setBounds(50, 100, 250, 30);
		add(l1);
		l1.setBounds(50, 150, 250, 30);
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				try { //例外処理が発生する部分をtryでくくる
					int n = Integer.parseInt(f1.getText());
					boolean prime = true;
					for (int i = 2; i < n; ++i) {
						if (n % i == 0) {
							prime = false;
						}
					}
					l1.setText(n + (prime ? "は素数。" : "は合成数。"));
					f1.setText("");
				} catch (Exception ex) { //例外発生した時の処理
					l1.setText("問題がありました。再度どうぞ。");
				}
			}
		});
	}

	public static void main(String[] args) {
		JFrame app = new JFrame();
		app.add(new Sample72b());
		app.setSize(400, 300);
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.setVisible(true);
	}
}

補足:レイアウトマネージャ

import java.awt.*;
import javax.swing.*;

class ButtonSampleGrid3 extends JPanel {
	JPanel panel, panel1;
	Button pushButton[] = new Button[4];
	TextField textfield;

	public static void main(String[] args) {
		JFrame app = new JFrame();
		app.add(new ButtonSampleGrid3());
		app.setSize(240, 240);
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.setVisible(true);
	}

	ButtonSampleGrid3() {
		panel = new JPanel();
		panel.setLayout(new GridLayout(2, 2)); // レイアウトを2行2列へ変更
		for (int i = 0; i < 4; i++) {
			pushButton[i] = new Button("ボタン" + (i + 1));
			panel.add(pushButton[i]); // ボタンを貼り付け
		}
		textfield = new TextField(20);
		textfield.setText("テキストフィールド"); // テキストフィールドでのメッセージ表示
		textfield.setEditable(false); // テキストフィールドへの書き込み禁止
		panel1 = new JPanel();
		panel1.setLayout(new GridLayout(2, 1));
		panel1.add(textfield);
		panel1.add(panel); // panel1にpanelを配置する.
		add(panel1); // フレームにpanel1を配置する.
	}
}

2015.5.20
ykitamura@kwansei.ac.jp