04.JavaSwing(JOptionPane类)

04.JavaSwing(JOptionPane类),第1张

04.JavaSwing(JOptionPane类) 一、概述

JOptionPane 有助于方便地d出要求用户提供值或向其发出通知的标准对话框。而且是 JavaSwing 内部已实现好的,以静态方法的形式提供调用,能够快速方便的d出要求用户提供值或向其发出通知的标准对话框。

JOptionPane 提供的 标准对话框 类型分为以下几种

方法名描述show/confirm/iDialog询问一个确认问题,如 yes/no/cancel。showInputDialog提示要求某些输入。showMessageDialog告知用户某事已发生。showOptionDialog上述三项的大统一 (Grand Unification)。
二、showMessageDialog

概述:显示一个带有OK 按钮的模态对话框。

static showMessageDialog(Component parentComponent, Object message) 


static void showMessageDialog(Component parentComponent, Object message, String title, int messageType) 

static void showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon) 

实例

错误对话框

JOptionPane.showMessageDialog(null, "错误", "提示", JOptionPane.ERROR_MESSAGE);

 警示对话框

JOptionPane.showMessageDialog(null, "警告", "提示", JOptionPane.WARNING_MESSAGE);

 普通信息对话框

JOptionPane.showMessageDialog(null, "普通", "提示",JOptionPane.INFORMATION_MESSAGE);

询问信息对话框

JOptionPane.showMessageDialog(null, "提问信息", "提示",JOptionPane.QUESTION_MESSAGE);

不带图标信息对话框

JOptionPane.showMessageDialog(null, "不带图标信息", "提示", JOptionPane.PLAIN_MESSAGE);

自定义图标对话框

JOptionPane.showMessageDialog(null, "message", "这是一个标题", JOptionPane.CANCEL_OPTION, new ImageIcon(
				new ImageIcon("images.jpg").getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT)));

三、showConfirmDialog
//调出带有选项 Yes、No 和 Cancel 的对话框;标题为 Select an Option。
static showConfirmDialog(Component parentComponent, Object message) 
//调出一个由 optionType 参数确定其中选项数的对话框。      
static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType) 
// 调用一个由 optionType 参数确定其中选项数的对话框,messageType 参数确定要显示的图标。        
static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType) 
//调出一个带有指定图标的对话框,其中的选项数由 optionType 参数确定。        
static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon) 
        

实例

int showConfirmDialog = JOptionPane.showConfirmDialog(null, "你确定要分手吗?");
if (showConfirmDialog == 0) {
    System.out.println("确定");
} else if (showConfirmDialog == 1) {
    System.out.println("否");
} else if (showConfirmDialog == 2) {
    System.out.println("取消");
}

JOptionPane.showConfirmDialog(null, "这是我的DEMO", "提示", JOptionPane.OK_OPTION);
JOptionPane.showConfirmDialog(null, "这是我的DEMO", "提示", JOptionPane.YES_OPTION);
JOptionPane.showConfirmDialog(null, "这是我的DEMO", "提示", JOptionPane.YES_NO_OPTION);
JOptionPane.showConfirmDialog(null, "这是我的DEMO", "提示", JOptionPane.NO_OPTION);
JOptionPane.showConfirmDialog(null, "这是我的DEMO", "提示", JOptionPane.YES_NO_CANCEL_OPTION);
JOptionPane.showConfirmDialog(null, "这是我的DEMO", "提示", JOptionPane.CANCEL_OPTION);
JOptionPane.showConfirmDialog(null, "这是我的DEMO", "提示", JOptionPane.OK_CANCEL_OPTION);
JOptionPane.showConfirmDialog(null, "这是我的DEMO", "提示", JOptionPane.CLOSED_OPTION);
JOptionPane.showConfirmDialog(null, "这是我的DEMO", "提示", JOptionPane.DEFAULT_OPTION);

四、showInputDialog
		
static String showInputDialog(Component parentComponent, Object message) 

static String showInputDialog(Component parentComponent, Object message, Object initialSelectionValue) 
   
static String showInputDialog(Component parentComponent, Object message, String title, int messageType) 
        
static Object showInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) 
      
static String showInputDialog(Object message) 
      
static String showInputDialog(Object message, Object initialSelectionValue) 

实例

String showInputDialog = JOptionPane.showInputDialog("请输入一个姓名:");
		System.out.println(showInputDialog);

 

Object[] fruits = {"苹果","梨子","香蕉","西瓜","荔枝"};
		
		
Object showInputDialog = JOptionPane.showInputDialog(null,"你喜欢什么水果","标题",JOptionPane.QUESTION_MESSAGE,null,fruits,fruits[2]);
System.out.println(showInputDialog);

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5650500.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存