Java jar只运行一次

时间:2015-05-31 12:47:13

标签: java jar

Singleton使用:

    public class Singleton {

       private static Singleton singleton = new Singleton( );

       /* A private Constructor prevents any other 
        * class from instantiating.
        */
       private Singleton(){ }

       /* Static 'instance' method */
       public static Singleton getInstance( ) {
      return singleton;
   }
   /* Other methods protected by singleton-ness */
   protected static void demoMethod( ) {
       new NewJFrame().setVisible(true);
   }
}

NewJFrame main:

   public static void main(String args[]) { 
        /* Set the Nimbus look and feel */
         * For details see ht
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//                new NewJFrame().setVisible(true);
                Singleton tmp = Singleton.getInstance( );
                tmp.demoMethod();
            }
        });
       }

我想要一个罐子。

不想要另一个罐子。看到: http://i.stack.imgur.com/5LPjc.png

我的英语很抱歉。

1 个答案:

答案 0 :(得分:-1)

我解决了:D

NewJFrame主要改变它:

public static void main(String args[]) { 
        /* Set the Nimbus look and feel */
         * For details see ht
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//              new NewJFrame().setVisible(true);
                ServerSocket once;
                Singleton singleton;

                try {
                    once = new ServerSocket(1334);
                    Singleton.getInstance().demoMethod();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Already Open");
                }
            }
        });
       }