JOptionPane如何自定义按钮绑定事件
JOptionPane如何自定义按钮绑定事件
摘自:https://blog.csdn.net/m0_37355951/article/details/79198713
JOptionPane是java方便快捷弹出窗口。虽然没有JDialog那么灵活,但是确实实用。切入正题如何在JOptionPane自定义按钮,这里用到如下方法。
-
public static int showOptionDialog(Component parentComponent,
-
Object message,
-
String title,
-
int optionType,
-
int messageType,
-
Icon icon,
-
Object[] options,
-
Object initialValue)
-
throws HeadlessException调出一个带有指定图标的对话框,其中的初始选择由 initialValue 参数确定,选项数由 optionType 参数确定。
-
如果 optionType 为 YES_NO_OPTION 或者 YES_NO_CANCEL_OPTION,并且 options 参数为 null,则由外观提供选项。
-
-
messageType 参数主要用于提供来自外观的默认图标。
-
-
-
参数:
-
parentComponent - 确定在其中显示对话框的 Frame;如果为 null 或者 parentComponent 不具有 Frame,则使用默认的 Frame
-
message - 要显示的 Object
-
title - 对话框的标题字符串
-
optionType - 指定可用于对话框的选项的整数:DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION 或 OK_CANCEL_OPTION
-
messageType - 指定消息种类的整数,主要用于确定来自可插入外观的图标:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGE
-
icon - 在对话框中显示的图标
-
options - 指示用户可能选择的对象组成的数组;如果对象是组件,则可以正确呈现;非 String 对象使用其 toString 方法呈现;如果此参数为 null,则由外观确定选项
-
initialValue - 表示对话框的默认选择的对象;只有在使用 options 时才有意义;可以为 null
这里网上找到的例子
-
import javax.swing.JOptionPane;
-
-
public class Main {
-
public static void main(String[] argv) throws Exception {
-
-
String[] buttons = { "Yes", "Yes to all", "No", "Cancel" };
-
-
int rc = JOptionPane.showOptionDialog(null, "Question ?", "Confirmation",
-
JOptionPane.WARNING_MESSAGE, 0, null, buttons, buttons[2]);
-
-
System.out.println(rc);
-
-
}
-
}
引用:http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizeJOptionPanebuttons.htm
总结:
1、字符串数组是可以显示成按钮,那么问题,我怎么获取这个按钮对象呢?大胆尝试一下向里面直接传入button对象是否可以?
直接传入JButton对象
-
public static void main(String[] args) {
-
JButton jbutton = new JButton( "Yes");
-
JButton jbuttons = new JButton( "no");
-
JButton[] buttons = { jbutton,jbuttons};
-
jbutton.addActionListener(new ActionListener() {
-
@Override
-
public void actionPerformed(ActionEvent paramActionEvent) {
-
System.out.println("您点击了YES。。。。。");
-
-
}
-
});
-
jbutton.setEnabled(false);
-
int rc = JOptionPane.showOptionDialog(new JLabel("12322322"), "Question ?", "Confirmation",
-
JOptionPane.INFORMATION_MESSAGE, 0, null, buttons, buttons[0]);
-
-
System.out.println(rc);
-
}
注意:jbutton事件绑定必须在JOptionPane.showOptionDialog(...)之前,因为这个对话框会阻塞,这个方法以下不会立即执行
继续修改问题:发现对话框关不掉了。是不是很尴尬?
-
Window win = SwingUtilities.getWindowAncestor(jbutton); //找到该组件所在窗口
-
win.dispose(); //关闭
整个代码如下:
-
public static void main(String[] args) {
-
final JButton jbutton = new JButton( "Yes");
-
JButton jbuttons = new JButton( "no");
-
JButton[] buttons = { jbutton,jbuttons};
-
jbutton.addActionListener(new ActionListener() {
-
@Override
-
public void actionPerformed(ActionEvent paramActionEvent) {
-
System.out.println("您点击了YES。。。。。");
-
//这里就是关闭窗口
-
Window win = SwingUtilities.getWindowAncestor(jbutton);
-
win.dispose();
-
}
-
});
-
int rc = JOptionPane.showOptionDialog(new JLabel("12322322"), "Question ?", "Confirmation",
-
JOptionPane.INFORMATION_MESSAGE, 0, null, buttons, buttons[0]);
-
-
System.out.println(rc);
-
}
参考网址:
1、http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizeJOptionPanebuttons.htm
2、https://stackoverflow.com/questions/29357055/close-window-jpanel-in-java
JOptionPane如何自定义按钮绑定事件的更多相关文章
- 在VS2005中设置WPF中自定义按钮的事件
原文:在VS2005中设置WPF中自定义按钮的事件 上篇讲了如何在Blend中绘制圆角矩形(http://blog.csdn.net/johnsuna/archive/2007/08/13/17407 ...
- 给bootstrap-sweetalert弹框的按钮绑定事件
一. sweetalert cdn使用 sweetalert提供了很多的炫酷弹框,有很多的用法.关于本地导入使用sweetalert的方法,在之前的博客里提到过(点击前往),不过我们也可以使用cdn. ...
- 一百、SAP中ALV事件之十三,给ALV的自定义按钮添加事件
一.我们查看定义的按钮,有一个名字是ZADD的自定义按钮 二.代码如下,用于判断点击了哪个按钮 三.点击测试按钮之后,会弹出一个弹窗 完美
- 常用代码之三:jQuery为按钮绑定事件的代码
如题,比如有一个按钮:<input type='button' class='btn-text' id ='addHtml' value='新增' /> 为它添加onclick事件的代码: ...
- jquery给按钮绑定事件
JQuery: $(function(){ $("#btn1").bind("click",function(){ $("#div1").s ...
- wxpython程序基本功能源码整理,包括基本文字,输入框,字体设置,按钮绑定事件触发
#coding=utf-8 import wx class MyApp(wx.App): def __init__(self): wx.App.__init__(self) def OnInit(se ...
- Android自定义工具类获取按钮并绑定事件(利用暴力反射和注解)
Android中为按钮绑定事件的有几种常见方式,你可以在布局文件中为按钮设置id,然后在MainActivity中通过findViewById方法获取按钮对象实例,再通过setOnClickListe ...
- Yii框架zii.widgets.grid自定义按钮,ajax触发事件并提示
相关类手册: http://www.yiichina.com/api/CButtonColumn buttons 属性 public array $buttons; the configurati ...
- Android_安卓为按钮控件绑定事件的五种方式
一.写在最前面 本次,来介绍一下安卓中为控件--Button绑定事件的五种方式. 二.具体的实现 第一种:直接绑定在Button控件上: 步骤1.在Button控件上设置android:onClick ...
随机推荐
- L120 单词造句
The old lady sits on a mobile chair every morning.The book contains scandalous text. The current sur ...
- hdu4451 Dressing(容斥原理)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- 【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 ...
- 1、在Eclipse中安装TestNG(离线方式)
1.TestNG安装包:链接: https://pan.baidu.com/s/1UXZlJfrp8LM-6XmDLzVXKg 密码: 46y2 2.安装教程: (1).下载testNG 离线安装包[ ...
- Visualforce入门第六篇_2017.3.1
Visualforce实现过滤.数据列表显示.分页功能 可以参考salesforce官网开发文档:https://trailhead.salesforce.com/modules/visualforc ...
- 蓝桥杯 基础训练 BASIC-27 2n皇后问题
基础练习 2n皇后问题 时间限制:1.0s 内存限制:512.0MB 问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都 ...
- [cinder] volume type 使用简记
cinder type-create sharecinder type-key share set volume_backend_name=GLUSTERFScinder type-create lo ...
- SQL基础(2)
SQL TOP (1)TOP子句 OP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. (2)SQL S ...
- PhoneGap应用图标icon和启动页面SplashScreen
app/config.xml <icon gap:platform="android" gap:qualifier="ldpi" src="re ...
- Oracle 11g oracle 用户密码过期问题 (ZT)
http://www.blogjava.net/freeman1984/archive/2013/04/23/398301.html Oracle 11g 之前默认的用户时是没有密码过期的限制的,在O ...