• 使用showConfirmDialog显示确认框


    ------------------siwuxie095

       

       

       

       

       

       

       

       

    工程名:TestJOptionPane

    包名:com.siwuxie095.showdialog

    类名:TestConfirmDialog.java

       

       

    工程结构目录如下:

       

       

       

       

       

       

    代码:

       

    package com.siwuxie095.showdialog;

       

    import java.awt.BorderLayout;

    import java.awt.EventQueue;

       

    import javax.swing.JFrame;

    import javax.swing.JOptionPane;

    import javax.swing.JPanel;

    import javax.swing.UIManager;

    import javax.swing.UnsupportedLookAndFeelException;

    import javax.swing.border.EmptyBorder;

       

    import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

       

    import javax.swing.JButton;

    import java.awt.event.MouseAdapter;

    import java.awt.event.MouseEvent;

       

    public class TestConfirmDialog extends JFrame {

       

    private JPanel contentPane;

       

    /**

    * Launch the application.

    */

    public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {

    public void run() {

    try {

    TestConfirmDialog frame = new TestConfirmDialog();

    frame.setVisible(true);

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    });

    }

       

    /**

    * Create the frame.

    */

    public TestConfirmDialog() {

     

    try {

    UIManager.setLookAndFeel(new WindowsLookAndFeel());

    } catch (UnsupportedLookAndFeelException e1) {

    e1.printStackTrace();

    }

     

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setBounds(100, 100, 450, 300);

    contentPane = new JPanel();

    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

    contentPane.setLayout(new BorderLayout(0, 0));

    setContentPane(contentPane);

     

    JButton btnshowconfirmdialog = new JButton("显示确认框(showConfirmDialog)");

     

    // 按钮 添加鼠标点击事件

    btnshowconfirmdialog.addMouseListener(new MouseAdapter() {

    @Override

    public void mouseClicked(MouseEvent e) {

     

    //直接通过静态方法调用,指定父级窗体 信息

    //返回值是 int 类型,创建以接收返回值

    //没有关闭确认框时,后面的主窗体是完全无法操作的(即 阻塞)

     

    // int value=JOptionPane.showConfirmDialog(TestConfirmDialog.this,

    // "你确认退出吗?");

    //

    // if (value==JOptionPane.YES_OPTION) {

    // System.out.println("你选择了 ");

    // System.exit(0);

    // }else if (value==JOptionPane.NO_OPTION) {

    // System.out.println("你选择了 ");

    // }else if (value==JOptionPane.CANCEL_OPTION) {

    // System.out.println("你选择了 取消");

    // }else if (value==JOptionPane.CLOSED_OPTION) {

    // System.out.println("你直接将窗体关闭了,没有选择");

    // }

     

     

    //需要指定 父级窗体,信息,标题,选项类型,信息类型

    int valuex=JOptionPane.showConfirmDialog(TestConfirmDialog.this,

    "你确认要退出程序吗?", "请确认",

    JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

     

     

    //判断时要根据第四个参数 optionType

    //可以选择一个最有用的按钮来判断它的值并处理即可,不用全部都判断

    if (valuex==JOptionPane.YES_OPTION) {

    System.exit(0);

    }

    }

    });

    btnshowconfirmdialog.setFocusable(false);

    contentPane.add(btnshowconfirmdialog, BorderLayout.NORTH);

    }

       

    }

       

       

       

    将窗体 JFrame 的 LookAndFeel 设定为 Windows

       

       

    在根面板 contentPane 的上方添加一个 JButton,

    将其 focusable 属性设为 false

       

       

    为 JButton 添加 mouseClicked 事件,点击 按钮 弹出确认框

       

       

       

    运行程序:

       

       

       

       

       

       

       

    【made by siwuxie095】

  • 相关阅读:
    日常学习——FFT
    poj 3353 Road Construction tarjan 边双联通分支 缩点+结论
    4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。
    tarjan总结
    hdu 4655 Cut Pieces 找规律
    POJ3592 Instantaneous Transference tarjan +spfa
    hdu 4647 Another Graph Game
    hdu4638 group 树状数组
    4630 no pain no game 树状数组
    hdu 4619 Warm up 2 网络流 最小割
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/6675457.html
Copyright © 2020-2023  润新知