本文目录一览:
Java在Swing中如何实现弹出一个对话框的效果?
可以使用JoptionPane:
有几种提示框:
第一种:
JOptionPane.showMessageDialog(jPanel, "提示消息", "标题",JOptionPane.WARNING_MESSAGE);
第二种:
int n = JOptionPane.showConfirmDialog(null, "你高兴吗?", "标题",JOptionPane.YES_NO_OPTION);//返回的是按钮的index i=0或者1
第三种:
Object[] obj2 ={ "足球", "篮球", "乒乓球" };
String s = (String) JOptionPane.showInputDialog(null,"请选择你的爱好:\n", "爱好", JOptionPane.PLAIN_MESSAGE, new ImageIcon("icon.png"), obj2, "足球");
java中关于页面弹出对话框问题
if(JOptionPane.showConfirmDialog(null, "确定不通过此次家长会申请吗?", "提示", JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_CANCEL_OPTION)==JOptionPane.YES_OPTION)或者下面
if(JOptionPane.showConfirmDialog(null, "确定不通过此次家长会申请吗?", "提示", JOptionPane.OK_CANCEL_OPTION)==0){
request.getRequestDispatcher("/AppAudit.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/a.jsp").forward(request, response);
}
方法签名:
showConfirmDialog
public static int showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType,
int messageType)
throws HeadlessException
调用一个由 optionType 参数确定其中选项数的对话框,messageType 参数确定要显示的图标。messageType 参数主要用于提供来自外观的默认图标。
参数:
parentComponent - 确定在其中显示对话框的 Frame;如果为 null 或者 parentComponent 不具有 Frame,则使用默认的 Frame。
message - 要显示的 Object
title - 对话框的标题字符串
optionType - 指定可用于对话框的选项的整数:YES_NO_OPTION 或 YES_NO_CANCEL_OPTION
messageType - 指定此消息种类的整数;主要用于确定来自可插入外观的图标:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGE
返回:
指示用户所选选项的整数
源码如下:
public class JOptionPane extends JComponent implements Accessible
{
public static final int YES_OPTION = 0;
/** Return value from class method if NO is chosen. */
public static final int NO_OPTION = 1;
/** Return value from class method if CANCEL is chosen. */
public static final int CANCEL_OPTION = 2;
/** Return value form class method if OK is chosen. */
public static final int OK_OPTION = 0;
/** Return value from class method if user closes window without selecting
* anything, more than likely this should be treated as either a
* codeCANCEL_OPTION/code or codeNO_OPTION/code. */
public static final int CLOSED_OPTION = -1;
}
如何用java弹出自己编辑的对话框
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class showMessage extends JFrame{
public showMessage(){
Container c =this.getContentPane();
JButton jb = new JButton("点我出现message");
c.add(jb,BorderLayout.NORTH);
setSize(100, 80);
setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "没错,我就是神奇的Message!");
}
});
}
public static void main(String[] args) {
new showMessage();
}
}