日期:2014-05-20  浏览次数:20885 次

java中e.getModifiers()的作用
public void initMusicModeControl() {
but_music.addMouseListener(new MouseAdapter() {

MyMenu popup_menuList = new MyMenu("播放曲目");

public void mouseClicked(MouseEvent e) {
int mod = e.getModifiers() & 4;
if (e.getButton() == MouseEvent.BUTTON1
&& e.getClickCount() == 2) {
int index = list.getSelectedIndex();
if (but_music.getActionCommand() == "true") {
changToMusicMode();
} else {
changToMoveMode(index);
}
} else if (mod != 0) {
System.out.println("Ok");

popup_menu.add(popup_menuList);
popup_menu.show(but_music, e.getX(), e.getY());
popupList.setSelectedIndex(list.getSelectedIndex());
popup_menuList.add(popupList);
}
}
});
}
上面代码中int mod = e.getModifiers() & 4的作用是什么,什么意思啊;
getModifiers()返回此类或接口以整数编码的 Java 语言修饰符是什么意思啊?

------解决方案--------------------
就是前面的修饰符,像public static之类的,只不过所有的修饰符都是不冲突的数字,这样一个数字就能代表多个修饰符,例如public 是 00000001而static 是 00000010,那么合在一起就是00000011 就代表public static了
具体的含义,请参见Modifier这个类
------解决方案--------------------
这个在Modifier类里面有定义的
Java code

/*
     * Access modifier flag constants from <em>The Java Virtual
     * Machine Specification, Second Edition</em>, tables 4.1, 4.4,
     * 4.5, and 4.7.
     */

    /**
     * The <code>int</code> value representing the <code>public</code> 
     * modifier.
     */    
    public static final int PUBLIC           = 0x00000001;

    /**
     * The <code>int</code> value representing the <code>private</code> 
     * modifier.
     */    
    public static final int PRIVATE          = 0x00000002;

    /**
     * The <code>int</code> value representing the <code>protected</code> 
     * modifier.
     */    
    public static final int PROTECTED        = 0x00000004;

    /**
     * The <code>int</code> value representing the <code>static</code> 
     * modifier.
     */    
    public static final int STATIC           = 0x00000008;

    /**
     * The <code>int</code> value representing the <code>final</code> 
     * modifier.
     */    
    public static final int FINAL            = 0x00000010;

    /**
     * The <code>int</code> value representing the <code>synchronized</code> 
     * modifier.
     */    
    public static final int SYNCHRONIZED     = 0x00000020;

    /**
     * The <code>int</code> value representing the <code>volatile</code> 
     * modifier.
     */    
    public static final int VOLATILE         = 0x00000040;

    /**
     * The <code>int</code> value representing the <code>transient</code> 
     * modifier.
     */    
    public static final int TRANSIENT        = 0x00000080;

    /**
     * The <code>int</code> value representing the <code>native</code> 
     * modifier.
     */    
    public static final int NATIVE           = 0x00000100;

    /**
     * The <code>int</code> value representing the <code>interface</code> 
     * modifier.
     */    
    public static final int INTERFACE        = 0x00000200;

    /**
     * The <code>int</code> value representing the <code>abstract</code> 
     * modifier.
     */    
    public static final int ABSTRACT         = 0x00000400;

    /**
     * The <code>int</code> value representing the <code>strictfp</code> 
     * modifier.
     */    
    public static final int STRICT           = 0x00000800;

    // Bits not (yet) exposed in the public API either because they
    // have different meanings for fields and methods and there is no
    // way to distinguish between the two in this class, or because
    // they are not Java programming language keywords
    static final int BRIDGE    = 0x00000040;
    static final int VARARGS   = 0x00000080;
    static final int SYNTHETIC = 0x00001000;
    static final int ANNOTATION= 0x00002000;
    static final int ENUM      = 0x00004000;