创建自定义阻止Java Swing提示 [英] Creating a custom blocking Java Swing prompt

查看:69
本文介绍了创建自定义阻止Java Swing提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已解决.

我正在开发基于Java Swing的项目,外观应用程序的感觉是完全定制的.我们试图在整个程序中保持一致的外观,并且默认的Java对话框窗口不同.

I'm am developing a Java Swing based projected, and the look & feel of the application is completely customized. We are trying to maintain a consistent appearance throughout the program, and the default Java dialog windows are not the same.

当前问题要求控件禁止调用用户提示.与JOptionPane.showConfirmDialog()相似,在这种情况下,静态调用将产生一个窗口,并暂停程序的流程,直到用户选择一个选项为止.它还返回选项的值.请注意,GUI本身并没有在逻辑上被锁定,但是用户无法与它的其余部分进行交互.

The current issue requires a control blocking call to the user prompt. Similar to JOptionPane.showConfirmDialog() In this case, the static call produces a window, and halts the flow of the program until the user selects an option. It also returns the value of the option. Note that the GUI itself is not locked up logically, but the user cannot interact with the rest of it.

int n = JOptionPane.showConfirmDialog(this,
    "Are you sure?",
    "Confirm"
    JOptionPane.YES_NO_OPTION);

我想使用自定义外观并使用字符串来复制此功能.理想情况下,我的代码如下所示:

I would like to duplicate this functionality with a customized appearance, and using a string. Ideally, my code would appear as follows:

String result = CustomPrompt.showPrompt(this,
    "Please enter your name", //Text
    "Prompt.",                //Title
    "John Smith");            //Default

通常将其用于密码输入,并且我知道密码的返回类型有所不同,但这只是一个示例.这可以通过在几个类中使用一系列按钮和事件侦听器来实现,但是代码的可读性和应用程序的可靠性下降.

This will generally be used for password input, and I understand the return type of a password is different, but this is an example. This can be accomplished by using a series of button and event listeners, in several classes, but the readability of the code and reliability of the application diminishes.

该框架将通过NetBeans构建并从那里进行定制.我确实知道Swing中已经存在这样的提示,但看起来是&感觉完全不同.

The Frame would be built via NetBeans and customized from there. I do understand that such a prompt already exists in Swing, but it's look & feel is completely and entirely different.

问题总结为:如何使用自定义框架以阻止方式提示用户输入.

The question summarized: How to use a customized frame to prompt the user for input in a blocking manner.

此问题的解决方案如下:

The solution to this problem is as follows:

public class PromptForm extends JDialog
{
    transient char[] result;

    public char[] getResult()
    {
        return result;
    }
    public PromptForm(Frame parent)
    {
        super(parent, true);
        initComponents();
    }
    public void prompt()
    {
        this.setVisible(true);
    }
    public static char[] promptForPassword(Frame parent)
    {
        PromptForm pf = new PromptForm(parent);
        pf.prompt();
        return pf.getResult();
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
        result = jPasswordField1.getPassword();
        setVisible(false);
        dispose();
    }
    private void initComponents() {...}

    private javax.swing.JButton jButton1;
    private javax.swing.JPasswordField jPasswordField1;
}

致电者:

    char [] ret = PromptForm.promptForPassword(this);
    JOptionPane.showMessageDialog(this, new String(ret), "Neat", JOptionPane.QUESTION_MESSAGE);

推荐答案

使CustomPrompt扩展

Make CustomPrompt extends JDialog, have it's constructor call super passing the owner Window and the desired ModalityType.

public class CustomPrompt extends JDialog {

  public static String showPrompt(Window parent, String title, String text, 
         String defaultText) {
    final CustomPrompt prompt = new CustomPrompt(parent);
    prompt.setTitle(title);
    // set other components text
    prompt.setVisible(true);

    return prompt.textField.getText();
  }

  private JTextField textField;

  // private if you only want this prompt to be accessible via constructor methods
  private CustomPrompt(Window parent) {
    super(parent, Dialog.DEFAULT_MODALITY_TYPE); 
    // Java >= 6, else user super(Frame, true) or super(Dialog, true);
    initComponents(); // like Netbeans
  }

  // initComponents() and irrelevant code. 
}

这篇关于创建自定义阻止Java Swing提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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