栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

04.JavaSwing(JOptionPane类)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

04.JavaSwing(JOptionPane类)

一、概述

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

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\1.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);

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/648299.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号