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

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

AWTの課題と同じ作業を行なうものについては、解答を省略する。

課題6-1 〜 6-4

省略

課題6-5

AWTの場合、WindowsとLinuxで外観が異なる(例:背景色が違う。ボタンが違う)が、 Swingの場合、同じ外観となる。

課題6-6

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

class MyFrame extends JFrame implements ActionListener {
    JButton b1;
    JTextField t;
    public MyFrame() {
	Container contentPane = getContentPane();
	setSize(300,200);
        addWindowListener(new MyWindowAdapter());
	contentPane.setLayout(new FlowLayout());
	t = new JTextField("Initial Value");
	contentPane.add(t);
	b1 = new JButton("Push");
	contentPane.add(b1);
	b1.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae) {
	// TextFieldの現在の値を標準出力に表示する
	System.out.println(t.getText());
    }
}

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

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

課題6-7

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

class MyFrame extends JFrame implements ActionListener {
    JButton b1;
    JCheckBox c1, c2, c3;
    public MyFrame() {
	Container contentPane = getContentPane();
	setSize(300,200);
        addWindowListener(new MyWindowAdapter());
	contentPane.setLayout(new FlowLayout());
	c1 = new JCheckBox("1");
	contentPane.add(c1);
	c2 = new JCheckBox("2");
	contentPane.add(c2);
	c3 = new JCheckBox("3");
	contentPane.add(c3);
	b1 = new JButton("Push");
	contentPane.add(b1);
	b1.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae) {
	// Checkboxの現在の値を標準出力に表示する
	System.out.println("checkbox1 is " + c1.isSelected());
	System.out.println("checkbox2 is " + c2.isSelected());
	System.out.println("checkbox3 is " + c3.isSelected());
    }
}

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);
    }
}

課題6-8

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

class MyFrame extends JFrame implements ActionListener {
    JButton b1;
    JTextArea t;
    public MyFrame() {
	Container contentPane = getContentPane();
	setSize(300,200);
        addWindowListener(new MyWindowAdapter());
	contentPane.setLayout(new FlowLayout());
	t = new JTextArea("Initial Value", 6, 20);
	contentPane.add(t);
	b1 = new JButton("Push");
	contentPane.add(b1);
	b1.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae) {
	// TextFieldの現在の値を標準出力に表示する
	System.out.println(t.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月4日〜2004年7月4日)