解答例 - 実習課題2 - 16.内部フレームとデスクトップペイン
(実習課題2)
実習課題1のプログラムを改良しなさい。
- 内部フレームを作成するときに、「サイズ変更可能性」「クローズ可能性」「最大化可能性」「アイコン化可能性」の4プロパティを指定できるようにする事
解答例
/**
* SetPropertyMdi.java
* TECHSCORE Javaユーザインタフェース16章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.ui.chapter16.exercise2;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class SetPropertyMdi extends JFrame implements ActionListener {
private JMenuItem addMenu;
private JDesktopPane desktopPane;
private int numOfInternal;
public SetPropertyMdi() {
super("SetPropertyMdi");
setDefaultCloseOperation(EXIT_ON_CLOSE);
//メニュー
JMenu menu = new JMenu("File");
addMenu = new JMenuItem("add");
addMenu.addActionListener(this);
menu.add(addMenu);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
setJMenuBar(menuBar);
//JDesktopPane
desktopPane = new JDesktopPane();
desktopPane.setPreferredSize(new Dimension(600, 400));
getContentPane().add(desktopPane);
pack();
numOfInternal = 0;
}
public void actionPerformed(ActionEvent event) {
new PropertyDialog(this).setVisible(true);
}
private class PropertyDialog extends JDialog implements ActionListener {
private JCheckBox[] checkBoxes;
private JButton okButton;
private JFrame rootFrame;
public PropertyDialog(JFrame rootFrame) {
super(rootFrame, "Input Property", true);
this.rootFrame = rootFrame;
Box box = new Box(BoxLayout.Y_AXIS);
checkBoxes = new JCheckBox[4];
checkBoxes[0] = new JCheckBox("resizable");
checkBoxes[1] = new JCheckBox("closable");
checkBoxes[2] = new JCheckBox("maximizable");
checkBoxes[3] = new JCheckBox("iconfiable");
for (int i = 0; i < checkBoxes.length; i++) {
box.add(checkBoxes[i]);
}
okButton = new JButton("OK");
okButton.addActionListener(this);
box.add(okButton);
getContentPane().add(box);
pack();
}
public void actionPerformed(ActionEvent event) {
JInternalFrame internal =
new JInternalFrame(
"internal frame",
checkBoxes[0].isSelected(),
checkBoxes[1].isSelected(),
checkBoxes[2].isSelected(),
checkBoxes[3].isSelected());
desktopPane.add(internal);
internal.reshape(numOfInternal * 10, numOfInternal * 10, 200, 200);
internal.setVisible(true);
numOfInternal++;
if (numOfInternal > 20) {
numOfInternal = 0;
}
dispose();
}
}
public static void main(String args[]) {
new SetPropertyMdi().setVisible(true);
}
}

