如何在非组件类中使用 JFileChooser.showOpenDialog() ?
我有一个包含 JMenuBar 的 Java GUI 项目,我刚刚添加了一个 JToolBar。在以前的版本中,事件是在扩展 JMenuBar 的同一类中实现的。我发现它很蹩脚,并将事件移动到另一个扩展 AbstractAction 的类中。我的目标是集中所有常见事件,使它们对不同的源(JMenuBar、JToolBar 等)做出反应。但是,我对 JFileChooser.showOpenDialog() 方法有问题。此方法将对话框的父组件作为参数。 如果我这样做:
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.event.*;
public class ActionUsuels extends AbstractAction
{
private String nameAction;
/** Instance de MyFileChooser pour explorer les dossiers/fichiers*/
private MyFileChooser fc;
/** Instance d'OpenSave qui contient les algorithmes d'ouverture/sauvegarde*/
private OpenSave openSave;
ActionUsuels(String inName, String inPathIcon)
{
nameAction = inName;
putValue(Action.NAME, inName);
putValue(Action.SMALL_ICON, new ImageIcon(inPathIcon));
putValue(Action.SHORT_DESCRIPTION, inName);
this.fc = new MyFileChooser();
this.openSave = new OpenSave(Panneau.getUnivers());
}
public void actionPerformed(ActionEvent e)
{
// Evénement nouveau projet
if(nameAction == "OPEN_PROJECT")
{
fc.ContMode();
fc.refresh();
int returnVal = fc.showOpenDialog(ActionUsuels.this);
if (returnVal == MyFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
openSave.OpenCont(file);
}
}
static ActionUsuels actionInactive;
}
我会收到以下错误:
showOpenDialog(组件)方法 JFileChooser 类型中不是 适用于论据 (ActionUsuels)。
我想这是正常的,因为 ActionUsuels 不扩展任何 JComponent 类。但我怎样才能超越它呢?我想做的是不好的做法吗?我的目的是编写一次事件并能够从任何组件调用它们。
为了让您了解我在做什么,我在 Menu 类中添加了以下内容:
actions = new ActionUsuels[nameActions.length];
for(int i = 0; i < nameActions.length; i++)
{
actions[i] = new ActionUsuels(nameActions[i], pathIcons[i]);
}
file_menu.add(actions[0]);
file_menu.addSeparator();
file_menu.add(actions[1]);
每个项目都与操作名称、图标和合适的事件相关联!
有什么想法吗?
谢谢 !
I have a Java GUI project containing a JMenuBar and I just added a JToolBar. In the previous version, the events were implemented in the same class that extends the JMenuBar. I found it lame and moved the events in another class that extends AbstractAction. My aim is to centralize all the common events to make them react to different sources (JMenuBar, JToolBar etc.). But, I have a problem with the JFileChooser.showOpenDialog() method. This method takes as an argument the parent component for the dialog.
If I do this :
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.event.*;
public class ActionUsuels extends AbstractAction
{
private String nameAction;
/** Instance de MyFileChooser pour explorer les dossiers/fichiers*/
private MyFileChooser fc;
/** Instance d'OpenSave qui contient les algorithmes d'ouverture/sauvegarde*/
private OpenSave openSave;
ActionUsuels(String inName, String inPathIcon)
{
nameAction = inName;
putValue(Action.NAME, inName);
putValue(Action.SMALL_ICON, new ImageIcon(inPathIcon));
putValue(Action.SHORT_DESCRIPTION, inName);
this.fc = new MyFileChooser();
this.openSave = new OpenSave(Panneau.getUnivers());
}
public void actionPerformed(ActionEvent e)
{
// Evénement nouveau projet
if(nameAction == "OPEN_PROJECT")
{
fc.ContMode();
fc.refresh();
int returnVal = fc.showOpenDialog(ActionUsuels.this);
if (returnVal == MyFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
openSave.OpenCont(file);
}
}
static ActionUsuels actionInactive;
}
I get the following error :
The method showOpenDialog(component)
in the type JFileChooser is not
applicable for the arguments
(ActionUsuels).
I guess this is normal because ActionUsuels doesn't extend any JComponent class. But how can I overpass that ? Is what I'm trying to do a bad practice ? My intention is to write the events once and be able to call them from any component.
Just to make you understand what I'm doing, I have this in the Menu class:
actions = new ActionUsuels[nameActions.length];
for(int i = 0; i < nameActions.length; i++)
{
actions[i] = new ActionUsuels(nameActions[i], pathIcons[i]);
}
file_menu.add(actions[0]);
file_menu.addSeparator();
file_menu.add(actions[1]);
Every item is associated to the name of the action, an icon and the suitable event !
Any idea ?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,传递给 JDialogs 的父类是应用程序的主 JFrame。除此之外,这允许对话框位于应用程序窗口的中心。
希望您的操作类能够访问主框架并可以传递对其的引用。实现此目的的一种方法可能是将主框架作为参数传递给
ActionUsuels
构造函数。否则,
null
也是有效的父规范。如果给定null
,对话框将位于屏幕中央,但无论如何通常都可以正常工作。好机会! :)
Usually, the parent class passed to JDialogs is the main JFrame of the application. Among other things, this allows for the dialog to be centered over the app's window.
Hopefully your action class will have access to the main frame and can pass a reference to it. One way to achieve this might be to pass the main frame as an argument to the
ActionUsuels
constructor.Failing that,
null
is also a valid parent specification. Givennull
, the dialog is centered on the screen but generally works OK anyway.Bonne chance! :)
这是一个坏主意:
您需要使用它:
== 运算符仅检查两个引用是否相等,而不检查它们指向的字符串是否相同。这就是 String 的 equals 方法的用途。这就是“浅”和“深”之间的区别。
您可以在这里看到它的实际效果:
This is a bad idea:
You need to use this instead:
The == operator only checks to see if two references are equal, not whether or not the Strings they point to are identical. That's what the equals method for String is written to do. It's the difference between "shallow" and "deep" equals.
You can see it in action here: