Java Swing应用程序转为浏览器应用程序

huangapple go评论77阅读模式
英文:

Java Swing Application to Browser Application

问题

我已经创建了一个相当复杂的Java Swing GUI程序。实质上它是一个修改过的棋盘游戏,使用了诸如SwingWorker、MouseListener和JComponents的子类之类的东西。我正在寻找一种将这个程序转换为可以在浏览器上运行的方法(不需要通过服务器进行多人在线游戏等)。是否有人对如何解决这个问题有任何建议?我进行了一些快速的搜索,找到了Vaadin和webswing,但我不知道它们是否包含我所需要的所有功能,或者是否值得花时间用它们来实现。

英文:

I have created a fairly complicated Java Swing GUI program. It’s essentially a modified board game and uses things like SwingWorker, MouseListener, children of JComponents. I am looking for a way to convert this program so that it can be run on a browser (not to have multiplayer online with servers and what not). Does anyone have any advice on how to approach this? I did some quick googling and found Vaadin and webswing but I do not know if they contain all the features I need or if it’s worth the effort to do it with them.

答案1

得分: 2

由于您明确提到了Vaadin作为一个例子,我将详细说明它带来了什么。Vaadin使用一种与Swing非常相似的编程模型。这意味着您通常可以以相同的方式构造代码,即通过创建和配置组件作为Java实例,将这些组件作为布局组件的子项添加,最后监听来自这些组件的事件。

实际的组件实现与Swing中的不同,但诸如按钮、文本输入字段和数据网格等相同的基本组件集仍然可用。不同的组件实现意味着您仍然需要重写与实际GUI相关的所有代码。

在这种情况下,Vaadin的好处在于您无需为了实现在浏览器中本地运行的东西而必须学习浏览器的工作原理。您还可以在相对较小的改动下重用所有现有的非GUI代码。另一方面,您也可以将此视为一个机会,以便熟悉本机Web技术,如HTML和JavaScript,因为这是一个非常有用的技能。

英文:

Since you explicitly mentioned Vaadin as an example, I will elaborate on what it brings to the table. Vaadin uses a programming model that is very similar to that of Swing. What this means is that you can in general structure your code in the same way, i.e. by creating and configuring components as Java instances, adding those components as children of layout components and finally listening to events from those components.

The actual component implementations are different than in Swing, but the same basic set of components such as buttons, text input fields and data grids are still available. The different component implementations means that you will still end up rewriting all of the code related to the actual GUI.

The benefit of Vaadin in this case is that you don't need to learn how browsers work just to implement something that runs natively in the browser. You can also reuse all your existing non-GUI code with relatively small changes. On the other hand, you can also see this as an opportunity to get familiar with native web technologies such as HTML and JavaScript since that's a very useful skill to have.

答案2

得分: 0

你所描述的是一个Java小程序,但这已不再是一个可行的平台。

如果你想在应用逻辑中使用Java而不使用界面,可以考虑使用一个前端库,通过Websockets或Ajax调用与Java后端连接。

如果你非常必须使用Swing,那么你唯一的选择就是在服务器上渲染应用程序,然后将“屏幕截图”或绘图调用发送回浏览器,并让浏览器使用JavaScript将其渲染到画布上,这本质上就是你找到的那些库所做的。这样做速度较慢且容易出现故障,可能不太具备可扩展性。

另一个选择是将Java应用程序打包为exe或jar文件,并在远程系统上独立运行,然后通过浏览器进行交付。

英文:

What you're describing is a java applet, which is no longer a viable platform.

If you want to use java for application logic but not the interface, you might consider a frontend library that connects to a java backend using websockets or ajax calls.

If you absolutely must use Swing, then you're only option is to render the application on a server and then send 'screen shots' or maybe draw calls back to the browser and have it render them to a canvas using javascript, which is essentially what the libraries you found do. It's slow and glitchy, and likely not very scalable.

Another option is packaging the java application into an exe or jar and running it on the remote system all on its own but delivering it through the browser.

答案3

得分: 0

如果您只对您的Swing应用在Web浏览器中的工作方式感兴趣,那么您绝对应该尝试一下Webswing。我不同意Charlie的观点,即“库的速度慢、存在故障并且不可扩展”。Webswing速度快,渲染效果与本地平台完全相同。这个快速入门指南将帮助您设置您的应用程序。

英文:

If you are just interested in how your Swing application will work in a web browser you should definitely give Webswing a try. I disagree with Charlie that the "libraries are slow, glitchy and not scalable". Webswing is fast and renders exactly same as on a native platform. This quickstart guide will help you setup your application.

huangapple
  • 本文由 发表于 2020年4月10日 14:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61134876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定