Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何以编程方式模拟复合材料背景上的按钮点击?_Java_User Interface_Click_Swt - Fatal编程技术网

Java 如何以编程方式模拟复合材料背景上的按钮点击?

Java 如何以编程方式模拟复合材料背景上的按钮点击?,java,user-interface,click,swt,Java,User Interface,Click,Swt,我需要以编程方式模拟复合材料背景上的按钮单击。特别是:在复合材料的左上角。好的,这是主要形式: 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.Sh

我需要以编程方式模拟复合材料背景上的按钮单击。特别是:在复合材料的左上角。

好的,这是主要形式:

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 ( );

    }
}
这是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 );
        }

    }

}

您必须在SWT应用程序中使用swing才能成功。

您可以使用
按钮。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());
    }
});

另一个选项是使用反射调用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(); }    
org.eclipse.swt.widgets.Button-Button=(org.eclipse.swt.widgets.Button)Button.this;
尝试
{                           
ClassbuttonClass=button.getClass().getSuperclass();
方法=按钮class.getDeclaredMethod(“单击”);
方法setAccessible(true);
方法调用(按钮);
}
catch(SecurityException e){e.printStackTrace();}
catch(NoSuchMethodException){e.printStackTrace();}
catch(IllegalArgumentException e){e.printStackTrace();}
catch(IllegalAccessException e){e.printStackTrace();}
catch(InvocationTargetException e){e.printStackTrace();}

这不是我想要的。我想以编程方式生成鼠标单击事件,而无需用户交互。如果用户单击了0,0位置,这里会显示messagebox…好的,我找到了一些东西。在swing中有一个机器人类,它实现了您想要的东西。特雷:这是密码。唯一的问题是您必须正确设置类的x和y字段。