在我的代码中,我有一个菜单项,其动作由动作地图附加
(menuItem.setActionName("someString")
)。
在动作地图中,作为值,我使用已实现的AbstractAction
方法扩展actionPerformed
的类对象。我的问题是,如何将鼠标事件(点击menuItem
时发布的鼠标)的修饰符传递给actionPerformed
?我必须检查在actionPerformed
方法中是否使用ctrl执行操作。有关修饰符的信息在MouseEvent
中,但调用堆栈显示调用了菜单项上的doClick
,调用actionPerformed
(通过setPressed
)。在那一刻,没有传递来自MouseEvent
的修饰符的信息,因此丢失了所需的信息。
谢谢!
答案 0 :(得分:0)
尝试覆盖扩展processMouseEvent
的类的JMenuItem
。您可以MouseEvent
方法从processMouseEvent
获取修饰符。
答案 1 :(得分:0)
这是一个代码示例。
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public class SSCE extends javax.swing.JFrame {
private static final String ACTION_NAME = "action";
private Map<String, MyAction> actionMap = new HashMap<>();
/**
* Creates new form SSCE
*/
public SSCE() {
initComponents();
actionMap.put(ACTION_NAME, new MyAction(ACTION_NAME));
configAction();
}
private void configAction(){
jMenuItem1.setAction(actionMap.get(ACTION_NAME));
}
/**
* This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jMenu1.setText("jMenu1");
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SSCE.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SSCE.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SSCE.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SSCE.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SSCE().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
// End of variables declaration
private static class MyAction extends AbstractAction {
public MyAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
if (ActionEvent.CTRL_MASK == (ActionEvent.CTRL_MASK & e.getModifiers())) {
JOptionPane.showMessageDialog(null, "Run with ctrl mode");
} else {
JOptionPane.showMessageDialog(null, "Run with normal mode");
}
}
}
}
但是,我担心我错了。修改器通过(不知何故,但我不知道如何),但不是在调试模式下。我在NetBeans中测试了SSCE,我认为在调试中有一些时间依赖性使得修饰符不会被传递(你可以用上面的代码检查它。只需在actionPerformed方法的第一行设置断点)。它也适用于我的应用程序。所以我认为问题可以结束。