问题:当向 JTree 添加键绑定时,为什么我必须将每个动作都设为自己的类? 为什么我不能让每个动作都使用一个动作类? 为了理解我的问题/问题,让我先解释一下我下面的问题的简短自包含示例。
addKeyBindings(JTree tree) has the following 2 lines commented out.
//Action addsiblingnodeaction = new RightClickNodeAction("Add SiblingNode");
//Action addchildnodeaction = new RightClickNodeAction("Add ChildNode");
/*These use 1 action class, to do 2 different actions. (I want this because I
find the code to be less verbose)
(They're commented out because they don't work for keybindings)
(The reason I ask question, is because they DO work for JPopupMenu)*/
真正的问题是为什么上面的//注释掉的代码//不适用于键绑定?(在我的示例中,我包含了一个 JPopupMenu,以表明上述注释掉的代码适用于 JPopupMenu,并且在我看来也应该适用于键绑定)(我能够使键绑定工作的唯一方法是为每个动作,您可以在下面的示例代码中看到)
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.KeyStroke;
public class StackExchangeQuestion2 {
public static void main(String[] args){
StackExchangeQuestion2 workaround = new StackExchangeQuestion2();
//workaround = compiler didn't like me throwing constructor code in main
}//end main
StackExchangeQuestion2(){
JTree tree = new JTree();
initRightClickMenu(tree);//purpose of existance is to show Action Code is accurate
addKeyBindings(tree);
JFrame window = new JFrame();
window.getRootPane().setContentPane(tree);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle("Stack Exchange Question");
window.setSize(400,500);//variable parameters would be best
window.setVisible(true);
}//constructor
private void initRightClickMenu(JTree tree){
JPopupMenu rightclickmenu = new JPopupMenu();
Action addsiblingnodeaction = new RightClickNodeAction("Add SiblingNode");
Action addchildnodeaction = new RightClickNodeAction("Add ChildNode");
rightclickmenu.add(addsiblingnodeaction);
rightclickmenu.add(addchildnodeaction);
tree.setComponentPopupMenu(rightclickmenu);
}
private void addSiblingNode(){
System.out.println("sibling node added");}
private void addChildNode(){
System.out.println("child node added");}
private void addKeyBindings(JTree tree){
//Action addsiblingnodeaction = new RightClickNodeAction("Add SiblingNode");
Action addsiblingnodeaction = new AddSiblingNodeAction("Add SiblingNode");
tree.getActionMap().put("Add SiblingNode", addsiblingnodeaction);
tree.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "Add SiblingNode");
//Action addchildnodeaction = new RightClickNodeAction("Add ChildNode");
Action addchildnodeaction = new AddChildNodeAction("Add ChildNode");
tree.getActionMap().put("Add ChildNode", addchildnodeaction);
tree.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "Add ChildNode");
}//end addKeyBindins
private class AddSiblingNodeAction extends AbstractAction{
AddSiblingNodeAction(String name){super(name);}
public void actionPerformed(ActionEvent ae) { addSiblingNode(); }}
private class AddChildNodeAction extends AbstractAction{
AddChildNodeAction(String name){super(name);}
public void actionPerformed(ActionEvent ae) { addChildNode(); }}
private class RightClickNodeAction extends AbstractAction{
RightClickNodeAction(String name){super(name);}
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equals("Add SiblingNode"))addSiblingNode();
if(ae.getActionCommand().equals("Add ChildNode"))addChildNode(); }}
}//end class