Java可运行应用程序sinlgeton问题
我在使用单例时遇到了一个问题,我将在代码之后解释这个问题 类启动器Java可运行应用程序sinlgeton问题,java,singleton,runnable,Java,Singleton,Runnable,我在使用单例时遇到了一个问题,我将在代码之后解释这个问题 类启动器 public class Launcher { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Controleur ctr = Controleur.getInstance(
public class Launcher {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Controleur ctr = Controleur.getInstance();
}
});
}
}
public class Controleur {
@SuppressWarnings("unused")
private EcranPrincipal EP;
private PremierEcran PE;
public Controleur() {
if(!VerifierFichier()){
PE = new PremierEcran();
PE.setVisible(true);
}
else
lancerEcranPrincipal();
}
private static Controleur INSTANCE = null;
public static Controleur getInstance(){
if(INSTANCE == null){
INSTANCE = new Controleur();
}
return INSTANCE;
}
public void lancerEcranPrincipal(){
try{
PE.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}catch(Exception e){}
EP = new EcranPrincipal();
}
public void setMDP(String mdp1){
new MotDePasse(mdp1);
}
public void setVille(String nomVilleFormat) {
FichierVille.getInstance(nomVilleFormat);
}
public String getNomVille() {
String nomVille = FichierVille.getInstance("").getNomFichier();
return "";
}
public boolean VerifierFichier(){
File mdpfile;
mdpfile = new File("data/MotDePasse.dat");
if(mdpfile.exists() && mdpfile.isFile()){
JOptionPane.showMessageDialog(null, "true");
return true;
}
else{
JOptionPane.showMessageDialog(null, "false");
return false;
}
}
}
class Controleur
public class Launcher {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Controleur ctr = Controleur.getInstance();
}
});
}
}
public class Controleur {
@SuppressWarnings("unused")
private EcranPrincipal EP;
private PremierEcran PE;
public Controleur() {
if(!VerifierFichier()){
PE = new PremierEcran();
PE.setVisible(true);
}
else
lancerEcranPrincipal();
}
private static Controleur INSTANCE = null;
public static Controleur getInstance(){
if(INSTANCE == null){
INSTANCE = new Controleur();
}
return INSTANCE;
}
public void lancerEcranPrincipal(){
try{
PE.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}catch(Exception e){}
EP = new EcranPrincipal();
}
public void setMDP(String mdp1){
new MotDePasse(mdp1);
}
public void setVille(String nomVilleFormat) {
FichierVille.getInstance(nomVilleFormat);
}
public String getNomVille() {
String nomVille = FichierVille.getInstance("").getNomFichier();
return "";
}
public boolean VerifierFichier(){
File mdpfile;
mdpfile = new File("data/MotDePasse.dat");
if(mdpfile.exists() && mdpfile.isFile()){
JOptionPane.showMessageDialog(null, "true");
return true;
}
else{
JOptionPane.showMessageDialog(null, "false");
return false;
}
}
}
班主任
public class EcranPrincipal extends JFrame{
public EcranPrincipal(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
getContentPane().setBackground(Color.white);
JPanel pane = new JPanel();
GridBagLayout gridbag = new GridBagLayout();
pane.setLayout(gridbag);
pane.setBackground(Color.white);
JTextPane Textmdp = new JTextPane();
String name = Controleur.getInstance().getNomVille();
Textmdp.setText("ville: ");
Textmdp.setEditable(false);
}
}
当singleton已经实例化时,在类“EcranPrincipal”中,我将类的实例singleton称为“controleur”:
这里最大的问题是这一行,因为它给出了这一行的执行结果
JOptionPane.showMessageDialog(null, "true");
谁在“Controleur”类的Function VerifierFichier()中。因此,我有无限次的showmessage对话框。我的问题是为什么我的单身汉没有按照我想要的那样执行。
确切地说,我希望我的变量“name”去获取函数“getNomVille()”中的信息
请帮帮我
提前谢谢