码迷,mamicode.com
首页 > 其他好文 > 详细

使用showMessageDialog显示消息框

时间:2017-04-06 22:17:03      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:点击   use   pac   静态   lis   xtend   window   返回   调用   

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

   

   

   

   

   

   

工程名:TestJOptionPane

包名:com.siwuxie095.showdialog

类名:TestMessageDialog.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 TestMessageDialog extends JFrame {

   

private JPanel contentPane;

   

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

TestMessageDialog frame = new TestMessageDialog();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

   

/**

* Create the frame.

*/

public TestMessageDialog() {

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 btnshowmessagedialog = new JButton("显示消息框(showMessageDialog)");

btnshowmessagedialog.setFocusable(false);

 

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

btnshowmessagedialog.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

 

/**

* 直接通过静态方法调用

* 需要指定父级窗体,消息,标题,消息类型

* 返回值是 void,即不会向系统返回任何信息

* 即消息框仅仅是为了向用户传达信息

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

*/

JOptionPane.showMessageDialog(TestMessageDialog.this,

"NullPointerException","Error", JOptionPane.ERROR_MESSAGE);

}

});

contentPane.add(btnshowmessagedialog, BorderLayout.NORTH);

}

   

}

   

   

   

将窗体 JFrame 的 LookAndFeel 设定为 Windows

   

   

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

将其 focusable 属性设为 false

   

   

为 JButton 添加 mouseClicked 事件,点击 按钮 弹出消息框

   

   

   

运行程序:

   

技术分享

   

   

   

   

   

   

   

   

【made by siwuxie095】

使用showMessageDialog显示消息框

标签:点击   use   pac   静态   lis   xtend   window   返回   调用   

原文地址:http://www.cnblogs.com/siwuxie095/p/6675484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!