📄 inputdialogframe.java
字号:
package inputdialog;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.lang.String;/** * <p>Title: Input Dialog Demo</p> * <p>Description: This is a Input Dialog demo</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: d6-125</p> * @author Liujun * @version 1.0 */public class InputDialogFrame extends JFrame { JPanel contentPane; //定义的私有变量 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); } }; 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(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); TitledBorder titledBorder1; //Construct the frame public InputDialogFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(InputDialogFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); titledBorder1 = new TitledBorder(""); contentPane.setLayout(gridBagLayout1); this.setSize(new Dimension(387, 216)); this.setTitle("Input Dialog Frame Demo"); contentPane1.setLayout(gridBagLayout2); contentPane1.setBorder(BorderFactory.createRaisedBevelBorder()); jRadioButton9.setText("Other"); 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("组合框输入方式"); jLabel4.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(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)); 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); } //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();//返回消息类型码,显示消息对话框 if (jRadioButton11.isSelected()){ //输入文本信息 String kk=JOptionPane.showInputDialog(this, getMessage(), "消息框演示"+Title, Type); jLabel3.setText(kk);//显示返回值 } else if (jRadioButton12.isSelected()){ String kk=null; //从组合框中选择输入 kk = (String)JOptionPane.showInputDialog(this, getMessage(), "消息框演示"+Title, Type, null, new String[] { "面包", "牛奶", "鸡蛋" ,"啤酒"}, "牛奶"); jLabel3.setText(kk);//显示返回值 } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -