📄 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 + -