?? optiondialogframe.java
字號:
package optiondialog;import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * <p>Title: Option Dialog Demo</p> * <p>Description: This is a Option Dialog demo</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: d6-125</p> * @author Liujun * @version 1.0 */public class OptionDialogFrame extends JFrame { JPanel contentPane; GridBagLayout gridBagLayout1 = new GridBagLayout(); JRadioButton jRadioButton9 = new JRadioButton(); JPanel contentPane1 = new JPanel(); JRadioButton jRadioButton8 = new JRadioButton(); JRadioButton jRadioButton7 = new JRadioButton(); JRadioButton jRadioButton6 = new JRadioButton(); JRadioButton jRadioButton10 = new JRadioButton(); JRadioButton jRadioButton5 = new JRadioButton(); GridBagLayout gridBagLayout2 = new GridBagLayout(); JRadioButton jRadioButton4 = new JRadioButton(); JRadioButton jRadioButton3 = new JRadioButton(); JRadioButton jRadioButton2 = new JRadioButton(); JRadioButton jRadioButton1 = new JRadioButton(); JLabel jLabel2 = new JLabel(); JLabel jLabel1 = new JLabel(); JButton jButton1 = new JButton(); ButtonGroup buttonGroup1 = new ButtonGroup(); ButtonGroup buttonGroup2 = new ButtonGroup(); ButtonGroup buttonGroup3 = new ButtonGroup(); JRadioButton jRadioButton11 = new JRadioButton(); JRadioButton jRadioButton12 = new JRadioButton(); JRadioButton jRadioButton13 = new JRadioButton(); JRadioButton jRadioButton14 = new JRadioButton(); JLabel jLabel3 = new JLabel(); //定義的私有變量 String Title=null;//對話框標題 private String messageString = "Message";//字符串消息對象 private Icon messageIcon //圖標消息對象 = new ImageIcon("001.gif"); private Font messageFont//Font消息對象 = new Font("Serif", Font.PLAIN, 8); private Component messageComponent//組件消息對象 = new JPanel() { public void paintComponent(Graphics g) { super.paintComponent(g); g.setFont(messageFont); g.drawString("Component", 0, 8); } public Dimension getMinimumSize() { return new Dimension(12, 30); } }; ButtonGroup buttonGroup4 = new ButtonGroup(); JLabel jLabel4 = new JLabel(); JRadioButton jRadioButton15 = new JRadioButton(); JRadioButton jRadioButton16 = new JRadioButton(); JRadioButton jRadioButton17 = new JRadioButton(); JLabel jLabel5 = new JLabel(); //Construct the frame public OptionDialogFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(OptionDialogFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(gridBagLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("Frame Demo"); contentPane = (JPanel) this.getContentPane(); contentPane1.setLayout(gridBagLayout2); contentPane1.setBorder(BorderFactory.createRaisedBevelBorder()); jRadioButton9.setText("Other"); contentPane.setLayout(gridBagLayout1); this.setSize(new Dimension(545, 276)); this.setTitle("Return Message Frame Demo"); jRadioButton8.setText("Component"); jRadioButton7.setText("Icon"); jRadioButton6.setText("String"); jRadioButton10.setText("Object[]"); jRadioButton5.setText("PLAIN_MESSAGE"); jRadioButton4.setText("QUESTION_MESSAGE"); jRadioButton3.setText("WARNING_MESSAGE"); jRadioButton2.setText("INFORMATION_MESSAGE"); jRadioButton1.setText("ERROR_MESSAGE"); jLabel2.setText("消息"); jLabel1.setText("各種消息類型"); jButton1.setText("顯示消息框"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jRadioButton11.setText("缺省選項"); jRadioButton12.setText("Yes和No選項"); jRadioButton13.setText("Yes,No和Cancel選項"); jRadioButton14.setText("OK和Cancel選項"); jLabel4.setText("按鈕類型"); jRadioButton15.setText("字符方式"); jRadioButton16.setText("圖標方式"); jRadioButton17.setText("組合對象方式"); jLabel5.setText("選項方式"); contentPane1.add(jButton1, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 182, -6)); contentPane1.add(jRadioButton1, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton2, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton3, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton5, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton6, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton7, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton9, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jLabel2, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jLabel1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton10, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton8, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton4, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0 ,GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton11, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton12, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton13, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton14, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jLabel3, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jLabel4, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton15, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton16, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jRadioButton17, new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane1.add(jLabel5, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(contentPane1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); buttonGroup2.add(jRadioButton8); buttonGroup2.add(jRadioButton7); buttonGroup2.add(jRadioButton6); buttonGroup2.add(jRadioButton9); buttonGroup2.add(jRadioButton10); buttonGroup1.add(jRadioButton1); buttonGroup1.add(jRadioButton2); buttonGroup1.add(jRadioButton3); buttonGroup1.add(jRadioButton4); buttonGroup1.add(jRadioButton5); buttonGroup3.add(jRadioButton11); buttonGroup3.add(jRadioButton12); buttonGroup3.add(jRadioButton13); buttonGroup3.add(jRadioButton14); buttonGroup4.add(jRadioButton15); buttonGroup4.add(jRadioButton16); buttonGroup4.add(jRadioButton17); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public Object getMessage() { //用于得到消息對象 if (jRadioButton6.isSelected()) return messageString;//返回字符串消息對象 else if (jRadioButton7.isSelected()) return messageIcon;//返回圖標消息對象 else if (jRadioButton8.isSelected()) return messageComponent;//返回組件消息對象 else if (jRadioButton10.isSelected()) return new Object[]//返回新建立一個混合對象 { messageString, messageIcon, messageComponent, messageFont }; else if (jRadioButton9.isSelected()) return messageFont;//返回Font消息對象 else return null; } public int getType() { String s=null; if (jRadioButton1.isSelected()){ s ="ERROR_MESSAGE";//設置消息類型 Title="---有錯誤";//設置標題 } else if (jRadioButton2.isSelected()){ s ="INFORMATION_MESSAGE";//設置消息類型 Title="---是提醒消息";//設置標題 } else if (jRadioButton3.isSelected()){ s ="WARNING_MESSAGE";//設置消息類型 Title="---警告消息";//設置標題 } else if (jRadioButton4.isSelected()){ s ="QUESTION_MESSAGE";//設置消息類型 Title="---是一個問題";//設置標題 } else if(jRadioButton5.isSelected()){ s ="PLAIN_MESSAGE";//設置消息類型 Title="---一般的消息";//設置標題 } try { Class cl = JOptionPane.class;//得到對話框類型 return cl.getField(s).getInt(cl);//根據消息類型返回類型代碼 } catch(Exception e) { return -1; } } public int getConfirmType() { String s=null; if (jRadioButton11.isSelected()){ s ="DEFAULT_OPTION";//設置消息按鈕類型 } else if (jRadioButton12.isSelected()){ s ="YES_NO_OPTION";//設置消息按鈕類型 } else if (jRadioButton13.isSelected()){ s ="YES_NO_CANCEL_OPTION";//設置消息按鈕類型 } else if (jRadioButton14.isSelected()){ s ="OK_CANCEL_OPTION";//設置消息按鈕類型 } try { Class cl = JOptionPane.class;//得到對話框類型 return cl.getField(s).getInt(cl);//根據消息按鈕類型返回按鈕類型代碼 } catch(Exception e) { return -1; } } public Object[] getOptions() {//得到選項類型對象 String s=null; if (jRadioButton15.isSelected()){ //返回字符串數組 return new String[] { "Yellow", "Blue", "Red" }; } else if (jRadioButton16.isSelected()){ return new Icon[]//返回圖標數組 { new ImageIcon("001.gif"), new ImageIcon("002.gif"), new ImageIcon("003.gif") }; } else if (jRadioButton17.isSelected()){ return new Object[]//返回一個組合對象數組 { messageString, messageIcon, messageComponent, messageFont }; } else return null;//否則返回空對象 } void jButton1_actionPerformed(ActionEvent e) { int Type=getType();//返回消息類型碼,顯示消息對話框 int kk=JOptionPane.showOptionDialog(this, getMessage(), "消息框演示"+Title, getConfirmType(), Type, null, getOptions(), getOptions()[0] ); jLabel3.setText(String.valueOf(kk));//顯示返回值 }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -