我的应用程序与第 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/