本文目錄一覽:
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();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/243561.html