弹出消息框

时间:2011-08-16 14:52:50

标签: java swing netbeans

我不确定如何在我的方法中编写弹出消息框。

public String verify(){
    String result = "failed";
    int authcode = staffBean.getVerifyCodeByName(getLoginUserName());

    if (code == authcode){       
        result ="success";
    }    
    else{ //statement to popup an error message box

    }
    return result;
}

我尝试在我的方法中使用JOptionPane,但它不起作用:

String st = "Welcome";
JOptionPane.showMessageDialog(null, st);

8 个答案:

答案 0 :(得分:132)

javax.swing.JOptionPane中

以下是每当我想要弹出信息框时我调用的方法的代码,它会占用屏幕直到它被接受为止:

import javax.swing.JOptionPane;

public class ClassNameHere
{

    public static void infoBox(String infoMessage, String titleBar)
    {
        JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + titleBar, JOptionPane.INFORMATION_MESSAGE);
    }
}

第一个JOptionPane参数(此示例中为null)用于对齐对话框。 null会使其在屏幕上居中,但可以指定任何java.awt.Component,对话框也会显示在Component的中心。

我倾向于使用titleBar字符串来描述调用框中代码的位置,如果它变得烦人,我可以轻松地跟踪并删除负责使用infoBoxes向我的屏幕发送垃圾邮件的代码。

要使用此方法,请致电:

ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE");

javafx.scene.control.Alert

有关如何使用JavaFX对话框的深入描述,请参阅:JavaFX Dialogs (official) by code.makery。它们比Swing对话框更强大,更灵活,并且远远不仅仅是弹出消息。

如上所述,我将发布一个小例子,说明如何使用JavaFX对话框来实现相同的结果

import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.application.Platform;

public class ClassNameHere
{

    public static void infoBox(String infoMessage, String titleBar)
    {
        /* By specifying a null headerMessage String, we cause the dialog to
           not have a header */
        infoBox(infoMessage, titleBar, null);
    }

    public static void infoBox(String infoMessage, String titleBar, String headerMessage)
    {
        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle(titleBar);
        alert.setHeaderText(headerMessage);
        alert.setContentText(infoMessage);
        alert.showAndWait();
    }
}

要记住的一件事是JavaFX是一个单线程GUI工具包,这意味着应该直接从JavaFX应用程序线程调用此方法。如果你有另一个线程正在工作,需要一个对话框,那么请看这些SO Q& As:JavaFX2: Can I pause a background Task / Service?Platform.Runlater and Task Javafx

要使用此方法,请致电:

ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE");

ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE", "HEADER MESSAGE");

答案 1 :(得分:25)

首先你必须导入: 导入javax.swing.JOptionPane; 然后你可以用它来调用它:

JOptionPane.showMessageDialog(null, 
                              "ALERT MESSAGE", 
                              "TITLE", 
                              JOptionPane.WARNING_MESSAGE);

null将其置于屏幕中间。在警报信息下放任何引号。标题显然是标题,最后一部分将其格式化为错误消息。如果您想要常规消息,请将其替换为PLAIN_MESSAGE。它在很多方面都非常适用于错误。

答案 2 :(得分:6)

