//************************************************************* // Java application // 「DialogTest」 // //          作 成 者:ルート高菜    //          作成開始月:2006/5 //          最終更新月:2006/6 [TN077/J67] //************************************************************* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DialogTest extends JFrame { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; // public static void main(String[] args) { DialogTest frame=new DialogTest(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //右上×印で終了 frame.setVisible(true); } // FileDialog flDialog=new FileDialog(this); String dirName=""; String fileName=""; // JPanel panel=new JPanel(); JLabel labelMessage=new JLabel("メッセージ:ダイアログのテスト"); JLabel labelIcon=new JLabel("アイコン:QUESTION_MESSAGE"); String message="ダイアログのテスト"; int icon=JOptionPane.QUESTION_MESSAGE; // JMenuBar mainMenu=new JMenuBar(); // JMenu file=new JMenu("ファイル"); JMenuItem open=new JMenuItem("開く"); JMenuItem save=new JMenuItem("保存"); JMenuItem exit=new JMenuItem("終了"); // JMenu option=new JMenu("サブメニュー"); JMenuItem optionDefault=new JMenuItem("DEFAULT"); JMenuItem optionYesNo=new JMenuItem("YES_NO"); JMenuItem optionYesNoCancel=new JMenuItem("YES_NO_CANCEL"); JMenuItem optionOkCancel=new JMenuItem("OK_CANCEL"); JMenu dialog=new JMenu("ダイアログ"); JMenuItem textInput=new JMenuItem("文字入力"); JMenuItem listBox=new JMenuItem("リストボックス"); JMenuItem colorDialog=new JMenuItem("色選択"); // JMenu help=new JMenu("ヘルプ"); JMenuItem version=new JMenuItem("バージョン"); //コンストラクタ public DialogTest() { // setTitle("DialogTest"); setSize(300,300); // panel.setBackground(Color.WHITE); panel.add(labelMessage); panel.add(labelIcon); //コンテナ枠 Container cp=getContentPane(); cp.add(panel); // file.add(open); file.add(save); file.addSeparator(); file.add(exit); option.add(optionDefault); option.add(optionYesNo); option.add(optionYesNoCancel); option.add(optionOkCancel); dialog.add(textInput); dialog.add(listBox); dialog.addSeparator(); dialog.add(option); dialog.addSeparator(); dialog.add(colorDialog); help.add(version); // setJMenuBar(mainMenu); mainMenu.add(file); mainMenu.add(dialog); mainMenu.add(help); //メニューイベント処理 MenuAction menuAction=new MenuAction(); // open.addActionListener(menuAction); save.addActionListener(menuAction); exit.addActionListener(menuAction); optionDefault.addActionListener(menuAction); optionYesNo.addActionListener(menuAction); optionYesNoCancel.addActionListener(menuAction); optionOkCancel.addActionListener(menuAction); textInput.addActionListener(menuAction); listBox.addActionListener(menuAction); colorDialog.addActionListener(menuAction); version.addActionListener(menuAction); // } //イベント処理用の内部クラス(アクションリスナーを定義) class MenuAction implements ActionListener { public void actionPerformed(ActionEvent event){ Object object=event.getSource(); if(object==open){ flDialog.setMode(FileDialog.LOAD); flDialog.setTitle("開く"); flDialog.setDirectory(dirName); flDialog.setFile(fileName); flDialog.setVisible(true); dirName=flDialog.getDirectory(); fileName=flDialog.getFile(); DialogTest.this.setTitle(dirName+fileName); }else if(object==save){ flDialog.setMode(FileDialog.SAVE); flDialog.setTitle("保存"); flDialog.setDirectory(dirName); flDialog.setFile(fileName); flDialog.setVisible(true); dirName=flDialog.getDirectory(); fileName=flDialog.getFile(); DialogTest.this.setTitle(dirName+fileName); }else if(object==exit){ int ret=JOptionPane.showConfirmDialog(DialogTest.this,"終了しますか","確認",JOptionPane.YES_NO_OPTION); if(ret==JOptionPane.YES_OPTION){ System.exit(0); } }else if(object==optionDefault){ /*int ret=*/JOptionPane.showConfirmDialog(DialogTest.this,message,"確認",JOptionPane.DEFAULT_OPTION,icon); }else if(object==optionYesNo){ /*int ret=*/JOptionPane.showConfirmDialog(DialogTest.this,message,"確認",JOptionPane.YES_NO_OPTION,icon); }else if(object==optionYesNoCancel){ /*int ret=*/JOptionPane.showConfirmDialog(DialogTest.this,message,"確認",JOptionPane.YES_NO_CANCEL_OPTION,icon); }else if(object==optionOkCancel){ /*int ret=*/JOptionPane.showConfirmDialog(DialogTest.this,message,"確認",JOptionPane.OK_CANCEL_OPTION,icon); }else if(object==textInput){ String ret=JOptionPane.showInputDialog(DialogTest.this,"メッセージを入力してください"); if(ret!=null){ message=ret; labelMessage.setText("メッセージ:"+ret); } }else if(object==listBox){ String[] list={"ERROR","INFORMATION","WARNING","QUESTION","PLAIN"}; Object ret=JOptionPane.showInputDialog(DialogTest.this,"アイコンを選択してください","アイコン選択",JOptionPane.INFORMATION_MESSAGE,null,list,list[3]); if(ret==null){ }else if(ret.equals(list[0])){ icon=JOptionPane.ERROR_MESSAGE; labelIcon.setText("アイコン:ERROR_MESSAGE"); }else if(ret.equals(list[1])){ icon=JOptionPane.INFORMATION_MESSAGE; labelIcon.setText("アイコン:INFORMATION_MESSAGE"); }else if(ret.equals(list[2])){ icon=JOptionPane.WARNING_MESSAGE; labelIcon.setText("アイコン:WARNING_MESSAGE"); }else if(ret.equals(list[3])){ icon=JOptionPane.QUESTION_MESSAGE; labelIcon.setText("アイコン:QUESTION_MESSAGE"); }else if(ret.equals(list[4])){ icon=JOptionPane.PLAIN_MESSAGE; labelIcon.setText("アイコン:PLAIN_MESSAGE"); } }else if(object==colorDialog){ Color cc=JColorChooser.showDialog(null,"背景色を選択",panel.getBackground()); panel.setBackground(cc); }else if(object==version){ JOptionPane.showMessageDialog(DialogTest.this,"This is Dialog Test Ver.1","Version",JOptionPane.INFORMATION_MESSAGE); } } } }