繁体   English   中英

如何从Java中的其他类调用Action

[英]How to call an Action from a different class in Java

我如何从Java中的另一个类调用Action? 我在线获得了一个CloseTabButton类,允许每个JTabbedPane上有一个简单的关闭标签按钮,但是当关闭标签时,我想根据信息弹出一个对话框(如果文件没有保存,请保存,等等) 。 这是文件:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class CloseTabButton extends JPanel implements ActionListener {
    private JTabbedPane pane;
    public CloseTabButton(JTabbedPane pane, int index) {
        this.pane = pane;
        setOpaque(false);

        // CloseIcon class just had a button with an x painted on it
        Icon closeIcon = new CloseIcon();
        JButton close = new JButton(closeIcon);

        close.setPreferredSize(new Dimension(closeIcon.getIconWidth(), closeIcon.getIconHeight()));
        close.addActionListener(this);

        add(new JLabel(pane.getTitleAt(index), pane.getIconAt(index), JLabel.LEFT));
        add(close);

        pane.setTabComponentAt(index, this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int i = pane.indexOfTabComponent(this);

        String fileName = pane.getToolTipTextAt(i);

        // Where I want to ask if user wants to save, etc.
        if (fileName == "Untitled") {
            // Do stuff
        }

        pane.remove(i); // Removes the tab

        // If tab count < 1, then disable the save and save as buttons on menu
        if (pane.getTabCount() < 1) {
            JFrame frame = (JFrame) pane.getParent().getParent().getParent().getParent().getParent(); // Yes, there is that many in my code to get the parent JFrame

            int menuCount = frame.getJMenuBar().getMenuCount();

            for (int a = 0; a < menuCount; a++) {
                int itemCount = frame.getJMenuBar().getMenu(a).getItemCount();

                for (int b = 0; b < itemCount; b++) {
                    Component component = frame.getJMenuBar().getMenu(a).getMenuComponent(b);

                    if (!(component instanceof JSeparator)) {
                        // Not a seperator
                        String itemName = frame.getJMenuBar().getMenu(a).getItem(b).getAccessibleContext().getAccessibleName();
                        if (itemName == "Save As..") {
                            frame.getJMenuBar().getMenu(a).getItem(b).setEnabled(false);
                        }
                    }
                }
            }
        }
    }
}

在我的主要课程中,我有如下列出的行动:

static Action Close = new AbstractAction("Close") {
    public void actionPerformed(ActionEvent e) {
        closeCurrentWindow(); // function that will close tab
    }
}

其他菜单项也是Actions,正如您所看到的,我目前在CloseTabButton类中所做的事情非常令人沮丧,而且很可能是错误的编码方式。 有没有更简单的方法来做我正在做的事情?

我可能做的第一件事就是为CloseTabButton提供ActionListener支持,例如......

public class CloseTabButton extends JPanel {

    private JTabbedPane pane;

    public CloseTabButton(JTabbedPane pane, int index) {
        this.pane = pane;
        setOpaque(false);

        // CloseIcon class just had a button with an x painted on it
        Icon closeIcon = new CloseIcon();
        JButton close = new JButton(closeIcon);

        close.setPreferredSize(new Dimension(closeIcon.getIconWidth(), closeIcon.getIconHeight()));
        close.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                fireActionPerformed();
            }

        });

        add(new JLabel(pane.getTitleAt(index), pane.getIconAt(index), JLabel.LEFT));
        add(close);

        pane.setTabComponentAt(index, this);
    }

    public void addActionListener(ActionListener listener) {
        listenerList.add(ActionListener.class, listener);
    }

    public void removeActionListener(ActionListener listener) {
        listenerList.remove(ActionListener.class, listener);
    }

    protected void fireActionPerformed() {
        ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
        ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "You could provide you own action command for each tab here");
        for (ActionListener listener : listeners) {
            listener.actionPerformed(evt);
        }
    }

}

基本上,现在允许您将自己的ActionListener注册到CloseTabButton

接下来,这个, fileName == "Untitled" ,不是你在Java中比较String的方式,你应该使用更像"Untitled".equals(fileName)

如果你的菜单是基于实际的Action ,那么你可以简单地自己禁用Action 这需要一点点工作,但是现在你正在做的“猜测”工作要少得多。

基本上,您将监视JTabbedPane本身,监视所选选项卡的更改并更新各个Action的状态

您可以通过多种方式执行此操作,例如将JTabbedPane的引用传递给Action以便它们可以执行自己的监视(但我会使用某种管理接口,可以更轻松地向Action提供信息和直接解耦代码和对JTabbedPane的依赖,然后你可以自由地使用JInternalFrame代替)。

你可以有一个“菜单管理器”,它做了类似的工作,监视对文档容器的更改,并根据当前状态更改菜单Action的状态,作为示例

更新

如果您正在使用Action API(我建议使用),那么您可以简单地执行类似...

public class CloseTabButton extends JPanel {

    private JTabbedPane pane;

    public CloseTabButton(JTabbedPane pane, Action action, int index) {
        this.pane = pane;
        setOpaque(false);

        // CloseIcon class just had a button with an x painted on it
        Icon closeIcon = new CloseIcon();
        JButton close = new JButton(action);
        close.setIcon(closeIcon);
        close.setPreferredSize(new Dimension(closeIcon.getIconWidth(), closeIcon.getIconHeight()));

        add(new JLabel(pane.getTitleAt(index), pane.getIconAt(index), JLabel.LEFT));
        add(close);

        pane.setTabComponentAt(index, this);
    }

}

传递Action以进行关闭操作,然后对JMenuItemJTabbedPane使用相同的操作。

“核心”问题将是如何以统一的方式识别“当前”选项卡和文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM