教育サーバーのページ
ソフトウエア基礎演習

GUIアプリケーション (AWT) 模範解答

課題 4-1

実行すると中身が空のウインドウが現われる。ウインドウの終了ボタン(×ボタン) を押しても終了しない。Ctrl-Cなどで強制終了する必要がある。

課題 4-2

setSize(300,200); とある行を setSize(480,300); に変更すればウインドウサイズが変わる。

課題 4-3

setTitle("My Window Application"); とある行を setTitle("New Window Application"); に変更する。

課題4-4

Component --+-- Button
            |
            +-- Container ----- Window ----- Frame
            |
            +-- TextComponent ----- TextArea

課題 4-5

b1 = new Button("Button1"); とある行を b1 = new Button("ボタン"); に変更する。

課題 4-6

省略

課題 4-7

System.out.println で表示しているメッセージを英語に直せばよい

課題 4-8

import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame implements ActionListener {
    Button b1;
    public MyFrame() {
	setTitle("My Button Application");
	setSize(300, 200);
	setLayout(new FlowLayout());
	b1 = new Button("Button1");
	b1.addActionListener(this);
	add(b1);
	addWindowListener(new MyWindowAdapter());
    }

    public void actionPerformed(ActionEvent ae) {
	b1.setLabel("Pressed");
    }
}

class MyWindowAdapter extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
	System.exit(0);
    }
}

public class MyButtonTest {
    public static void main(String[] args) {
	MyFrame f = new MyFrame();
	//       f.show();
	f.setVisible(true);
    }
}

課題 4-9

actionPerformedメソッドを以下のように書き変える。
    public void actionPerformed(ActionEvent ae) {
        if (b1.getLabel().equals("Black")) {
            b1.setLabel("White");
        } else {
            b1.setLabel("Black");
        }
    }

課題 4-10

import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame implements ActionListener {
    Button b1;
    TextField t1, t2;
    public MyFrame() {
	setSize(300,200);
	setLayout(new FlowLayout());
	t1 = new TextField("Initial Value");
	t2 = new TextField("Initial Value");
	add(t1);
	add(t2);
	b1 = new Button("Push");
	add(b1);
	b1.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae) {
	if (t1.getText().equals(t2.getText())) {
	    System.out.println("Equal");
	} else {
	    System.out.println("Not Equal");
	}
    }
}

public class MyTextFieldTest {
    public static void main(String[] args) {
	MyFrame f = new MyFrame();
	f.setVisible(true);
    }
}

課題 4-11

import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame implements ActionListener {
    Button b1;
    Checkbox c1, c2, c3;
    public MyFrame() {
	setSize(300,200);
        addWindowListener(new MyWindowAdapter());
	setLayout(new FlowLayout());
	c1 = new Checkbox("RED");
	add(c1);
	c2 = new Checkbox("GREEN");
	add(c2);
	c3 = new Checkbox("BLUE");
	add(c3);
	b1 = new Button("Push");
	add(b1);
	b1.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae) {
	// Checkboxの現在の値を標準出力に表示する
	if (c1.getState() && c2.getState() && c3.getState()) {
	    System.out.println("White color");
	}
    }
}

class MyWindowAdapter extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
	System.exit(0);
    }
}

public class MyCheckboxTest {
    public static void main(String[] args) {
	MyFrame f = new MyFrame();
	f.setVisible(true);
    }
}

課題 4-12

import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame implements ActionListener {
    Button b1;
    TextArea t1,t2;
    public MyFrame() {
	setSize(400,200);
        addWindowListener(new MyWindowAdapter());
	setLayout(new FlowLayout());
	t1 = new TextArea("Initial Value", 6, 20);
	t2 = new TextArea("Initial Value", 6, 20);
	add(t1);
	add(t2);
	b1 = new Button("Push");
	add(b1);
	b1.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae) {
	// TextAreaの現在の値を標準出力に表示する
	System.out.println(t1.getText());
	System.out.println(t2.getText());
    }
}

class MyWindowAdapter extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
	System.exit(0);
    }
}

public class MyTextAreaTest {
    public static void main(String[] args) {
	MyFrame f = new MyFrame();
	f.setVisible(true);
    }
}

ソフトウエア基礎演習


ohmi@rsch.tuis.ac.jp (2004年7月5日〜2004年7月5日)