{{code Java, 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){} } }} {{code Java, 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(); } } }} {{category2 プログラミング言語,Java}}