?? dialogdemo.java
字號(hào):
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: 對(duì)話(huà)框演示</p> * <p>Description: 全面的演示各種類(lèi)型的對(duì)話(huà)框的使用</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 = "簡(jiǎn)單的信息提示對(duì)話(huà)窗"; String iconDesc = "帶有圖標(biāo)的對(duì)話(huà)窗"; String moreDialogDesc = "復(fù)雜信息對(duì)話(huà)窗"; CustomDialog customDialog;/** *<br>方法說(shuō)明:構(gòu)造器,生成一個(gè)面板添加到JFrame中 *<br>輸入?yún)?shù): *<br>返回類(lèi)型: */ public DialogDemo(JFrame frame) { super(new BorderLayout()); this.frame = frame; customDialog = new CustomDialog(frame, "tom", this); customDialog.pack(); //創(chuàng)建成員 JPanel frequentPanel = createSimpleDialogBox(); JPanel featurePanel = createFeatureDialogBox(); JPanel iconPanel = createIconDialogBox(); label = new JLabel("點(diǎn)擊\"顯示\" 按鈕" + " 顯示一個(gè)選擇的對(duì)話(huà)框", JLabel.CENTER); //放置對(duì)象 Border padding = BorderFactory.createEmptyBorder(20,20,5,20); frequentPanel.setBorder(padding); featurePanel.setBorder(padding); iconPanel.setBorder(padding); //創(chuàng)建選項(xiàng)卡 JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab("簡(jiǎn)單對(duì)話(huà)窗", null, frequentPanel, simpleDialogDesc); tabbedPane.addTab("復(fù)雜對(duì)話(huà)窗", null, featurePanel, moreDialogDesc); tabbedPane.addTab("圖標(biāo)對(duì)話(huà)窗", null, iconPanel, iconDesc); add(tabbedPane, BorderLayout.CENTER); add(label, BorderLayout.PAGE_END); label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); }/** *<br>方法說(shuō)明:設(shè)置按鈕上的文字 *<br>輸入?yún)?shù):String newText 添加的文字 *<br>返回類(lèi)型: */ void setLabel(String newText) { label.setText(newText); }/** *<br>方法說(shuō)明:獲取圖片 *<br>輸入?yún)?shù):String path 圖片完整路徑和名字 *<br>返回類(lèi)型:ImageIcon 圖片對(duì)象 */ 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>方法說(shuō)明:創(chuàng)建一個(gè)JPanel,給第一個(gè)選項(xiàng)卡 *<br>輸入?yún)?shù): *<br>返回類(lèi)型: */ 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"; //添加單選到數(shù)字 radioButtons[0] = new JRadioButton("只有“OK”按鈕"); radioButtons[0].setActionCommand(defaultMessageCommand); radioButtons[1] = new JRadioButton("有“Yes/No”二個(gè)按鈕"); radioButtons[1].setActionCommand(yesNoCommand); radioButtons[2] = new JRadioButton("有“Yes/No”兩個(gè)按鈕 " + "(程序添加文字)"); radioButtons[2].setActionCommand(yeahNahCommand); radioButtons[3] = new JRadioButton("有“Yes/No/Cancel”三個(gè)按鈕 " + "(程序添加文字)"); radioButtons[3].setActionCommand(yncCommand); //將四個(gè)單選組成一個(gè)群 for (int i = 0; i < numButtons; i++) { group.add(radioButtons[i]); } //設(shè)置第一個(gè)為默認(rèn)選擇 radioButtons[0].setSelected(true); //定義“顯示”按鈕 showItButton = new JButton("顯示"); //給“顯示”按鈕添加監(jiān)聽(tīng) showItButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String command = group.getSelection().getActionCommand(); //ok對(duì)話(huà)窗 if (command == defaultMessageCommand) { JOptionPane.showMessageDialog(frame, "雞蛋不可能是綠色的!"); //yes/no 對(duì)話(huà)窗 } else if (command == yesNoCommand) { int n = JOptionPane.showConfirmDialog( frame, "你喜歡吃酸菜魚(yú)嗎?", "一個(gè)很無(wú)聊的問(wèn)題!!", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) {//選擇yes setLabel("哇!我也是!"); } else if (n == JOptionPane.NO_OPTION) {//選擇no setLabel("唉!我喜歡吃!"); } else { setLabel("快告訴我吧!"); } //yes/no (自己輸入選項(xiàng)) } else if (command == yeahNahCommand) { Object[] options = {"是的", "不喜歡"}; int n = JOptionPane.showOptionDialog(frame, "你喜歡酸菜魚(yú)嗎?", "又一個(gè)無(wú)聊的問(wèn)題!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (n == JOptionPane.YES_OPTION) { setLabel("你哄人的吧,我也喜歡。"); } else if (n == JOptionPane.NO_OPTION) { setLabel("其實(shí)我也不喜歡!"); } else { setLabel("這都不肯告訴我,小氣鬼!"); } //yes/no/cancel 對(duì)話(huà)框 } else if (command == yncCommand) { Object[] options = {"是的,給我來(lái)一份。", "不,謝謝!", "不,我要水煮魚(yú)!"}; //構(gòu)造對(duì)話(huà)框 int n = JOptionPane.showOptionDialog(frame, "先生!我們這里有鮮美的酸菜魚(yú),您需要嗎?", "服務(wù)生的問(wèn)題。", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]); if (n == JOptionPane.YES_OPTION) { setLabel("你要的酸菜魚(yú)來(lái)了!"); } else if (n == JOptionPane.NO_OPTION) { setLabel("好的,你需要其它的。"); } else if (n == JOptionPane.CANCEL_OPTION) { setLabel("好的,我們給你做水煮魚(yú)!"); } else { setLabel("對(duì)不起!你還沒(méi)有點(diǎn)菜呢!"); } } return; } }); return createPane(simpleDialogDesc + ":", radioButtons, showItButton); }/** *<br>方法說(shuō)明:提供給createSimpleDialogBox和createFeatureDialogBox方法 *<br>方法說(shuō)明:創(chuàng)建帶提示信息、一列單選框和“顯示”按鈕 *<br>輸入?yún)?shù):String description 提示幫助信息 *<br>輸入?yún)?shù):JRadioButton[] radioButtons 單選框組 *<br>輸入?yún)?shù):JButton showButton “顯示”按鈕 *<br>返回類(lèi)型: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>方法說(shuō)明:提供給createSimpleDialogBox和createFeatureDialogBox方法 *<br>方法說(shuō)明:創(chuàng)建帶提示信息、二列單選框和“顯示”按鈕 *<br>輸入?yún)?shù):String description 提示幫助信息 *<br>輸入?yún)?shù):JRadioButton[] radioButtons 單選框組 *<br>輸入?yún)?shù):JButton showButton “顯示”按鈕 *<br>返回類(lèi)型: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>方法說(shuō)明:創(chuàng)建第三個(gè)選項(xiàng)卡的面板 *<br>方法說(shuō)明:這里都是實(shí)現(xiàn)showMessageDialog類(lèi),但是也可以指定圖標(biāo) *<br>輸入?yún)?shù): *<br>返回類(lèi)型:JPanel 構(gòu)造好的面板 */ 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("普通(沒(méi)有圖標(biāo))"); radioButtons[0].setActionCommand(plainCommand); radioButtons[1] = new JRadioButton("信息圖標(biāo)"); radioButtons[1].setActionCommand(infoCommand); radioButtons[2] = new JRadioButton("問(wèn)題圖標(biāo)");
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -