Swing中的JOptionPane类

Swing中的JOptionPane类详解
JOptionPane 有助于方便地弹出要求用户提供值或向其发出通知的标准对话框。

需要实例化

第一类:信息类对话框

方法1:showMessageDialog(Component parentComponent, Object message)
          调出标题为 "Message" 的信息消息对话框
   parentComponent - 确定在其中显示对话框的 Frame;如果为 null 或者           parentComponent 不具有 Frame,则使用默认的 Frame

方法1重载1:public static void showMessageDialog(
         Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType)
          messageType - 要显示的消息类型:有常量,区别为显示的图标不同
          JOptionPane.ERROR_MESSAGE,
   JOptionPane.INFORMATION_MESSAGE,
   JOptionPane.WARNING_MESSAGE,
   JOptionPane.QUESTION_MESSAGE
   JOptionPane.PLAIN_MESSAGE
方法1重载2:public static void showMessageDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType,
                                     Icon icon)
 icon - 要在对话框中显示的图标,该图标可以帮助用户识别要显示的消息种类

第二类:确认对话框

方法2:public static int showConfirmDialog(Component parentComponent,Object message)
       返回值为用户点击的按钮的序号
方法2重载:
       showConfirmDialog(Component parentComponent,Object message,
                                    String title,
                                    int optionType)
 optionType为YES_NO_OPTION、YES_NO_CANCEL_OPTION 或 OK_CANCEL_OPTION
方法2重载:
 showConfirmDialog(Component parentComponent,
                                    Object message,
                                    String title,
                                    int optionType,
                                    int messageType)
方法3重载:
  showConfirmDialog(Component parentComponent,
                                    Object message,
                                    String title,
                                    int optionType,
                                    int messageType,
                                    Icon icon)
 messageType - 指定此消息种类的整数;主要用于确定来自可插入外观的图标: ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或  PLAIN_MESSAGE

第三类:提示要求某些输入。
方法3:public static String showInputDialog(Component parentComponent,
                                     Object message)
方法3重载1:public static String showInputDialog(Component parentComponent,
                                     Object message,
                                     Object initialSelectionValue)
 initialSelectionValue - 用于初始化输入字段的值
方法3重载2:public static String showInputDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType)
 messageType - 要显示的消息类型:ERROR_MESSAGE、INFORMATION_MESSAGE、 WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGE

方法3重载3:public static Object showInputDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType,
                                     Icon icon,
                                     Object[] selectionValues,
                                     Object initialSelectionValue)
  selectionValues - 给出可能选择的 Object 数组,将显示成下拉列表框,只允许选择

 测试代码:

package dialogs;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class DialogTest {
	public static final JOptionPane j = new JOptionPane();
	public static void main(String[] args) {
		final JFrame jf = new JFrame();
		JButton btn1 = new JButton();
		JButton btn2 = new JButton();
		JButton btn3 = new JButton();
		btn1.setText("OptionDialog");
		btn2.setText("InputDialog");
		btn3.setText("MessageDialog");
		jf.setSize(350,80);
		jf.setResizable(false);
		Toolkit t = Toolkit.getDefaultToolkit();
		double width = t.getScreenSize().getWidth();
		double height = t.getScreenSize().getHeight();
		int x = Double.valueOf((width-320)/2).intValue();
		int y = Double.valueOf((height-80)/2).intValue();
		jf.setLocation(x,y);
		jf.setTitle("JOptionPaneTest by Eric zhou");
		jf.add(btn1,BorderLayout.EAST);
		jf.add(btn2,BorderLayout.CENTER);
		jf.add(btn3,BorderLayout.WEST);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		btn1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				confirm(jf,"Are You Sure?");
			}
		});
		btn2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				j.showInputDialog(jf,"Input Your Password Please!");
			}
		});
		btn3.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				alert(jf,"You Got A Message!");
			}
		});
	}
	public static void alert(Component parentComponent,String str){
		j.showMessageDialog(parentComponent,
                str,
                "actionPerformed...",
                JOptionPane.INFORMATION_MESSAGE,
                new ImageIcon("c:\\1.jpeg"));
	}
	public static void confirm(Component parentComponent,String str){
		j.showConfirmDialog( parentComponent,
                 str,
                 "actionPerformed...",
                 JOptionPane.OK_CANCEL_OPTION ,
                 JOptionPane.QUESTION_MESSAGE,
                 new ImageIcon("c:\\1.jpeg"));
	}
}

 

你可能感兴趣的:(JavaSe,java)