弹出消息框

gym*_*ode 64 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;
}
Run Code Online (Sandbox Code Playgroud)

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

String st = "Welcome";
JOptionPane.showMessageDialog(null, st);
Run Code Online (Sandbox Code Playgroud)

Tro*_*eph 141

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

要使用此方法调用:

ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE");
Run Code Online (Sandbox Code Playgroud)

javafx.scene.control.Alert

有关如何使用JavaFX对话框的深入描述,请参阅:code.makery的JavaFX Dialogs(官方).它们比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();
    }
}
Run Code Online (Sandbox Code Playgroud)

要记住的一件事是JavaFX是一个单线程GUI工具包,这意味着应该直接从JavaFX应用程序线程调用此方法.如果你有另一个线程正在工作,需要一个对话框然后看到这些问题和答案:JavaFX2:我可以暂停后台任务/服务吗?Platform.Runlater和Task Javafx.

要使用此方法调用:

ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE");
Run Code Online (Sandbox Code Playgroud)

要么

ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE", "HEADER MESSAGE");
Run Code Online (Sandbox Code Playgroud)


小智 27

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

JOptionPane.showMessageDialog(null, 
                              "ALERT MESSAGE", 
                              "TITLE", 
                              JOptionPane.WARNING_MESSAGE);
Run Code Online (Sandbox Code Playgroud)

null将它放在屏幕中间.在警报信息下放任何引号.标题显然是标题,最后一部分将其格式化为错误消息.如果你想要一个常规消息,只需用它替换它PLAIN_MESSAGE.它在很多方面都很好用,主要是出错.


cor*_*ttk 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();
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

完整的类也有调试和警告方法,但我为了简洁而剪切它们,无论如何你得到了主要观点.您可以使用公共静态布尔值isDebugEnabled来抑制调试消息.如果操作正确,优化器将(几乎)从生产代码中删除这些方法调用.请参阅:http://c2.com/cgi/wiki?ConditionalCompilationInJava

干杯.基思.