java - 如何以编程方式获取 JOptionPane 消息内容

标签 java swing swingutilities

我的应用程序与第 3 方独立应用程序集成,该应用程序将在单独的线程中打开 JOptionPane 对话框,并且我正在运行该线程来关闭所有打开的对话框。因此,在关闭之前,我需要获取在对话框上写入的消息。

我试图实现的示例主程序:

   public static void main(String[] args)throws Exception{
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    executor.scheduleAtFixedRate(() -> {
        Window[] possibleWindow = Window.getWindows();
        if (possibleWindow != null && possibleWindow.length > 0) {
            System.out.println("Found " + possibleWindow.length + "Window(s) " + possibleWindow[0].getClass().getSuperclass());
            for (int i = possibleWindow.length - 1; i >= 0; i--) {
                try {
                    Window window = possibleWindow[i];
                    //here where I need to get the dialog box message before closing it.
                    window.dispose();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }, 1, 1, TimeUnit.SECONDS);

    JOptionPane.showMessageDialog(null, "test !!!!");
}

最佳答案

如果我正确地理解了您的问题,您将创建 JOptionPane 对象并向它们发送消息;后来,你想知道你给他们的信息吗?

如果是这样,一个简单的解决方案是创建一个中心 map ,例如 Map<JOptionPane, String> 。每次创建新的 JOptionPane 时,您都会记住它(及其消息);以及清理后;您只需获取那些仍在运行的 JOptionPane 对象的消息即可。

关于java - 如何以编程方式获取 JOptionPane 消息内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38343879/

相关文章:

Java SwingUtilities.invokeLater() 拖放计时

java - Ant、Ivy 和 JUnit 找不到类 - build.xml 中有错误?

java - 使用 EMF 模型比较两个源代码文件

java - 应用程序崩溃(指定的子级已经有父级)

java - 为什么需要 Swing Utilities 以及如何使用它?

java - SwingUtilities invokeLater的主要目的

java - 主题错误 - 如何修复?

java - Java Swing 中 MouseMotionListener 的适当方法是什么?

java - 无法设置 java 小部件的位置

java - 如何在 Nimbus 外观和感觉中删除/设置 JTable 单元格中的 JTextArea 边框