java 如何以编程方式模拟复合背景上的按钮单击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7685400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to simulate programmatically button click on background of Composite?
提问by alhcr
I need to simulate programmatically button click on background of Composite. Specifically: on left-top corner of Composite.
我需要以编程方式模拟复合背景上的按钮单击。具体来说:在 Composite 的左上角。
采纳答案by Jordan Borisov
OK this is the main form :
好的,这是主要形式:
package test.src;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CompositeClicker
{
private static Display display;
private static Shell shell;
private static PComposite composite;
public static void main ( String [ ] args )
{
display = new Display ( );
shell = new Shell ( display );
shell.setLayout ( new GridLayout ( 1 , false ) );
shell.setLocation ( 100 , 50 );
shell.setSize ( 100 , 500 );
composite = new PComposite ( shell );
composite.setLayoutData ( new GridData ( SWT.FILL , SWT.FILL , true , true ) );
composite.setLayout ( new GridLayout ( 1 , false ) );
shell.open ( );
while ( !shell.isDisposed ( ) )
{
if ( !display.readAndDispatch ( ) )
display.sleep ( );
}
display.dispose ( );
}
}
And this is the PComposite class:
这是 PComposite 类:
package test.src;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import javax.swing.SwingUtilities;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Composite;
public class PComposite extends Composite
{
int x;
int y;
public PComposite ( Composite parent )
{
super ( parent , SWT.BORDER | SWT.EMBEDDED );
addPaintListener ( new PaintListener ( )
{
@Override
public void paintControl ( PaintEvent e )
{
Point p = new Point ( PComposite.this.getClientArea ( ).x + 11 , PComposite.this.getClientArea ( ).y + 12 );
Panel panel = new Panel ( );
panel.setBounds ( getShell ( ).getBounds ( ).x , getShell ( ).getBounds ( ).y + 25 , getShell ( ).getBounds ( ).width , getShell ( ).getBounds ( ).height );
SwingUtilities.convertPointToScreen ( p , panel );
x = p.x;
y = p.y;
try
{
trainer ( );
}
catch ( Exception exc )
{
exc.printStackTrace ( );
}
}
} );
addMouseListener ( new MouseAdapter ( )
{
@Override
public void mouseDown ( MouseEvent e )
{
System.out.println ( e.x + " " + e.y );
}
} );
}
private void trainer ( ) throws Exception
{
Robot trainer = new Robot ( );
trainer.mouseMove ( x , y );
int delay = 100;
int clicks = 1;
while ( clicks-- > 0 )
{
trainer.mouseMove ( x , y );
trainer.mousePress ( InputEvent.BUTTON1_MASK );
trainer.delay ( delay );
}
}
}
You have to use swing in your SWT application to success.
您必须在 SWT 应用程序中使用 Swing 才能成功。
回答by Vasil Lukach
You can use Button.notifyListeners(int eventType, Event event)
to simulate a SWT button click:
您可以使用Button.notifyListeners(int eventType, Event event)
模拟 SWT 按钮单击:
final Button setButton = new Button(composite, SWT.PUSH);
setButton.setText("Set");
setButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
// some code here
}
});
MenuItem clickMenuItem = new MenuItem(testMenu, SWT.PUSH);
clickMenuItem.setText("Click");
clickMenuItem.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
setButton.notifyListeners(SWT.Selection, new Event());
}
});
回答by Dinis Cruz
Another option is to use reflection to invoke the clickmethod of the org.eclipse.swt.widgets.Button class
另一种选择是使用反射调用org.eclipse.swt.widgets.Button类的click方法
org.eclipse.swt.widgets.Button button = (org.eclipse.swt.widgets.Button)Button.this;
try
{
Class<?>buttonClass = button.getClass().getSuperclass();
Method method = buttonClass.getDeclaredMethod("click");
method.setAccessible(true);
method.invoke(button);
}
catch (SecurityException e) { e.printStackTrace(); }
catch (NoSuchMethodException e) { e.printStackTrace(); }
catch (IllegalArgumentException e) { e.printStackTrace(); }
catch (IllegalAccessException e) { e.printStackTrace(); }
catch (InvocationTargetException e) { e.printStackTrace(); }