看过了信息对话框,接着我们来看确认对话框到底怎样.Confirm Dialog目的在让用户对某个问题选择"Yes"或"No",可算是一种 相当简单的是非选择对话框.下面这个范例中,用户可选择不同按钮类型的确认对话框,为方便说明,在此我们将Message Type都默认 为JOptionPane.INFORMATION_MESSAGE.
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; import com.incors.plaf.alloy.*; import com.incors.plaf.alloy.themes.glass.*; public class ConfirmDialog implements ActionListener { JFrame f = null; JLabel label = null; public ConfirmDialog() { f = new JFrame("OptionPane Demo"); Container contentPane = f.getContentPane(); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(2, 2)); JButton b = new JButton("Show DEFAULT_OPTION"); b.addActionListener(this); panel.add(b); b = new JButton("Show YES_NO_OPTION"); b.addActionListener(this); panel.add(b); b = new JButton("Show YES_NO_CANCEL_OPTION"); b.addActionListener(this); panel.add(b); b = new JButton("Show OK_CANCEL_OPTION"); b.addActionListener(this); panel.add(b); label = new JLabel(" ", JLabel.CENTER); contentPane.add(label, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER); f.pack(); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { SwingUtil.setLookAndFeel(); new ConfirmDialog(); } public void actionPerformed(ActionEvent e) { // 处理用户按钮事件,默认的messageType是JoptionPane.INFORMATION_MESSAGE. String cmd = e.getActionCommand(); String title = "Confirm Dialog"; String message = ""; int messageType = JOptionPane.INFORMATION_MESSAGE; int optionType = JOptionPane.YES_NO_OPTION; if (cmd.equals("Show DEFAULT_OPTION")) { optionType = JOptionPane.DEFAULT_OPTION; message = "Show DEFAULT_OPTION Buttons"; } else if (cmd.equals("Show YES_NO_OPTION")) { optionType = JOptionPane.YES_NO_OPTION; message = "Show YES_NO_OPTION Buttons"; } else if (cmd.equals("Show YES_NO_CANCEL_OPTION")) { optionType = JOptionPane.YES_NO_CANCEL_OPTION; message = "Show YES_NO_CANCEL_OPTION Buttons"; } else if (cmd.equals("Show OK_CANCEL_OPTION")) { optionType = JOptionPane.OK_CANCEL_OPTION; message = "Show OK_CANCEL_OPTION Buttons"; } int result = JOptionPane.showConfirmDialog(f, message, title, optionType, messageType); if (result == JOptionPane.YES_OPTION) label.setText("您选择:Yes or OK"); if (result == JOptionPane.NO_OPTION) label.setText("您选择:No"); if (result == JOptionPane.CANCEL_OPTION) label.setText("您选择:Cancel"); if (result == JOptionPane.CLOSED_OPTION) label.setText("您没做任何选择,并关闭了对话框"); } } class SwingUtil { public static final void setLookAndFeel() { try { Font font = new Font("JFrame", Font.PLAIN, 12); Enumeration keys = UIManager.getLookAndFeelDefaults().keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); if (UIManager.get(key) instanceof Font) { UIManager.put(key, font); } } AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true"); AlloyTheme theme = new GlassTheme(); LookAndFeel alloyLnF = new AlloyLookAndFeel(theme); UIManager.setLookAndFeel(alloyLnF); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } } }