【问题标题】:FileDialog not working in JAppletFileDialog 在 JApplet 中不起作用
【发布时间】:2012-04-22 16:41:09
【问题描述】:

我正在将应用程序从独立 (JFrame) 转换为 Applet (JApplet)。我已将 FileDialog 构造函数中的参数从父框架更改为 getContentPane,但这无法正常工作。我得到了类 Cast 异常,说不能将 Jpanel 转换为 Frame。

请找到 SSCCE。请帮我解决这个问题。

public class SampleApplet extends JApplet{

protected JButton countryButton = new JButton("Select");

public synchronized void init()
{
    this.setBounds(new Rectangle(350,350));
    this.add(countryButton);


    countryButton.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent arg0) {
            getCountries();
            getCountries();             
        }

    });
}

protected void getCountries() {
    FileDialog fileDialog_ImageIn = new FileDialog((Frame) getContentPane() ,"Select a GIF file", FileDialog.LOAD);
    fileDialog_ImageIn.setVisible(true);
    if (fileDialog_ImageIn.getFile() == null) 
        return;
    else
        System.out.println(fileDialog_ImageIn.getDirectory() + fileDialog_ImageIn.getFile());
}

}

【问题讨论】:

  • 小程序已经过时了,我认为你应该看看 Java Web start
  • 如果你想用 Swing 进行文件对话框,我会看看JFileChooser。混合使用 Swing 和 AWT 组件可能会导致错误行为。
  • 感谢 Jeffery 提醒。我之前也犯过同样的错误。我会试试 JfileChooser
  • @Jeffrey 通常是好的建议,但请注意,这是少数(极端情况)情况之一,其中将 Swing 与 AWT 混合导致的典型“奇怪渲染”不会出现。 ;)
  • 我尝试使用 JFilechooser 并且它工作正常谢谢。

标签: java swing jfilechooser japplet


【解决方案1】:

我正在将应用程序从独立 (JFrame) 转换为 Applet (JApplet)。

不要那样做!相反,从使用Java Web Start 的链接启动框架。它将提供更好的用户体验,并且更易于开发和部署。

顺便说一句

  1. 基于 Swing 的 JFileChooser 比基于 AWT 的 FileDialog 更好(更可配置等)。
  2. 需要信任使用 JWS 启动的小程序或框架才能使用其中任何一个类。如果应用程序。 JNLP API 是使用 JWS 启动的,JNLP API 提供了一种访问文件系统的方法,该方法甚至适用于完全沙盒代码,尽管它需要进行一些更改,因为它使用的机制不是我上面提到的 2 个组件中的任何一个点 1. 这是一个小demo. of the JNLP file services。尝试沙盒版本,看看它是否适用于您的用例。代码链接位于启动按钮的右侧。

【讨论】:

  • 感谢您的建议,我会试试这个
  • 哪个“这个” - JWS?您是否要将其扩展到使用 JNLP API 打开文件? (请注意,如果您从 IDE 运行代码,它通常不会有安全沙箱 - 一旦代码嵌入网页或使用 JWS 启动,它将有一个沙箱。)
猜你喜欢
  • 2011-10-08
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多