弹出消息框 [英] Popup Message boxes

查看:100
本文介绍了弹出消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何在我的方法中对弹出消息框进行编码。

I am unsure of how to code popup message box in my methods.

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 在我的方法中但不起作用:

I have tried to use JOptionPane in my method but it does not work:

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


推荐答案

javax.swing.JOptionPane



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

javax.swing.JOptionPane

Here is the code to a method I call whenever I want an information box to pop up, it hogs the screen until it is accepted:

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 ,对话框将会出现而是出现在组件的中心。

The first JOptionPane parameter (null in this example) is used to align the dialog. null causes it to center itself on the screen, however any java.awt.Component can be specified and the dialog will appear in the center of that Component instead.

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

I tend to use the titleBar String to describe where in the code the box is being called from, that way if it gets annoying I can easily track down and delete the code responsible for spamming my screen with infoBoxes.

要使用此方法,请致电:

To use this method call:

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






javafx.scene.control.Alert



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


javafx.scene.control.Alert

For a an in depth description of how to use JavaFX dialogs see: JavaFX Dialogs (official) by code.makery. They are much more powerful and flexible than Swing dialogs and capable of far more than just popping up messages.

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

As above I'll post a small example of how you could use JavaFX dialogs to achieve the same result

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:我可以暂停后台任务/服务吗?平台。 Runlater和Task Javafx

One thing to keep in mind is that JavaFX is a single threaded GUI toolkit, which means this method should be called directly from the JavaFX application thread. If you have another thread doing work, which needs a dialog then see these SO Q&As: JavaFX2: Can I pause a background Task / Service? and Platform.Runlater and Task Javafx.

要使用此方法,请致电:

To use this method call:

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

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

这篇关于弹出消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