Src006 modal


/**

* DialogFrame?.java

* TECHSCORE Javaユーザインタフェース11章 実習課題1

*

* Copyright (c) 2004 Four-Dimensional Data, Inc.

*/

package test3;

import java.awt.BorderLayout?;

import java.awt.Frame;

import java.awt.event.ActionEvent?;

import java.awt.event.ActionListener?;

import javax.swing.DefaultListModel?;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JList;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem?;

import javax.swing.JScrollPane?;

import javax.swing.JTextField?;

public class DialogFrame? extends JFrame implements ActionListener? {

private JMenuItem? addMenuItem?;

private JList list;

private DefaultListModel? listModel;

public DialogFrame?() {

super("DialogFrame?");

setDefaultCloseOperation?(EXIT_ON_CLOSE);

JMenuBar menuBar = new JMenuBar();

setJMenuBar(menuBar);

JMenu addMenu = new JMenu("edit");

menuBar.add(addMenu);

String listItem[] =

{ "Sapporo", "Sendai", "Chiba", "Tokyo", "Shizuoka", "Nagoya", "Osaka", "Okayama", "Hiroshima", "Fukuoka" };

listModel = new DefaultListModel?();

for (int i = 0; i < listItem.length; i++) {

listModel.addElement(listItem[i]);

}

list = new JList(listModel);

getContentPane?().add(new JScrollPane?(list));

addMenuItem? = new JMenuItem?("add");

addMenuItem?.addActionListener?(this);

addMenu.add(addMenuItem?);

pack();

}

public void actionPerformed(ActionEvent? event) {

// AddCityDialog? dialog = new AddCityDialog?(this);

AddCityDialog2 dialog = new AddCityDialog2(this);

dialog.show();

if (dialog.isButtonPressed?()) {

listModel.addElement(dialog.getInputCity?());

}

}

public static void main(String args[]) {

new DialogFrame?().setVisible(true);

}

}

				

KKKKKKKKKKKKKKKKKKKK

package test3;

import java.awt.BorderLayout?;

import java.awt.Dimension;

import java.awt.Frame;

import java.awt.event.ActionEvent?;

import java.awt.event.ActionListener?;

import javax.swing.DefaultListModel?;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JList;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem?;

import javax.swing.JPanel;

import javax.swing.JScrollPane?;

import javax.swing.JTable;

import javax.swing.JTextField?;

public class AddCityDialog2 extends JDialog implements ActionListener? {

private JTextField? cityField;

private boolean buttonPressed;

private String[][] tabledata = { { "日本", "3勝", "0敗", "1分" }, {

クロアチア, "3勝", "1敗", "0分" }, {

ブラジル, "1勝", "2敗", "1分" }, {

オーストラリア, "2勝", "2敗", "0分" }

};

private String[] columnNames = { "COUNTRY", "WIN", "LOST", "EVEN" };

public AddCityDialog2(Frame owner) {

super(owner, "add a city", true);

cityField = new JTextField?();

getContentPane?().add(cityField);

JButton addButton = new JButton("addssssssssss");

addButton.addActionListener?(this);

//

JTable table = new JTable(tabledata, columnNames);

JScrollPane? sp = new JScrollPane?(table);

sp.setPreferredSize?(new Dimension(250, 70));

JPanel p = new JPanel();

p.add(sp);

// getContentPane?().add(p, BorderLayout?.SOUTH);

// どっちでも出る

// getContentPane?().add(sp);

getContentPane?().add(p);

//

getContentPane?().add(addButton, BorderLayout?.SOUTH);

//

//以下こうやるといいらしい

// Dimension dm; // Dimension の宣言

// dm = new Dimension(300, 200); // Dimension を横300px、たて200pxで生成。

// f.setPreferredSize?(dm); // フレームの好ましいサイズを指定。

// f.pack();

// f.setVisible(true);

//

// サブコンポーネントの推奨サイズおよびレイアウトに合わせて

// この Window をサイズ変更するように設定

pack();

}

public boolean isButtonPressed?() {

return (buttonPressed);

}

public String getInputCity?() {

return (cityField.getText());

}

public void actionPerformed(ActionEvent? e) {

if (!cityField.getText().equals("")) {

buttonPressed = true;

}

this.hide();

}

}

LLLLLLLLLLL

package test3;

import java.awt.BorderLayout?;

import java.awt.Frame;

import java.awt.event.ActionEvent?;

import java.awt.event.ActionListener?;

import javax.swing.DefaultListModel?;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JList;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem?;

import javax.swing.JScrollPane?;

import javax.swing.JTextField?;

public class AddCityDialog? extends JDialog implements ActionListener? {

private JTextField? cityField;

private boolean buttonPressed;

public AddCityDialog?(Frame owner) {

super(owner, "add a city", true);

cityField = new JTextField?();

getContentPane?().add(cityField);

JButton addButton = new JButton("add");

addButton.addActionListener?(this);

getContentPane?().add(addButton, BorderLayout?.SOUTH);

pack();

}

public boolean isButtonPressed?() {

return (buttonPressed);

}

public String getInputCity?() {

return (cityField.getText());

}

public void actionPerformed(ActionEvent? e) {

if (!cityField.getText().equals("")) {

buttonPressed = true;

}

this.hide();

}

}