如何组织code打造的applet和应用容易吗? [英] How to organize code to build applet and application easily?

查看:132
本文介绍了如何组织code打造的applet和应用容易吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被用Java实现的客户端应用程序作为与Swing。但现在我想建立也从中小程序。什么是重新设计/重构,以便能够很容易地建立他们与保持最佳途径干燥。

这是code短提取具有的main()

 公共类客户{
    公共静态最终ScheduledExecutorService的服务;
    受保护的静态字符串主机;
    受保护的静态INT端口;    静态的 {
         SERVICE = Executors.newSingleThreadScheduledExecutor();
         主机=
         端口=
    }    公共静态无效的主要(字串[] args){
         // initalize网状         //创建用户界面=的JFrame在SwingUtilities.invokeLater         连接();
    }    公共静态无效连接(){
         //连接使用网状
    }

所以我复制此文件作为一个单独的之一,从 JApplet的扩展它,并改变的init ,因此它可以运行,但当然是丑陋的,因为许多code的仅仅是复制粘贴的。

有没有通用的解决方案是如何重新设计它?

UPD:

 公共类客户{
    公共静态无效的主要(字串[] args){
        应用程序=新的App();
        app.connect();
    }
}公共Applet类扩展JApplet的{
    公共无效的init(){
        应用程序=新的App();
        app.connect();
    }
}

和所有的初始化逻辑移至应用


解决方案

去除大量的应用程序,以不依赖于顶层容器类组。

这意味着,您可以重用/重新部署,因为你需要无链接你的自我顶层容器应用程序。

您再需要一个主类为您的桌面入口点和一个小程序类。这只会构建应用程序的主界面并将其连接到屏幕的容器中。

这就是为什么我们建议您不要从顶层容器覆盖直接,但是从简单的容器扩展应用程序的逻辑/ UI,如的JP​​anel

I have being implementing a client in Java as application with Swing. But now I want to build also Applet from it. What is the best way to redesign/refactor in order to be able to build both of them easily and keeping it DRY.

This is a short extraction of code which has main()

public class Client {
    public static final ScheduledExecutorService SERVICE;
    protected static String host;
    protected static int port;

    static {
         SERVICE = Executors.newSingleThreadScheduledExecutor();
         host =
         port = 
    }

    public static void main(String[] args) {
         //initalize netty

         //create user interface = JFrame in SwingUtilities.invokeLater

         connect();
    }

    public static void connect () {
         //connect using netty
    }

So I copy this file as a separate one, extend it from JApplet and change main to init, so it can be run, but of course it is ugly, because much of code is just copy-pasted.

Is there universal solution how to redesign it?

UPD:

public class Client {
    public static void main (String[] args) {
        App app = new App();
        app.connect();
    }
}

public class Applet extends JApplet {
    public void init () {
        App app = new App();
        app.connect();
    }
}

and to move all initialization logic to App

解决方案

Remove the bulk of the application to group of classes that do not rely on the top level container.

This means, you can reuse/redeploy the application as you need without chaining your self to a top level container.

You will then need a "main" class for your desktop entry point and a "applet" class. This would simply construct the applications main interface and attach it to a screen container.

This is one of the reasons why we suggest you never override from a top level container directly, but extend your application logic/UI from simple container, like JPanel

这篇关于如何组织code打造的applet和应用容易吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