トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

JavaのJOptinoPane の代わり

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

abstract public class AbstractOptionDialog extends JDialog implements WindowListener, ActionListener {

 protected int result = JOptionPane.NO_OPTION;
 protected Component owner = null;
 protected KeyStroke enterPress = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
 protected KeyStroke enterRelease = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
 
 protected int getResult(){
   return result;
 }
 
 public AbstractOptionDialog(Frame owner, String title, String message){
   super(owner, true);
   this.owner = owner;
   result = JOptionPane.NO_OPTION;
   setTitle(title);
   initComponent(message);
 }
 
 public int showDialog(){
   setVisible(true);
   return result;
 }
 
 public void windowClosing(WindowEvent e){
   result = JOptionPane.NO_OPTION;
   dispose();
 }
 
 public void actionPerformed(ActionEvent e){
   try {
     result = Integer.parseInt(e.getActionCommand());
   } catch (Exception ex){
     result = JOptionPane.NO_OPTION;
   }
   dispose();
 }
 
 protected void initComponent(String message){
   setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
   addWindowListener(this);
   
   setResizable(false);
   
   JPanel mainPanel = createMainPanel();
   mainPanel.setLayout(new BorderLayout(15,0));
   
   // アイコンを追加
   mainPanel.add(createIcon(), BorderLayout.WEST);
   // メッセージ追加
   mainPanel.add(createMessagePanel(message), BorderLayout.CENTER);
   // ボタン追加
   mainPanel.add(createButtonPanel(), BorderLayout.SOUTH);
   
   pack();
   setLocationRelativeTo(owner);
 }
 
 abstract protected Component createIcon();
 
 abstract protected JPanel createMessagePanel(String message);
 
 abstract protected JPanel createButtonPanel();
 
 protected JPanel createMainPanel(){
   JPanel panel = new JPanel();
   
   Container c = getContentPane();
   GridBagLayout gridbag = new GridBagLayout();
   GridBagConstraints cons = new GridBagConstraints();
   c.setLayout(gridbag);
   
   cons.fill = GridBagConstraints.BOTH;
   cons.weightx = 1;
   cons.weighty = 1;
   cons.gridy = 0;
   cons.insets = new Insets(10,10,10,10);
   
   c.add(panel, cons);
   return panel;
 }
 public void windowActivated(WindowEvent e){}
 public void windowClosed(WindowEvent e) {}
 public void windowDeactivated(WindowEvent e){}
 public void windowDeiconified(WindowEvent e){}
 public void windowIconified(WindowEvent e){}
 public void windowOpened(WindowEvent e){}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class ConfirmDialog extends AbstractOptionDialog {

 public ConfirmDialog(Frame owner, String title, String message){
   super(owner, title, message);
 }
 
 @Override
 protected Component createIcon(){
   Icon icon = UIManager.getIcon("OptionPane.questionIcon");
   JLabel iconLabel = new JLabel(icon);
   iconLabel.setVerticalAlignment(SwingConstants.TOP);
   
   return iconLabel;
 }
 
 @Override
 protected JPanel createMessagePanel(String message){
   JPanel panel = new JPanel();
   
   try {
     GridBagLayout gridbag = new GridBagLayout();
     GridBagConstraints cons = new GridBagConstraints();
     panel.setLayout(gridbag);
     
     cons.fill = GridBagConstraints.HORIZONTAL;
     cons.weightx = 1;
     cons.weighty = 0;
     cons.gridy = 0;
     cons.insets = new Insets(0,0,3,0);
     
     BufferedReader br = new BufferedReader( new StringReader(message));
     String line = "";
     while( (line = br.readLine()) != null){
       panel.add(new JLabel(line), cons);
       cons.gridy++;
     }
   } catch (Exception e){
   }
   
   return panel;
 }
 
 @Override
 protected JPanel createButtonPanel(){
   JPanel panel = new JPanel();
   
   panel.setLayout(new FlowLayout());
   Dimension margin = new Dimension(40, 0);
   JPanel leftMargin = new JPanel();
   leftMargin.setPreferredSize(margin);
   JPanel rightMargin = new JPanel();
   rightMargin.setPreferredSize(margin);
   
   // 「はい」 ボタン
   JButton yesButton = new JButton(UIManager.getString("OptionPane.yesButtonText"));
   yesButton.setActionCommand(Integer.toString(JOptionPane.OK_OPTION));
   yesButton.getInputMap().put(enterPress, "pressed");
   yesButton.getInputMap().put(enterRelease, "released");
   yesButton.addActionListener(this);
   try {
     int mnemonic = Integer.parseInt((String)UIManager.get("OptionPane.yesButtonMnemonic"));
     yesButton.setMnemonic(mnemonic);
   } catch (Exception e){
   }
   
   // 「いいえ」ボタン
   JButton noButton = new JButton(UIManager.getString("OptionPane.noButtonText"));
   noButton.setActionCommand(Integer.toString(JOptionPane.NO_OPTION));
   noButton.getInputMap().put(enterPress, "pressed");
   noButton.getInputMap().put(enterRelease, "released");
   noButton.addActionListener(this);
   try {
     int mnemonic = Integer.parseInt((String)UIManager.get("OptionPane.noButtonMnemonic"));
     noButton.setMnemonic(mnemonic);
   } catch (Exception e){
   }
   
   
   panel.add(leftMargin);
   panel.add(yesButton);
   panel.add(noButton);
   panel.add(rightMargin);
   
   return panel;
 }
 
 public static int showDialog(Frame owner, String title, String message){
   ConfirmDialog dialog = new ConfirmDialog(owner, title, message);
   return dialog.showDialog();
 }

}

[カテゴリ: プログラミング言語 > Java]

[通知用URL]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2011年02月19日 19時38分28秒