In my program, there are two panels organized in CardLayout, panel 1 for user login, panel 2 for displaying user profile, and a singleton class is created for data sharing between these two panels.Scenario can be, on panel 1, user type in username and password, click button "login", then in actionPerformed(ActionEvent e), username will be saved in singleton class String username = "somebody" and panel 2 will be made visible on the screen with the username in a JLabel.My problem is, when I click the button, panel 2 shows up, but the username is lost, seems the change in actionPerformed() does not affect the singleton variable username. But if I put the change String username = "somebody" in constructor, the change takes place.I've tested different ways of singleton, didn't work out though. the original code was long and a little messy. below is the simplified version which maintains the problem.Any help will be appreciated!This is the main class of the program.[CODE]
import javax.swing.*;
import java.awt.*;public class AlbumSys extends JFrame {    JPanel pnlCards = null;
    CardLayout layout = null;
    
    public AlbumSys ()
    {
        layout = new CardLayout();
        pnlCards = new JPanel(layout);
        pnlCards.add(new PanelAuthen(), "authentication");
        pnlCards.add(new PanelAlbumList(), "albumlist");
        getContentPane().add(pnlCards);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    public void init()
    {
        setLocation(500, 400);
        pack();
        setVisible(true);
        
        while(true) {
            if (Repository.getInstance().getStageChanged()) {
                switch (Repository.getInstance().getCurrStage()) {
                    case 0:
                        layout.show(pnlCards, "authentication");
                        break;
                    case 1:
                        layout.show(pnlCards, "albumlist");
                        break;
                    default:
                        layout.show(pnlCards, "authentication");
                }
                Repository.getInstance().setStageChanged(false);
            }
            
            try {
                Thread.sleep(10);
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }       
        }
    }
    
    public static void main(String[] args)
    {
        new AlbumSys().init();
    }
}
[/CODE]This is the singleton class for data sharing between panels.[CODE]
public class Repository {
    
    private static Repository instance = null;
    private Repository () {}
    public static Repository getInstance()
    {
        if (instance == null)
            instance = new Repository();
            
        return instance;
    }
    
    private static int currStage = 0;
    private static Boolean stageChanged = false;
    private static String username = "Sad! It's not changed.";
    
    public void setCurrStage(int i)
    {
        this.currStage = i;
    }
    
    public int getCurrStage()
    {
        return this.currStage;
    }
    
    public void setStageChanged(boolean b)
    {
        this.stageChanged = b;
    }
    
    public boolean getStageChanged()
    {
        return this.stageChanged;
    }
    
    public void setUsername(String s)
    {
        this.username = s;
    }
    
    public String getUsername()
    {
        return this.username;
    }
}
[/CODE]This is panel 1 for user login.[CODE]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class PanelAuthen extends JPanel implements ActionListener{
   
    public PanelAuthen()
    {
        Button btnSignIn = new Button("signin");
        btnSignIn.addActionListener(this);
        add(btnSignIn);
    }
    
    public void actionPerformed(ActionEvent e)
    {
        String cmd = e.getActionCommand();
        if (cmd.equals("signin")) {
            Repository.getInstance().setUsername("Haha! It's been changed!");
            Repository.getInstance().setCurrStage(1); // to switch to album list panel
            Repository.getInstance().setStageChanged(true);
        }
    }
}[/CODE]This is panel 2 for displaying user profile.[CODE]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class PanelAlbumList extends JPanel{    public PanelAlbumList()
    {
        JLabel lblTitle = new JLabel(Repository.getInstance().getUsername());
        add(lblTitle);
    }
}
[/CODE]

解决方案 »

  1.   

    In my program, there are two panels organized in CardLayout, panel 1 for user login, panel 2 for displaying user profile, and a singleton class is created for data sharing between these two panels.Scenario can be, on panel 1, user type in username and password, click button "login", then in actionPerformed(ActionEvent e), username will be saved in singleton class String username = "somebody" and panel 2 will be made visible on the screen with the username in a JLabel.My problem is, when I click the button, panel 2 shows up, but the username is lost, seems the change in actionPerformed() does not affect the singleton variable username. But if I put the change String username = "somebody" in constructor, the change takes place.I've tested different ways of singleton, didn't work out though. the original code was long and a little messy. below is the simplified version which maintains the problem.Any help will be appreciated!This is the main class of the program.[CODE]
    import javax.swing.*;
    import java.awt.*;public class AlbumSys extends JFrame {    JPanel pnlCards = null;
        CardLayout layout = null;
        
        public AlbumSys ()
        {
            layout = new CardLayout();
            pnlCards = new JPanel(layout);
            pnlCards.add(new PanelAuthen(), "authentication");
            pnlCards.add(new PanelAlbumList(), "albumlist");
            getContentPane().add(pnlCards);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        
        public void init()
        {
            setLocation(500, 400);
            pack();
            setVisible(true);
            
            while(true) {
                if (Repository.getInstance().getStageChanged()) {
                    switch (Repository.getInstance().getCurrStage()) {
                        case 0:
                            layout.show(pnlCards, "authentication");
                            break;
                        case 1:
                            layout.show(pnlCards, "albumlist");
                            break;
                        default:
                            layout.show(pnlCards, "authentication");
                    }
                    Repository.getInstance().setStageChanged(false);
                }
                
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }       
            }
        }
        
        public static void main(String[] args)
        {
            new AlbumSys().init();
        }
    }
    [/CODE]This is the singleton class for data sharing between panels.[CODE]
    public class Repository {
        
        private static Repository instance = null;
        private Repository () {}
        public static Repository getInstance()
        {
            if (instance == null)
                instance = new Repository();
                
            return instance;
        }
        
        private static int currStage = 0;
        private static Boolean stageChanged = false;
        private static String username = "Sad! It's not changed.";
        
        public void setCurrStage(int i)
        {
            this.currStage = i;
        }
        
        public int getCurrStage()
        {
            return this.currStage;
        }
        
        public void setStageChanged(boolean b)
        {
            this.stageChanged = b;
        }
        
        public boolean getStageChanged()
        {
            return this.stageChanged;
        }
        
        public void setUsername(String s)
        {
            this.username = s;
        }
        
        public String getUsername()
        {
            return this.username;
        }
    }
    [/CODE]This is panel 1 for user login.[CODE]
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class PanelAuthen extends JPanel implements ActionListener{
       
        public PanelAuthen()
        {
            Button btnSignIn = new Button("signin");
            btnSignIn.addActionListener(this);
            add(btnSignIn);
        }
        
        public void actionPerformed(ActionEvent e)
        {
            String cmd = e.getActionCommand();
            if (cmd.equals("signin")) {
                Repository.getInstance().setUsername("Haha! It's been changed!");
                Repository.getInstance().setCurrStage(1); // to switch to album list panel
                Repository.getInstance().setStageChanged(true);
            }
        }
    }[/CODE]This is panel 2 for displaying user profile.[CODE]
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class PanelAlbumList extends JPanel{    public PanelAlbumList()
        {
            JLabel lblTitle = new JLabel(Repository.getInstance().getUsername());
            add(lblTitle);
        }
    }
    [/CODE]
      

  2.   

    你的panel怎么去修改singleton class的username的,我估计是这里出了问题吧
      

  3.   

    where's your code, nothing found at allone thing first to confirm is that has the listener received the event and turned to any response?
      

  4.   

    My eyes are tired, So I don't want to read anything in English