?? dialogdemo.java
字號:
import javax.swing.JOptionPane;import javax.swing.JDialog;import javax.swing.JButton;import javax.swing.JRadioButton;import javax.swing.ButtonGroup;import javax.swing.JLabel;import javax.swing.ImageIcon;import javax.swing.BoxLayout;import javax.swing.Box;import javax.swing.BorderFactory;import javax.swing.border.Border;import javax.swing.JTabbedPane;import javax.swing.JPanel;import javax.swing.JFrame;import java.beans.*; import java.awt.*;import java.awt.event.*;/** * <p>Title: 對話框演示</p> * <p>Description: 全面的演示各種類型的對話框的使用</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Filename: DialogDemo.java</p> * @author 杜江 * @version 1.0 */public class DialogDemo extends JPanel { JLabel label; ImageIcon icon = createImageIcon("images/middle.gif"); JFrame frame; String simpleDialogDesc = "簡單的信息提示對話窗"; String iconDesc = "帶有圖標的對話窗"; String moreDialogDesc = "復雜信息對話窗"; CustomDialog customDialog;/** *<br>方法說明:構造器,生成一個面板添加到JFrame中 *<br>輸入參數: *<br>返回類型: */ public DialogDemo(JFrame frame) { super(new BorderLayout()); this.frame = frame; customDialog = new CustomDialog(frame, "tom", this); customDialog.pack(); //創建成員 JPanel frequentPanel = createSimpleDialogBox(); JPanel featurePanel = createFeatureDialogBox(); JPanel iconPanel = createIconDialogBox(); label = new JLabel("點擊\"顯示\" 按鈕" + " 顯示一個選擇的對話框", JLabel.CENTER); //放置對象 Border padding = BorderFactory.createEmptyBorder(20,20,5,20); frequentPanel.setBorder(padding); featurePanel.setBorder(padding); iconPanel.setBorder(padding); //創建選項卡 JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab("簡單對話窗", null, frequentPanel, simpleDialogDesc); tabbedPane.addTab("復雜對話窗", null, featurePanel, moreDialogDesc); tabbedPane.addTab("圖標對話窗", null, iconPanel, iconDesc); add(tabbedPane, BorderLayout.CENTER); add(label, BorderLayout.PAGE_END); label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); }/** *<br>方法說明:設置按鈕上的文字 *<br>輸入參數:String newText 添加的文字 *<br>返回類型: */ void setLabel(String newText) { label.setText(newText); }/** *<br>方法說明:獲取圖片 *<br>輸入參數:String path 圖片完整路徑和名字 *<br>返回類型:ImageIcon 圖片對象 */ protected static ImageIcon createImageIcon(String path) { java.net.URL imgURL = DialogDemo.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } }/** *<br>方法說明:創建一個JPanel,給第一個選項卡 *<br>輸入參數: *<br>返回類型: */ private JPanel createSimpleDialogBox() { final int numButtons = 4; JRadioButton[] radioButtons = new JRadioButton[numButtons]; final ButtonGroup group = new ButtonGroup(); JButton showItButton = null; final String defaultMessageCommand = "default"; final String yesNoCommand = "yesno"; final String yeahNahCommand = "yeahnah"; final String yncCommand = "ync"; //添加單選到數字 radioButtons[0] = new JRadioButton("只有“OK”按鈕"); radioButtons[0].setActionCommand(defaultMessageCommand); radioButtons[1] = new JRadioButton("有“Yes/No”二個按鈕"); radioButtons[1].setActionCommand(yesNoCommand); radioButtons[2] = new JRadioButton("有“Yes/No”兩個按鈕 " + "(程序添加文字)"); radioButtons[2].setActionCommand(yeahNahCommand); radioButtons[3] = new JRadioButton("有“Yes/No/Cancel”三個按鈕 " + "(程序添加文字)"); radioButtons[3].setActionCommand(yncCommand); //將四個單選組成一個群 for (int i = 0; i < numButtons; i++) { group.add(radioButtons[i]); } //設置第一個為默認選擇 radioButtons[0].setSelected(true); //定義“顯示”按鈕 showItButton = new JButton("顯示"); //給“顯示”按鈕添加監聽 showItButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String command = group.getSelection().getActionCommand(); //ok對話窗 if (command == defaultMessageCommand) { JOptionPane.showMessageDialog(frame, "雞蛋不可能是綠色的!"); //yes/no 對話窗 } else if (command == yesNoCommand) { int n = JOptionPane.showConfirmDialog( frame, "你喜歡吃酸菜魚嗎?", "一個很無聊的問題!!", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) {//選擇yes setLabel("哇!我也是!"); } else if (n == JOptionPane.NO_OPTION) {//選擇no setLabel("唉!我喜歡吃!"); } else { setLabel("快告訴我吧!"); } //yes/no (自己輸入選項) } else if (command == yeahNahCommand) { Object[] options = {"是的", "不喜歡"}; int n = JOptionPane.showOptionDialog(frame, "你喜歡酸菜魚嗎?", "又一個無聊的問題!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (n == JOptionPane.YES_OPTION) { setLabel("你哄人的吧,我也喜歡。"); } else if (n == JOptionPane.NO_OPTION) { setLabel("其實我也不喜歡!"); } else { setLabel("這都不肯告訴我,小氣鬼!"); } //yes/no/cancel 對話框 } else if (command == yncCommand) { Object[] options = {"是的,給我來一份。", "不,謝謝!", "不,我要水煮魚!"}; //構造對話框 int n = JOptionPane.showOptionDialog(frame, "先生!我們這里有鮮美的酸菜魚,您需要嗎?", "服務生的問題。", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]); if (n == JOptionPane.YES_OPTION) { setLabel("你要的酸菜魚來了!"); } else if (n == JOptionPane.NO_OPTION) { setLabel("好的,你需要其它的。"); } else if (n == JOptionPane.CANCEL_OPTION) { setLabel("好的,我們給你做水煮魚!"); } else { setLabel("對不起!你還沒有點菜呢!"); } } return; } }); return createPane(simpleDialogDesc + ":", radioButtons, showItButton); }/** *<br>方法說明:提供給createSimpleDialogBox和createFeatureDialogBox方法 *<br>方法說明:創建帶提示信息、一列單選框和“顯示”按鈕 *<br>輸入參數:String description 提示幫助信息 *<br>輸入參數:JRadioButton[] radioButtons 單選框組 *<br>輸入參數:JButton showButton “顯示”按鈕 *<br>返回類型:JPanel 添加好的面板 */ private JPanel createPane(String description, JRadioButton[] radioButtons, JButton showButton) { int numChoices = radioButtons.length; JPanel box = new JPanel(); JLabel label = new JLabel(description); box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS)); box.add(label); //添加radio for (int i = 0; i < numChoices; i++) { box.add(radioButtons[i]); } JPanel pane = new JPanel(new BorderLayout()); pane.add(box, BorderLayout.PAGE_START); pane.add(showButton, BorderLayout.PAGE_END); return pane; }/** *<br>方法說明:提供給createSimpleDialogBox和createFeatureDialogBox方法 *<br>方法說明:創建帶提示信息、二列單選框和“顯示”按鈕 *<br>輸入參數:String description 提示幫助信息 *<br>輸入參數:JRadioButton[] radioButtons 單選框組 *<br>輸入參數:JButton showButton “顯示”按鈕 *<br>返回類型:JPanel 添加好的面板 */ private JPanel create2ColPane(String description, JRadioButton[] radioButtons, JButton showButton) { JLabel label = new JLabel(description); int numPerColumn = radioButtons.length/2; JPanel grid = new JPanel(new GridLayout(0, 2)); for (int i = 0; i < numPerColumn; i++) { grid.add(radioButtons[i]); grid.add(radioButtons[i + numPerColumn]); } JPanel box = new JPanel(); box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS)); box.add(label); grid.setAlignmentX(0.0f); box.add(grid); JPanel pane = new JPanel(new BorderLayout()); pane.add(box, BorderLayout.PAGE_START); pane.add(showButton, BorderLayout.PAGE_END); return pane; }/** *<br>方法說明:創建第三個選項卡的面板 *<br>方法說明:這里都是實現showMessageDialog類,但是也可以指定圖標 *<br>輸入參數: *<br>返回類型:JPanel 構造好的面板 */ private JPanel createIconDialogBox() { JButton showItButton = null; final int numButtons = 6; JRadioButton[] radioButtons = new JRadioButton[numButtons]; final ButtonGroup group = new ButtonGroup(); final String plainCommand = "plain"; final String infoCommand = "info"; final String questionCommand = "question"; final String errorCommand = "error"; final String warningCommand = "warning"; final String customCommand = "custom"; radioButtons[0] = new JRadioButton("普通(沒有圖標)"); radioButtons[0].setActionCommand(plainCommand); radioButtons[1] = new JRadioButton("信息圖標"); radioButtons[1].setActionCommand(infoCommand); radioButtons[2] = new JRadioButton("問題圖標");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -