下面是本人做的小例子使用的是swt技术,这个例子没有业务逻辑主要是为了说明swt打开浏览器的实现,本人加入了线程的控制提交程序运行速度。小弟初来乍到,如果下面的小例子有可以改进的地方希望大侠不惜赐教:
代码如下:
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Test extends Thread {
public static void main(String[] args) throws Exception {
new Test().start();
}
public void run(){
goIntnet();
}
public void goIntnet(){
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("打开浏览器");
shell.setBounds(300, 100, 600, 400);
Button goIntenet = new Button(shell, SWT.NONE);
goIntenet.setText("打开浏览器");
goIntenet.setBounds(150, 80, 300, 200);
goIntenet.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e){
try {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://localhost:8080/");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
}
}
运行结果如下图所示:
点击按钮,可以成功打开浏览器窗口如下图所示:
下面是本人做的小例子使用的是swt技术,这个例子没有业务逻辑主要是为了说明swt打开浏览器的实现,本人加入了线程的控制提交程序运行速度。小弟初来乍到,如果下面的小例子有可以改进的地方希望大侠不惜赐教:
代码如下:
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Test extends Thread {
public static void main(String[] args) throws Exception {
new Test().start();
}
public void run(){
goIntnet();
}
public void goIntnet(){
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("打开浏览器");
shell.setBounds(300, 100, 600, 400);
Button goIntenet = new Button(shell, SWT.NONE);
goIntenet.setText("打开浏览器");
goIntenet.setBounds(150, 80, 300, 200);
goIntenet.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e){
try {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://localhost:8080/");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
}
}
运行结果如下图所示:
点击按钮,可以成功打开浏览器窗口如下图所示: