JOptionPane如何自定义按钮绑定事件

2018年01月29日 19:27:10
阅读数:475

摘自:https://blog.csdn.net/m0_37355951/article/details/79198713

JOptionPane是java方便快捷弹出窗口。虽然没有JDialog那么灵活,但是确实实用。切入正题如何在JOptionPane自定义按钮,这里用到如下方法。


  1. public static int showOptionDialog(Component parentComponent,
  2. Object message,
  3. String title,
  4. int optionType,
  5. int messageType,
  6. Icon icon,
  7. Object[] options,
  8. Object initialValue)
  9. throws HeadlessException调出一个带有指定图标的对话框,其中的初始选择由 initialValue 参数确定,选项数由 optionType 参数确定。
  10. 如果 optionType 为 YES_NO_OPTION 或者 YES_NO_CANCEL_OPTION,并且 options 参数为 null,则由外观提供选项。
  11. messageType 参数主要用于提供来自外观的默认图标。
  12. 参数:
  13. parentComponent - 确定在其中显示对话框的 Frame;如果为 null 或者 parentComponent 不具有 Frame,则使用默认的 Frame
  14. message - 要显示的 Object
  15. title - 对话框的标题字符串
  16. optionType - 指定可用于对话框的选项的整数:DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION 或 OK_CANCEL_OPTION
  17. messageType - 指定消息种类的整数,主要用于确定来自可插入外观的图标:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGE
  18. icon - 在对话框中显示的图标
  19. options - 指示用户可能选择的对象组成的数组;如果对象是组件,则可以正确呈现;非 String 对象使用其 toString 方法呈现;如果此参数为 null,则由外观确定选项
  20. initialValue - 表示对话框的默认选择的对象;只有在使用 options 时才有意义;可以为 null

这里网上找到的例子


  1. import javax.swing.JOptionPane;
  2. public class Main {
  3. public static void main(String[] argv) throws Exception {
  4. String[] buttons = { "Yes", "Yes to all", "No", "Cancel" };
  5. int rc = JOptionPane.showOptionDialog(null, "Question ?", "Confirmation",
  6. JOptionPane.WARNING_MESSAGE, 0, null, buttons, buttons[2]);
  7. System.out.println(rc);
  8. }
  9. }

引用:http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizeJOptionPanebuttons.htm
总结:

1、字符串数组是可以显示成按钮,那么问题,我怎么获取这个按钮对象呢?大胆尝试一下向里面直接传入button对象是否可以?

直接传入JButton对象


  1. public static void main(String[] args) {
  2. JButton jbutton = new JButton( "Yes");
  3. JButton jbuttons = new JButton( "no");
  4. JButton[] buttons = { jbutton,jbuttons};
  5. jbutton.addActionListener(new ActionListener() {
  6. @Override
  7. public void actionPerformed(ActionEvent paramActionEvent) {
  8. System.out.println("您点击了YES。。。。。");
  9. }
  10. });
  11. jbutton.setEnabled(false);
  12. int rc = JOptionPane.showOptionDialog(new JLabel("12322322"), "Question ?", "Confirmation",
  13. JOptionPane.INFORMATION_MESSAGE, 0, null, buttons, buttons[0]);
  14. System.out.println(rc);
  15. }

注意:jbutton事件绑定必须在JOptionPane.showOptionDialog(...)之前,因为这个对话框会阻塞,这个方法以下不会立即执行
继续修改问题:发现对话框关不掉了。是不是很尴尬?


  1. Window win = SwingUtilities.getWindowAncestor(jbutton); //找到该组件所在窗口
  2. win.dispose(); //关闭

整个代码如下:


  1. public static void main(String[] args) {
  2. final JButton jbutton = new JButton( "Yes");
  3. JButton jbuttons = new JButton( "no");
  4. JButton[] buttons = { jbutton,jbuttons};
  5. jbutton.addActionListener(new ActionListener() {
  6. @Override
  7. public void actionPerformed(ActionEvent paramActionEvent) {
  8. System.out.println("您点击了YES。。。。。");
  9. //这里就是关闭窗口
  10. Window win = SwingUtilities.getWindowAncestor(jbutton);
  11. win.dispose();
  12. }
  13. });
  14. int rc = JOptionPane.showOptionDialog(new JLabel("12322322"), "Question ?", "Confirmation",
  15. JOptionPane.INFORMATION_MESSAGE, 0, null, buttons, buttons[0]);
  16. System.out.println(rc);
  17. }

