我如何从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
}
}其他菜单项也是操作,正如您所看到的,我目前在CloseTabButton类中所做的事情非常令人沮丧,而且很可能是编写代码的错误方式。有更简单的方法来做我正在做的事吗?
发布于 2015-07-31 02:08:43
我可能做的第一件事是向ActionListener提供CloseTabButton支持,例如.
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"中的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,然后对JMenuItem和JTabbedPane使用相同的操作。
“核心”问题是如何以统一的方式识别“当前”选项卡和文档。
https://stackoverflow.com/questions/31736888
复制相似问题