前言
因为我还是个小白,昨天又摸索了一天,出现了各种问题......路径不对,Jar包问题,JDK版本问题,32位或者是64位问题等等等等,终于,我弄好了,它可以运行了....
(注意,在你已经有一个WEB前端的情况下才可以用,要把VUE项目导进去啊!!)
SWT代码:
1. //你自己的包名 2. 3. import java.io.File; 4. 5. import org.eclipse.swt.SWT; 6. import org.eclipse.swt.browser.Browser; 7. import org.eclipse.swt.browser.BrowserFunction; 8. import org.eclipse.swt.layout.FillLayout; 9. import org.eclipse.swt.widgets.Display; 10. import org.eclipse.swt.widgets.Shell; 11. import org.eclipse.swt.widgets.Composite; 12. import org.eclipse.swt.layout.FormLayout; 13. import org.eclipse.swt.layout.FormData; 14. import org.eclipse.swt.layout.FormAttachment; 15. import org.eclipse.swt.graphics.Point; 16. 17. public class Main { 18. 19. public static void main(String[] args) { 20. //Shell组件,就是那个窗口,在这里调他的位置大小啥的 21. Display display = new Display(); 22. Shell shell = new Shell(display); 23. shell.setMinimumSize(new Point(1300, 900)); 24. shell.setSize(900, 600); 25. shell.setText("管理系统"); 26. shell.setLayout(new FillLayout(SWT.HORIZONTAL)); 27. shell.setLocation(347, 70); 28. 29. //Composite组件,把Web页面放进去 30. Composite composite = new Composite(shell, SWT.NONE); 31. composite.setLayout(new FormLayout()); 32. 33. //这里是关键啊,Browser是交互互联网的关键 34. Browser browser = new Browser(composite, SWT.NONE); 35. FormData fd_browser = new FormData(); 36. fd_browser.bottom = new FormAttachment(100); 37. fd_browser.right = new FormAttachment(100); 38. fd_browser.top = new FormAttachment(0); 39. fd_browser.left = new FormAttachment(0); 40. browser.setLayoutData(fd_browser); 41. 42. //file:///后加上你自己的Vue项目路径,一般都是什么什么index.html 43. browser.setUrl("file:///"); // Replace with your Vue app's index.html path 44. //这里是我自己弄的按钮Demo 45. new MyJavaFunction(browser, "javaFunction"); // Expose Java function to JavaScript 46. 47. //打开窗体 48. shell.open(); 49. while (!shell.isDisposed()) { 50. if (!display.readAndDispatch()) { 51. display.sleep(); 52. } 53. } 54. display.dispose(); 55. } 56. } 57. 58. class MyJavaFunction extends BrowserFunction { 59. 60. public MyJavaFunction(Browser browser, String name) { 61. super(browser, name); 62. } 63. 64. @Override 65. public Object function(Object[] arguments) { 66. // Handle JavaScript call and perform actions in Java 67. if (arguments.length > 0 && arguments[0] instanceof String) { 68. String arg = (String) arguments[0]; 69. if ("showSettings".equals(arg)) { 70. // Code to show settings dialog 71. } else if ("switchPage".equals(arg)) { 72. // Code to switch to another page 73. } 74. } 75. return null; 76. } 77. }
然后接下来我就要继续摸索页面里面的按钮,让他们进行JS和JAVA的交互了
有什么不对请指正