?? messagedialogframe.java
字號:
package messagedialog;import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * <p>Title: Message Dialog Demo</p> * <p>Description: This is a MessageDialog demo</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: d6-125</p> * @author Liujun * @version 1.0 */public class MessageDialogFrame extends JFrame { JPanel contentPane; JButton jButton1 = new JButton(); GridBagLayout gridBagLayout1 = new GridBagLayout(); ButtonGroup buttonGroup1 = new ButtonGroup(); ButtonGroup buttonGroup2 = new ButtonGroup(); JRadioButton jRadioButton1 = new JRadioButton(); JRadioButton jRadioButton2 = new JRadioButton(); JRadioButton jRadioButton3 = new JRadioButton(); JRadioButton jRadioButton4 = new JRadioButton(); JRadioButton jRadioButton5 = new JRadioButton(); JRadioButton jRadioButton6 = new JRadioButton(); JRadioButton jRadioButton7 = new JRadioButton(); JRadioButton jRadioButton8 = new JRadioButton(); JRadioButton jRadioButton9 = new JRadioButton(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JRadioButton jRadioButton10 = new JRadioButton(); //定義的私有變量 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); } }; //Construct the frame public MessageDialogFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(MessageDialogFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(gridBagLayout1); this.setSize(new Dimension(336, 249)); this.setTitle("Message Dialog Frame Demo"); jButton1.setText("顯示消息框"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); contentPane.setBorder(BorderFactory.createRaisedBevelBorder()); jRadioButton1.setText("ERROR_MESSAGE"); jRadioButton2.setText("INFORMATION_MESSAGE"); jRadioButton3.setText("WARNING_MESSAGE"); jRadioButton4.setText("QUESTION_MESSAGE"); jRadioButton5.setText("PLAIN_MESSAGE"); jRadioButton6.setText("String"); jRadioButton7.setText("Icon"); jRadioButton8.setText("Component"); jRadioButton9.setText("Other"); jLabel1.setText("各種消息類型"); jLabel2.setText("消息"); jRadioButton10.setText("Object[]"); contentPane.add(jButton1, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 80, -6)); contentPane.add(jRadioButton1, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton2, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton3, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton4, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton5, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton6, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton7, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton9, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); buttonGroup1.add(jRadioButton1); buttonGroup1.add(jRadioButton2); buttonGroup1.add(jRadioButton3); buttonGroup1.add(jRadioButton4); buttonGroup1.add(jRadioButton5); buttonGroup2.add(jRadioButton6); buttonGroup2.add(jRadioButton7); buttonGroup2.add(jRadioButton8); buttonGroup2.add(jRadioButton9); buttonGroup2.add(jRadioButton10); contentPane.add(jLabel2, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jLabel1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton10, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton8, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(-5, 0, 5, 0), 0, 0)); } //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; } } void jButton1_actionPerformed(ActionEvent e) { int Type=getType();//返回消息類型碼,顯示消息對話框 JOptionPane.showMessageDialog(this, getMessage(), "消息框演示"+Title, Type); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -