【发布时间】: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