因为我还是个小白,昨天又摸索了一天,出现了各种问题......路径不对,Jar包问题,JDK版本问题,32位或者是64位问题等等等等,终于,我弄好了,它可以运行了....
(注意,在你已经有一个WEB前端的情况下才可以用,要把VUE项目导进去啊!!)
//你自己的包名
import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.BrowserFunction;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.graphics.Point;
public class Main {
public static void main(String[] args) {
//Shell组件,就是那个窗口,在这里调他的位置大小啥的
Display display = new Display();
Shell shell = new Shell(display);
shell.setMinimumSize(new Point(1300, 900));
shell.setSize(900, 600);
shell.setText("管理系统");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
shell.setLocation(347, 70);
//Composite组件,把Web页面放进去
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new FormLayout());
//这里是关键啊,Browser是交互互联网的关键
Browser browser = new Browser(composite, SWT.NONE);
FormData fd_browser = new FormData();
fd_browser.bottom = new FormAttachment(100);
fd_browser.right = new FormAttachment(100);
fd_browser.top = new FormAttachment(0);
fd_browser.left = new FormAttachment(0);
browser.setLayoutData(fd_browser);
//file:///后加上你自己的Vue项目路径,一般都是什么什么index.html
browser.setUrl("file:///"); // Replace with your Vue app's index.html path
//这里是我自己弄的按钮Demo
new MyJavaFunction(browser, "javaFunction"); // Expose Java function to JavaScript
//打开窗体
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
class MyJavaFunction extends BrowserFunction {
public MyJavaFunction(Browser browser, String name) {
super(browser, name);
}
@Override
public Object function(Object[] arguments) {
// Handle JavaScript call and perform actions in Java
if (arguments.length > 0 && arguments[0] instanceof String) {
String arg = (String) arguments[0];
if ("showSettings".equals(arg)) {
// Code to show settings dialog
} else if ("switchPage".equals(arg)) {
// Code to switch to another page
}
}
return null;
}
}
然后接下来我就要继续摸索页面里面的按钮,让他们进行JS和JAVA的交互了
有什么不对请指正