gpt4 book ai didi

java - Applet 中的 SWT FileDialog 不显示

转载 作者:行者123 更新时间:2023-11-30 05:00:35 25 4
gpt4 key购买 nike

我不是 SWT 用户,但我需要它在我正在开发的 JApplet 中的功能之一:FileDialog。当我使用浏览器运行小程序时,我遇到了问题:不会弹出对话框,不会引发异常,什么也不会发生(除了窗口闪烁)。

Applet 仅适用于 Windows 用户,这是要求。

当我在 Eclipse 中从 appletviewer 运行相同的 applet 时,它工作正常。

我将所有 SWT 库添加到 Windows 的 PATH 中,因此它们应该可用于 VM。我在互联网上搜索,但找不到任何可以帮助我的东西。

我正在使用的代码:

final Display display = new Display();
final java.awt.Canvas awtParent = new java.awt.Canvas();

applet.getApplet().add(awtParent);

final Shell swtParent = SWT_AWT.new_Shell(display, awtParent);
try {
FileDialog dialog = new FileDialog(swtParent, SWT.OPEN);
dialog.setFilterExtensions(getMediaFilesExtensions());
dialog.setFilterNames(new String[] {"All files", "Media Files"});
String fileName = dialog.open();
File[] files = null;
if (fileName != null) {
files = new File[] { new File(dialog.getFileName()) };
}
return files;
} finally {
display.syncExec(new Runnable () {
public void run () {
if (swtParent != null && !swtParent.isDisposed ()) swtParent.dispose ();
display.dispose ();
applet.getApplet().remove(awtParent);
}
});
}

当用户单击按钮时调用此代码。应用程序的其余部分是基于 Swing 的。

我在 Windows 7 上使用 SWT 3.7.1。所有DLL都在java.library.path中(Windows中为%PATH%)

你有什么建议吗?

顺便说一句:使用原生 L&F 的 Swing 不是一个选择。我需要 native 文件选择对话框。

这是我创建的可运行的演示小程序。它需要:swt-win32-x86-3.7.jar。

package com.applet;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Arrays;

import javax.swing.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.*;

public class DemoApplet extends JApplet {
@Override
public void init() {
JPanel mainPanel = new JPanel();
JButton button = new JButton(new AbstractAction("Select files") {
@Override
public void actionPerformed(ActionEvent e) {
File[] filesNative = getFilesNative();
String msg = "Selected files: " + Arrays.toString(filesNative);
JOptionPane.showMessageDialog(null, msg);
}
});
mainPanel.add(button, BorderLayout.CENTER);
add(mainPanel);
super.init();
}

private File[] getFilesNative() {
final Display display = new Display();
final java.awt.Canvas awtParent = new java.awt.Canvas();

add(awtParent);

final Shell swtParent = SWT_AWT.new_Shell(display, awtParent);
try {
FileDialog dialog = new FileDialog(swtParent, SWT.OPEN);
dialog.setFilterNames(new String[] {"All files", "Media Files"});
String fileName = dialog.open();
File[] files = null;
if (fileName != null) {
files = new File[] { new File(dialog.getFileName()) };
}
return files;
} finally {
display.syncExec(new Runnable () {
public void run () {
if (swtParent != null && !swtParent.isDisposed ()) swtParent.dispose ();
display.dispose ();
remove(awtParent);
}
});
}
}
}

最佳答案

如果您想在小程序中创建对话框,您必须找到父框架。我最近也遇到了这个问题,发现这很有用:http://www.jguru.com/faq/view.jsp?EID=27423

public Frame findParentFrame(){
java.awt.Component c = getParent();

while(true) {
if(c instanceof Frame)
return (Frame)c;
c = c.getParent();
}
}

这将为您提供父框架,并将其传递给对话框构造函数:

FileDialog fd = new FileDialog(findParentFrame(), "Save Building", FileDialog.SAVE);

请注意,某些浏览器可能会阻止此弹出窗口。

关于java - Applet 中的 SWT FileDialog 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6843286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com