参考网址:

1、http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizeJOptionPanebuttons.htm

2、https://stackoverflow.com/questions/29357055/close-window-jpanel-in-java

JOptionPane如何自定义按钮绑定事件的更多相关文章

  1. 在VS2005中设置WPF中自定义按钮的事件

    原文:在VS2005中设置WPF中自定义按钮的事件 上篇讲了如何在Blend中绘制圆角矩形(http://blog.csdn.net/johnsuna/archive/2007/08/13/17407 ...

  2. 给bootstrap-sweetalert弹框的按钮绑定事件

    一. sweetalert cdn使用 sweetalert提供了很多的炫酷弹框,有很多的用法.关于本地导入使用sweetalert的方法,在之前的博客里提到过(点击前往),不过我们也可以使用cdn. ...

  3. 一百、SAP中ALV事件之十三,给ALV的自定义按钮添加事件

    一.我们查看定义的按钮,有一个名字是ZADD的自定义按钮 二.代码如下,用于判断点击了哪个按钮 三.点击测试按钮之后,会弹出一个弹窗 完美

  4. 常用代码之三:jQuery为按钮绑定事件的代码

    如题,比如有一个按钮:<input type='button' class='btn-text' id ='addHtml' value='新增' /> 为它添加onclick事件的代码: ...

  5. jquery给按钮绑定事件

    JQuery: $(function(){ $("#btn1").bind("click",function(){ $("#div1").s ...

  6. wxpython程序基本功能源码整理,包括基本文字,输入框,字体设置,按钮绑定事件触发

    #coding=utf-8 import wx class MyApp(wx.App): def __init__(self): wx.App.__init__(self) def OnInit(se ...

  7. Android自定义工具类获取按钮并绑定事件(利用暴力反射和注解)

    Android中为按钮绑定事件的有几种常见方式,你可以在布局文件中为按钮设置id,然后在MainActivity中通过findViewById方法获取按钮对象实例,再通过setOnClickListe ...

  8. Yii框架zii.widgets.grid自定义按钮,ajax触发事件并提示

    相关类手册: http://www.yiichina.com/api/CButtonColumn   buttons 属性 public array $buttons; the configurati ...

  9. Android_安卓为按钮控件绑定事件的五种方式

    一.写在最前面 本次,来介绍一下安卓中为控件--Button绑定事件的五种方式. 二.具体的实现 第一种:直接绑定在Button控件上: 步骤1.在Button控件上设置android:onClick ...

随机推荐

  1. L120 单词造句

    The old lady sits on a mobile chair every morning.The book contains scandalous text. The current sur ...

  2. hdu4451 Dressing(容斥原理)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  3. 【LeetCode】004. Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  4. 1、在Eclipse中安装TestNG(离线方式)

    1.TestNG安装包:链接: https://pan.baidu.com/s/1UXZlJfrp8LM-6XmDLzVXKg 密码: 46y2 2.安装教程: (1).下载testNG 离线安装包[ ...

  5. Visualforce入门第六篇_2017.3.1

    Visualforce实现过滤.数据列表显示.分页功能 可以参考salesforce官网开发文档:https://trailhead.salesforce.com/modules/visualforc ...

  6. 蓝桥杯 基础训练 BASIC-27 2n皇后问题

    基础练习 2n皇后问题   时间限制:1.0s   内存限制:512.0MB 问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都 ...

  7. [cinder] volume type 使用简记

    cinder type-create sharecinder type-key share set volume_backend_name=GLUSTERFScinder type-create lo ...

  8. SQL基础(2)

    SQL TOP (1)TOP子句 OP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. (2)SQL S ...

  9. PhoneGap应用图标icon和启动页面SplashScreen

    app/config.xml <icon gap:platform="android" gap:qualifier="ldpi" src="re ...

  10. Oracle 11g oracle 用户密码过期问题 (ZT)

    http://www.blogjava.net/freeman1984/archive/2013/04/23/398301.html Oracle 11g 之前默认的用户时是没有密码过期的限制的,在O ...