SWT中开发右键菜单
package menudemo;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RightMenu extends org.eclipse.swt.widgets.Composite {
private Menu m;
private MenuItem mi;
private MenuItem mj;
/**
* Auto-generated main method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void main(String[] args) {
showGUI();
}
/**
* Auto-generated method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void showGUI() {
Display display = Display.getDefault();
Shell shell = new Shell(display);
RightMenu inst = new RightMenu(shell, SWT.NULL);
Point size = inst.getSize();
shell.setLayout(new FillLayout());
shell.layout();
if(size.x == 0 && size.y == 0) {
inst.pack();
shell.pack();
} else {
Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
shell.setSize(shellBounds.width, shellBounds.height);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public RightMenu(org.eclipse.swt.widgets.Composite parent, int style) {
super(parent, style);
initGUI();
}
private void initGUI() {
try {
this.setLayout(new FormLayout());
this.setSize(385, 180);
m=new Menu(getShell(),SWT.POP_UP);
mi=new MenuItem(m,SWT.PUSH);
mj=new MenuItem(m,SWT.CHECK);
getShell().setMenu(m);
this.layout();
} catch (Exception e) {
e.printStackTrace();
}
}
}
大家给看看,代码有什么问题
编译能通过,右键菜单弹不出来
[解决办法]
public class RightMenu{
.................
.................
public RightMenu(org.eclipse.swt.widgets.Composite parent, int style) {
super(parent, style);
this.addMouseListener(new MouseListener(){
public void mouseDoubleClick(MouseEvent e){}
public void mouseDown(MouseEvent e){}
public void mouseUp(MouseEvent e) {
updateMenu(e.button==3);
}
});
initGUI();
}
public void updateMenu(boolean visible){
m.setVisible(visible);
}
...................
..................
}