通过 Swt Eclipse 浏览器监听字段的变化
Listen to changes of field by Swt Eclipse browser
在 Java 项目中,我使用 SWT Eclipse
作为浏览器。
我 运行 这个浏览器和加载页面很好。
加载的页面如下所示:
<!DOCTYPE html>
<html>
<body>
Start: <input type="text" id="startTimeField" onchange="myFunction()" placeholder="Type text and press Enter"> ms
<script>
function myFunction() {
document.getElementById('startTimeField').value = new Date().getMilliseconds();
}
</script>
</body>
</html>
我想在 Java 代码中监听页面中的元素。
例如我想要实现的目标:
在 Java 中按下 JButton,它应该从页面中找到 ID 为 startTimeField
的按钮并向它们添加侦听器。监听器应该监听这个字段的变化值。这该怎么做 ?
这是一个非常简单的示例,其中包含两个 HTML 按钮。单击时,他们将调用 SWT BrowserFunction
并传入按钮的 ID:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
browser.setText("<button id='button1' onclick='buttonClicked(this.id)'>Click me</button><button id='button2' onclick='buttonClicked(this.id)'>Click me</button>");
new BrowserFunction(browser, "buttonClicked")
{
@Override
public Object function(Object[] objects)
{
System.out.println("BUTTON CLICK; ID: " + objects[0]);
return null;
}
};
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
在 Java 项目中,我使用 SWT Eclipse
作为浏览器。
我 运行 这个浏览器和加载页面很好。
加载的页面如下所示:
<!DOCTYPE html>
<html>
<body>
Start: <input type="text" id="startTimeField" onchange="myFunction()" placeholder="Type text and press Enter"> ms
<script>
function myFunction() {
document.getElementById('startTimeField').value = new Date().getMilliseconds();
}
</script>
</body>
</html>
我想在 Java 代码中监听页面中的元素。
例如我想要实现的目标:
在 Java 中按下 JButton,它应该从页面中找到 ID 为 startTimeField
的按钮并向它们添加侦听器。监听器应该监听这个字段的变化值。这该怎么做 ?
这是一个非常简单的示例,其中包含两个 HTML 按钮。单击时,他们将调用 SWT BrowserFunction
并传入按钮的 ID:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
browser.setText("<button id='button1' onclick='buttonClicked(this.id)'>Click me</button><button id='button2' onclick='buttonClicked(this.id)'>Click me</button>");
new BrowserFunction(browser, "buttonClicked")
{
@Override
public Object function(Object[] objects)
{
System.out.println("BUTTON CLICK; ID: " + objects[0]);
return null;
}
};
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}