gpt4 book ai didi

java - 用户单击菜单项后如何保持 SWT 菜单打开?

转载 作者:搜寻专家 更新时间:2023-11-01 02:50:23 25 4
gpt4 key购买 nike

我有一个带有复选框菜单项菜单的 Eclipse RCP/SWT 应用程序。

我希望能够在单击其他地方关闭菜单之前选中/取消选中多个项目。但是,默认的 SWT 行为是单击后关闭菜单。

我已经实现了以下被黑客入侵的解决方案,该解决方案有效,但肯定不够优雅,并且可能无法在所有平台或所有情况下正常工作。所以我对一种更简单的技术非常感兴趣(如果存在的话)。

下面的代码应该可以在 eclipse 中直接编译和运行(抱歉太长了,它是我能创建的最短的独立示例):

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener2;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;

public class MenuTest
{
public static void main( String[] args )
{
// create a SWT Display and Shell
final Display display = new Display( );
Shell shell = new Shell( display );
shell.setText( "Menu Example" );

// create a jface MenuManager and Menu
MenuManager popupMenu = new MenuManager( );
Menu menu = popupMenu.createContextMenu( shell );
shell.setMenu( menu );

// create a custom listener class
final PopupListener listener = new PopupListener( shell, menu );

// attach the listener to the Manager, Menu, and Shell (yuck!)
popupMenu.addMenuListener( listener );
menu.addListener( SWT.Show, listener );
shell.addListener( SWT.MouseDown, listener );

// add an item to the menu
popupMenu.add( new Action( "Test", Action.AS_CHECK_BOX )
{
@Override
public void run( )
{
System.out.println( "Test checked: " + isChecked( ) );

listener.keepMenuVisible( );
}
} );

// show the SWT shell
shell.setSize( 800, 800 );
shell.setLocation( 0, 0 );
shell.open( );
shell.moveAbove( null );

while ( !shell.isDisposed( ) )
if ( !display.readAndDispatch( ) ) display.sleep( );

return;
}

public static class PopupListener implements Listener, IMenuListener2
{
Menu menu;
Control control;
Point point;

public PopupListener( Control control, Menu menu )
{
this.control = control;
this.menu = menu;
}

@Override
public void handleEvent( Event event )
{
// when SWT.Show events are received, make the Menu visible
// (we'll programmatically create such events)
if ( event.type == SWT.Show )
{
menu.setVisible( true );
}
// when the mouse is clicked, map the position from Shell
// coordinates to Display coordinates and save the result
// this is necessary because there appears to be no way
// to ask the Menu what its current position is
else if ( event.type == SWT.MouseDown )
{
point = Display.getDefault( ).map( control, null, event.x, event.y );
}
}

@Override
public void menuAboutToShow( IMenuManager manager )
{
// if we have a saved point, use it to set the menu location
if ( point != null )
{
menu.setLocation( point.x, point.y );
}
}

@Override
public void menuAboutToHide( IMenuManager manager )
{
// do nothing
}

// whenever the checkbox action is pressed, the menu closes
// we run this to reopen the menu
public void keepMenuVisible( )
{
Display.getDefault( ).asyncExec( new Runnable( )
{
@Override
public void run( )
{
Event event = new Event( );
event.type = SWT.Show;
event.button = 3;

menu.notifyListeners( SWT.Show, event );
if ( point != null )
{
menu.setLocation( point.x, point.y );
}
}
} );
}
}
}

最佳答案

我在 Win7 32 位和 eclipse 4.2 上试过你的代码。不幸的是,它出现了问题并且在闪烁。无论如何,这是另一种变体。在我看来,您必须至少使用两个监听器,一个用于您的菜单项,在任何情况下都是需要的,另一个用于获取菜单的坐标:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class TestMenu
{
private static Point point;

public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));

final Menu menu = new Menu(shell);
MenuItem item = new MenuItem(menu, SWT.CHECK);
item.setText("Check 1");
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e)
{
if(point == null)
return;
menu.setLocation(point);
menu.setVisible(true);
}
});

shell.addMenuDetectListener(new MenuDetectListener() {
public void menuDetected(MenuDetectEvent e) {
point = new Point(e.x, e.y);
}
});

shell.setMenu(menu);

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}


更新

感谢@Baz,请参阅下面的评论。

Tried this on Linux 32bit with eclipse 3.6.2 and unfortunately it doesn't seem to work.

更新 2(通过 ulmangt)

下面是对您的解决方案的修改,它适用于 64 位 Windows 7 和 64 位 Ubuntu Linux。

org.eclipse.swt.widgets.Menu 类有一个包保护字段,它确定菜单位置是否已设置。如果没有,至少在 Linux 上,菜单会出现在鼠标单击下方。

因此要获得正确的行为需要使用反射将此 boolean 字段重置为 false。或者,可以处理并重新创建菜单。

最后,Linux 似乎喜欢 menu.setLocation( point )menu.setVisible( true )asyncExec block 组成.

import java.lang.reflect.Field;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class MenuTest
{
private static Point point;

public static void main( String[] args )
{
Display display = new Display( );
final Shell shell = new Shell( display );
shell.setLayout( new GridLayout( 1, false ) );

final Menu menu = new Menu( shell );
MenuItem item = new MenuItem( menu, SWT.CHECK );
item.setText( "Check 1" );
item.addSelectionListener( new SelectionAdapter( )
{
public void widgetSelected( final SelectionEvent e )
{
if ( point == null ) return;

Display.getDefault( ).asyncExec( new Runnable( )
{
@Override
public void run( )
{
menu.setLocation( point );
menu.setVisible( true );
}
} );
}
} );

shell.addMenuDetectListener( new MenuDetectListener( )
{
public void menuDetected( MenuDetectEvent e )
{
point = new Point( e.x, e.y );
}
} );

menu.addMenuListener( new MenuListener( )
{
@Override
public void menuHidden( MenuEvent event )
{
try
{
Field field = Menu.class.getDeclaredField( "hasLocation" );
field.setAccessible( true );
field.set( menu, false );
}
catch ( Exception e )
{
e.printStackTrace();
}
}

@Override
public void menuShown( MenuEvent event )
{
}
});

shell.setMenu( menu );

shell.open( );
while ( !shell.isDisposed( ) )
{
if ( !display.readAndDispatch( ) ) display.sleep( );
}
display.dispose( );
}
}

关于java - 用户单击菜单项后如何保持 SWT 菜单打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12288028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com