Java - 在browser.execute中使用方法(脚本)

时间:2018-03-16 14:41:48

标签: javascript java eclipse swt

我有一个包含名称的数组的方法,我正在使用SWT浏览器和browser.execute()我想填写表单。

我的问题: 我想在generateName()部分中使用browser.execute()方法而不是TEST作为值。

方法

/**
 * generate name, lastname
 */
public void generateName() {
   Random generateName = new Random();
   String[] name = {"Liam", "Elias", "Kevin", "Julian", "Linus", "Levi", "Jonas", "Daniel", "Milan", "Maria", "Alexander", "Manfred", "Max", "Jens", "Dennis", "Oliver", "Jan", "Bob", "Henry"};
   String[] lastname = {"Meyer", "Müller", "Eilers", "Gerdes", "Schröder", "Janssen", "Ahlers", "Bruns", "Behrens", "Harms", "Schmidt", "Macke", "Kruse", "Becker", "Lange", "Suhr", "Schulte", "Weber", "Lampe"};

   // debug
   System.out.println(name[generateName.nextInt(19)] + " " + lastname[generateName.nextInt(19)]);
}

带列表器的按钮

/**
 * Create contents of the window.
 */
protected void createContents() {
    shell = new Shell();
    shell.setSize(800, 600);
    shell.setText("SWT Application");

    Browser browser = new Browser(shell, SWT.NONE);
    browser.setUrl("www.pseudopage.com/SignUp");
    browser.setBounds(10, 10, 780, 352);

    Button btnNewButton = new Button(shell, SWT.BORDER);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            browser.execute("document.getElementById('txtFirstName').value=\"TEST\"");
        }
    });
    btnNewButton.setBounds(10, 374, 94, 28);
    btnNewButton.setText("Generate");

}

1 个答案:

答案 0 :(得分:1)

我不确定我是否理解你的问题..所以,让我希望我有:

你可以:

browser.execute("document.getElementById('txtFirstName').value=\"" + generateName() + "\"");

但generateName必须返回一个值而不是打印它..

public String generateName() {
   Random generateName = new Random();
   String[] name = ...
   String[] lastname = ...

   return name[generateName.nextInt(19)] + " " + lastname[generateName.nextInt(19)];
}