如何在非组件类中使用 JFileChooser.showOpenDialog() ?

发布于 2024-08-15 19:58:43 字数 2145 浏览 6 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见 2024-08-22 19:58:43

通常,传递给 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. Given null, the dialog is centered on the screen but generally works OK anyway.

Bonne chance! :)

零時差 2024-08-22 19:58:43

这是一个坏主意:

if(nameAction == "OPEN_PROJECT")

您需要使用它:

if("OPEN_PROJECT".equals(nameAction))

== 运算符仅检查两个引用是否相等,而不检查它们指向的字符串是否相同。这就是 String 的 equals 方法的用途。这就是“浅”和“深”之间的区别。

您可以在这里看到它的实际效果:

String x = new String("foo");  // don't write code like this; just an example
String y = new String("foo");  // x and y are different reference values
System.out.println(x == y);    // prints "false"
System.out.println(x.equals(y)); // prints "true"

This is a bad idea:

if(nameAction == "OPEN_PROJECT")

You need to use this instead:

if("OPEN_PROJECT".equals(nameAction))

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:

String x = new String("foo");  // don't write code like this; just an example
String y = new String("foo");  // x and y are different reference values
System.out.println(x == y);    // prints "false"
System.out.println(x.equals(y)); // prints "true"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文