?? dialogdemo.java
字號:
radioButtons[2].setActionCommand(questionCommand); radioButtons[3] = new JRadioButton("錯誤圖標"); radioButtons[3].setActionCommand(errorCommand); radioButtons[4] = new JRadioButton("警告圖標"); radioButtons[4].setActionCommand(warningCommand); radioButtons[5] = new JRadioButton("自定義圖標"); radioButtons[5].setActionCommand(customCommand); 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(); //沒有圖標 if (command == plainCommand) { JOptionPane.showMessageDialog(frame, "水煮魚里不要放酸菜!", "無圖標", JOptionPane.PLAIN_MESSAGE); //信息圖標 } else if (command == infoCommand) { JOptionPane.showMessageDialog(frame, "水煮魚里不要放酸菜!", "信息圖標", JOptionPane.INFORMATION_MESSAGE); //問題圖標 } else if (command == questionCommand) { JOptionPane.showMessageDialog(frame, "請你吃飯前洗手,好嗎?", "問題", JOptionPane.QUESTION_MESSAGE); //錯誤圖標 } else if (command == errorCommand) { JOptionPane.showMessageDialog(frame, "對不起,你的信用卡沒有資金了!", "錯誤信息", JOptionPane.ERROR_MESSAGE); //警告圖標 } else if (command == warningCommand) { JOptionPane.showMessageDialog(frame, "警告!你嚴重透支信用卡,請盡快補齊金額!", "警告信息", JOptionPane.WARNING_MESSAGE); //自定義圖標 } else if (command == customCommand) { JOptionPane.showMessageDialog(frame, "哈哈。我想用什么圖標都可以!", "自定義對話窗", JOptionPane.INFORMATION_MESSAGE, icon); } } }); return create2ColPane(iconDesc + ":", radioButtons, showItButton); }/** *<br>方法說明:創建一個JPanel,放在第二個選項卡上 *<br>輸入參數: *<br>返回類型: */ private JPanel createFeatureDialogBox() { final int numButtons = 5; JRadioButton[] radioButtons = new JRadioButton[numButtons]; final ButtonGroup group = new ButtonGroup(); JButton showItButton = null; //定義操作命令 final String pickOneCommand = "pickone"; final String textEnteredCommand = "textfield"; final String nonAutoCommand = "nonautooption"; final String customOptionCommand = "customoption"; final String nonModalCommand = "nonmodal"; //定義radio數組 radioButtons[0] = new JRadioButton("選擇一個"); radioButtons[0].setActionCommand(pickOneCommand); radioButtons[1] = new JRadioButton("輸入信息"); radioButtons[1].setActionCommand(textEnteredCommand); radioButtons[2] = new JRadioButton("關閉按鈕無效"); radioButtons[2].setActionCommand(nonAutoCommand); radioButtons[3] = new JRadioButton("輸入校驗" + "(用戶輸入信息)"); radioButtons[3].setActionCommand(customOptionCommand); radioButtons[4] = new JRadioButton("沒有模式"); radioButtons[4].setActionCommand(nonModalCommand); //合成一個組群 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(); //選擇一個 if (command == pickOneCommand) { Object[] possibilities = {"辣椒", "西紅柿", "洋蔥"}; //設置對話框 String s = (String)JOptionPane.showInputDialog( frame, //所屬窗體 "請選擇項目:\n" + "\"雞蛋炒\"", //輸出信息 "客戶選擇", JOptionPane.PLAIN_MESSAGE, //對話框模式 icon, //顯示圖標 possibilities, //選項內容 "辣椒"); //默認選項 //如果有選擇 if ((s != null) && (s.length() > 0)) { setLabel("雞蛋炒" + s + "!"); return; } //如果客戶沒有選擇 setLabel("快點!"); //文本輸入 } else if (command == textEnteredCommand) { String s = (String)JOptionPane.showInputDialog( frame, "選擇一個配料\n" + "\"雞蛋炒\"", "客戶輸入", JOptionPane.PLAIN_MESSAGE, icon, null, "辣椒"); //如果用戶有輸入 if ((s != null) && (s.length() > 0)) { setLabel("你要的是雞蛋炒" + s + "!"); return; } //如果返回的是空或者是null。 setLabel("快些選擇!"); //關閉按鈕無效 } else if (command == nonAutoCommand) { //構造一個對話框面板 final JOptionPane optionPane = new JOptionPane( "關閉這個對話框\n" + "請點擊下面的按鈕\n" + "明白嗎?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); JDialog.setDefaultLookAndFeelDecorated(false); //構造一個對話框 final JDialog dialog = new JDialog(frame, "點擊一個按鈕", true); //將對話框面板添加到對話框中 dialog.setContentPane(optionPane); //設置對話框關閉時的操作模式 dialog.setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { //當點擊關閉按鈕 setLabel("阻礙用戶視圖關閉窗體!"); } }); JDialog.setDefaultLookAndFeelDecorated(true); optionPane.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { String prop = e.getPropertyName(); if (dialog.isVisible() && (e.getSource() == optionPane) && (JOptionPane.VALUE_PROPERTY.equals(prop))) { //如果你要阻止關閉按鈕,可以在這里進行處理。 dialog.setVisible(false); } } }); dialog.pack(); dialog.setLocationRelativeTo(frame); dialog.setVisible(true); int value = ((Integer)optionPane.getValue()).intValue(); if (value == JOptionPane.YES_OPTION) { setLabel("好的"); } else if (value == JOptionPane.NO_OPTION) { setLabel("試圖點擊關閉按鈕來關閉一個不能關閉的對話框!" + "你不能!"); } else { setLabel("窗體可以使用ESC鍵關閉。"); } //自己定義版面 } else if (command == customOptionCommand) { customDialog.setLocationRelativeTo(frame); customDialog.setVisible(true); String s = customDialog.getValidatedText(); if (s != null) { //The text is valid. setLabel("歡迎你!" + "你已經進入了\"" + s + "\"。"); } //沒有模式 } else if (command == nonModalCommand) { //創建一個對話框 final JDialog dialog = new JDialog(frame, "一個沒有模式的對話框"); //使用html語言來顯示信息 JLabel label = new JLabel("<html><p align=center>" + "這是一個沒有模式的對話框<br>" + "你可以使用更多的格式<br>" + "甚至可以使用主窗體!"); label.setHorizontalAlignment(JLabel.CENTER); Font font = label.getFont(); label.setFont(label.getFont().deriveFont(font.PLAIN, 14.0f)); JButton closeButton = new JButton("關閉"); closeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.setVisible(false); dialog.dispose(); } }); JPanel closePanel = new JPanel(); closePanel.setLayout(new BoxLayout(closePanel, BoxLayout.LINE_AXIS)); closePanel.add(Box.createHorizontalGlue()); closePanel.add(closeButton); closePanel.setBorder(BorderFactory. createEmptyBorder(0,0,5,5)); JPanel contentPane = new JPanel(new BorderLayout()); contentPane.add(label, BorderLayout.CENTER); contentPane.add(closePanel, BorderLayout.PAGE_END); contentPane.setOpaque(true); dialog.setContentPane(contentPane); //顯示窗體 dialog.setSize(new Dimension(300, 150)); dialog.setLocationRelativeTo(frame); dialog.setVisible(true); } } }); return createPane(moreDialogDesc + ":", radioButtons, showItButton); } public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); //創建和設置一個窗體 JFrame frame = new JFrame("DialogDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置一個面板 Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(1,1)); contentPane.add(new DialogDemo(frame)); //顯示窗體 frame.pack(); frame.setVisible(true); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -