[英]Java jar run only once
單例使用:
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 主要:
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
我的英語不好抱歉。
我解決了: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");
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.