Java 限制纯消息JOptionPane中输入文本的大小

Java 限制纯消息JOptionPane中输入文本的大小,java,swing,joptionpane,Java,Swing,Joptionpane,我正在使用JOptionPane.showInputDialog方法创建一个对话框,该方法具有JOptionPane.PLAIN_消息选项类型。这将显示一个带有自由文本输入字段的确认弹出窗口。我需要文本最多256个字符,但在声明对话框时找不到任何选项来限制它 此时,我测试返回的字符串的长度,如果它太长,则截断它: private void showConfirmationPopup(Component parent) { String title = ""; String mes

我正在使用JOptionPane.showInputDialog方法创建一个对话框,该方法具有JOptionPane.PLAIN_消息选项类型。这将显示一个带有自由文本输入字段的确认弹出窗口。我需要文本最多256个字符,但在声明对话框时找不到任何选项来限制它

此时,我测试返回的字符串的长度,如果它太长,则截断它:

private void showConfirmationPopup(Component parent) {
    String title = "";
    String message = "";
    String defaultComment = "";
    // Open an Input Dialog with a Text field
    Object comment = JOptionPane.showInputDialog(
            parent,
            message, 
            title, 
            JOptionPane.PLAIN_MESSAGE,
            null,
            null,
            defaultComment);

    if(comment != null) {
        String inputComment = (String)comment;
        if(inputComment.length()>256) {
            inputComment = inputComment.substring(0, 256);
        }
        // ...
    }
}

我想知道是否可以在声明对话框时设置限制,或者是否有一个聪明的技巧来实现它,这样我就不必在声明之后进行检查了。

阅读上Swing教程的部分

它显示了如何编辑输入的文本,并在未输入有效数据时防止对话框关闭

编辑:

您可以尝试访问选项窗格的文本字段:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class OptionPaneInput2
{
    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

        JTextField label = new JTextField("How old are you?");
        label.addAncestorListener( new AncestorListener()
        {
            @Override
            public void ancestorAdded(AncestorEvent e)
            {
                Component component = e.getComponent();
                Container parent = component.getParent();
                JTextField textField = (JTextField)parent.getComponent(1);
                System.out.println("added");
            }

            @Override
            public void ancestorMoved(AncestorEvent e) {}

            @Override
            public void ancestorRemoved(AncestorEvent e) {}

        });


        //  Simple text field for input:

        String age = JOptionPane.showInputDialog(
            frame,
            label);

        frame.dispose();
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater( () -> createAndShowGUI() );
/*
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}
通过将消息指定为JLabel,可以在将标签添加到对话框中时通知您,然后您可以访问输入文本字段并将DocumentFilter添加到文本字段的文档中,并执行所需的编辑

有关限制显示字符数的筛选器,请参见上的Swing教程


如果这对您来说仍然太有创意,那么我建议您只需创建自己的定制JDialog。这确实是最好的方法。

简短的回答是:如果您使用方便的方法创建对话框,您就不能做您想做的事情。@SeanBright我恐怕您是对的:阅读后,它不是关于showInputDialog方法,而是关于创建自定义选项窗格和自定义对话框,这不是我真正想要的。谢谢你的意见。这不是我真正想要的是的,但是没有直接的方法访问选项窗格的内部。请参阅编辑以了解另一个hack。