一些"增强功能"我用于调试,特别是在运行项目时(即不在调试模式下)。

  1. 默认消息框标题为调用方法的名称。这对于在给定点停止线程很方便,但必须在发布之前进行清理。
  2. 自动将来电者姓名和留言复制到剪贴板,因为您无法搜索图片!

    package forumposts;
    
    import java.awt.Toolkit;
    import java.awt.datatransfer.Clipboard;
    import java.awt.datatransfer.StringSelection;
    import javax.swing.JOptionPane;
    
    public final class MsgBox
    {
        public static void info(String message) {
            info(message, theNameOfTheMethodThatCalledMe());
        }
        public static void info(String message, String caller) {
            show(message, caller, JOptionPane.INFORMATION_MESSAGE);
        }
    
        static void error(String message) {
            error(message, theNameOfTheMethodThatCalledMe());
        }
        public static void error(String message, String caller) {
            show(message, caller, JOptionPane.ERROR_MESSAGE);
        }
    
        public static void show(String message, String title, int iconId) {
            setClipboard(title+":"+NEW_LINE+message);
            JOptionPane.showMessageDialog(null, message, title, iconId);
        }
        private static final String NEW_LINE = System.lineSeparator();
    
        public static String theNameOfTheMethodThatCalledMe() {
            return Thread.currentThread().getStackTrace()[3].getMethodName();
        }
    
        public static void setClipboard(String message) {
            CLIPBOARD.setContents(new StringSelection(message), null);
            // nb: we don't respond to the "your content was splattered"
            //     event, so it's OK to pass a null owner.
        }
        private static final Toolkit AWT_TOOLKIT = Toolkit.getDefaultToolkit();
        private static final Clipboard CLIPBOARD = AWT_TOOLKIT.getSystemClipboard();
    
    }
    
  3. 完整的类也有调试和警告方法,但我为了简洁而剪切它们,无论如何你得到了主要观点。您可以使用公共静态布尔值isDebugEnabled来抑制调试消息。如果操作正确,优化器将(几乎)从生产代码中删除这些方法调用。请参阅:http://c2.com/cgi/wiki?ConditionalCompilationInJava

    干杯。基思。

答案 3 :(得分:1)

JOptionPane.showMessageDialog(btn1, "you are clicked save button","title of dialog",2);

btn1是一个JButton变量,它在此对话框中用于对话框打开位置btn1或textfield等,默认情况下使用frame的空位置。下一步是你的消息,接下来是对话框的标题。警报类型图标3的2个数字是信息1,2,3,4。 好的,我希望你理解它

答案 4 :(得分:0)

好的,基本上我认为我有一个简单而有效的解决方案。

package AnotherPopUpMessage;
import javax.swing.JOptionPane;
public class AnotherPopUp {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JOptionPane.showMessageDialog(null, "Again? Where do all these come from?", 
            "PopUp4", JOptionPane.CLOSED_OPTION);
    }
}

答案 5 :(得分:0)

使用以下库: dev

在代码行的顶部输入。 您只需添加此内容,因为其他操作正确完成了!

答案 6 :(得分:0)

import javax.swing.*;
class Demo extends JFrame
{
           String str1;
           Demo(String s1)
           {
             str1=s1;
            JOptionPane.showMessageDialog(null,"your message : "+str1);
            }
            public static void main (String ar[])
            {
             new Demo("Java");
            }
}

答案 7 :(得分:-1)

APPLET中的POP UP WINDOWS

大家好我在互联网上搜索applet中的弹出窗口但是找不到Windows的答案。

虽然很简单但我只是在帮助你。希望你会喜欢它,因为它是最简单的形式。 这是代码:

文件名:PopUpWindow.java用于java文件,我们也需要html文件。

对于applet,我们来看看它的popup.html

CODE:

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class PopUpWindow extends Applet{

public void init(){

Button open = new Button("open window");

add(open);

Button close = new Button("close window");

add(close);

 Frame f = new Frame("pupup win");

  f.setSize(200,200);




 open.addActionListener(new ActionListener() {

                 public void actionPerformed(ActionEvent e) {
                     if(!f.isShowing()) {
                         f.setVisible(true);
                     }


                 }

              });
 close.addActionListener(new ActionListener() {

                 public void actionPerformed(ActionEvent e) {
                     if(f.isShowing()) {
                        f.setVisible(false);
                     }

                 }

              });

 }

}




/*
<html>

<body>

<APPLET CODE="PopUpWindow" width="" height="">

</APPLET>

</body>

</html>

*/


to run:
$javac PopUpWindow.java && appletviewer popup.html