Windowを閉じるときにDialogを出す
戻る
::::::::::::::
JDialogTest.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.geom.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* $Id: Dialog.html,v 1.1 2009/06/22 16:11:41 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public class JDialogTest extends JFrame {
public JDialogTest () {
super( "JDialogのテスト" );
try {
// 外観を設定します
UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
// 外観を変更します
SwingUtilities.updateComponentTreeUI( this );
} catch ( Exception e ) {
e.printStackTrace();
System.exit( -1 );
}
setBounds( 0, 0, 640, 480 );
// パネルをコンテナに配置
JPanel panel = new JPanel();
Container container = getContentPane();
container.add( panel );
setVisible( true );
/**
* DO_NOTHING_ON_CLOSEを指定しないと、windowsClosing()メソッドが呼ばれたときに自動的にウィンドウが閉じてしまうので注意!
*/
setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );
// ウィンドウを閉じるボタンが選択されたときのアクションを定義する
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent ev ) {
Frame frame = ( Frame ) ev.getSource();
new MyDialog( frame );
}
}
);
}
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JDialogTest();
}
public static void main( String[] args ) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater( new Runnable() {
public void run() {
createAndShowGUI();
}
}
);
}
}
::::::::::::::
MyDialog.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.geom.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* $Id: Dialog.html,v 1.1 2009/06/22 16:11:41 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public class MyDialog extends JDialog {
private Button yesButton, noButton;
// コンストラクタ
public MyDialog( Frame host ) {
super( host, "ウィンドウを閉じますがよろしいですか?", true );
setBounds( 128, 256, 300, 60 );
Container cp = getContentPane();
cp.setLayout( new BoxLayout( cp, BoxLayout.LINE_AXIS ) );
Font f = new Font( "MS 明朝", Font.PLAIN, 10 );
yesButton = new Button( "はい" );
yesButton.setFont( f );
cp.add( yesButton );
noButton = new Button( "いいえ" );
noButton.setFont( f );
cp.add( noButton );
MyDialogAction action = new MyDialogAction( this );
yesButton.addActionListener( action );
yesButton.setActionCommand( "EXIT" );
noButton.addActionListener( action );
noButton.setActionCommand( "NOT_EXIT" );
setVisible( true );
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
setVisible( false );
}
}
);
}
}
::::::::::::::
MyDialogAction.java
::::::::::::::
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.geom.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* $Id: Dialog.html,v 1.1 2009/06/22 16:11:41 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public class MyDialogAction extends AbstractAction {
private JDialog dialog;
public MyDialogAction( JDialog dialog ) {
super();
this.dialog = dialog;
}
public void actionPerformed( ActionEvent event ) {
Component component = ( Component ) event.getSource();
String command = event.getActionCommand();
System.out.println( command );
if ( "EXIT".equals( command ) ) {
System.exit( 0 );
} else {
dialog.dispose();
}
}
}
戻る