How can my ActionListener for a JButton access variables in another class?

前端 未结 1 1250
别跟我提以往
别跟我提以往 2020-12-06 19:47

I am doing a slot machine for a project. I am having trouble getting my JButton to generate new random numbers from my ArrayList. I can randomize t

相关标签:
1条回答
  • 2020-12-06 20:19

    Trying to extends GGCGuiLotto isn't going to do what you think it's supposed to, which is give you access to the same instance variables. So get rid of that. Instead you can pass-by-reference, the current instance of GGCGuiLotto to your listener. And have some getters and setters to access the variables you need from the GGCGuiLotto class. What I mean is maybe something like this (not completely sure what you're trying to accomplish, so this is just an example).

    public class GenPLCListener implements ActionListener {
        private GGCGuiLotto lotto;
    
        public GenPLCListener(GGCGuiLotto lotto) {
            this.lotto = lotto;
        }
    
        @Override
        public void actionPerfomred(ActionEvent e) {
            List<ImageIcon> slotList = lotto.getSlotList();
            Collections.shuffle(slotList);  // shuffle the list
            // do something else if need be.
        }
    }
    

    When you create the listener, pass this to it. this being the instance of GGCGuiLotto


    Few Side Notes

    • Swing programs aren't like console programs. You don't want to do everything inside your main method. For starters, the code in your main method, you could put in the constructor instead. Then create an instance of GGCGuiLotto in the main method.

    • Swing apps should be run on the Event Dispatch Thread. See Initial Threads


    EDIT

    Maybe a more proper solution to your problem would be to have an interface with the pullSlot method that you can override in the GGCGuiLotto class and just pass the interface to the listener and call the pullSlot method inf your actionPerformed. Something like this

    public interface PullInterface {
        public void pullSlot();
    }
    
    public class GGCGuiLotto implements PullInterface {
        ArrayList<ImageIcon> slotList = new ArrayList<>();  // global scope.
        JLabel aReel1lbl = new JLabel();
        JLabel bReel2lbl = new JLabel();
        JLabel cReel3lbl = new JLabel();
        Random rand = new Random();
    
        public GGCGuiLotto() {
            GenPLCListener listener = new GenPLCListener(this);
        }
    
        @Override
        public void pullSlot() {
            // do what you need to do here to implement a pulling of the lever
            int r1 = rand.nextInt(slotList.size());
            int r2 = rand.nextInt(slotList.size());
            int r3 = rand.nextInt(slotList.size());
    
            reel1lbl.setIcon(slotList.get(r1));
        }
    }
    
    public class GenPLCListener implement ActionListener {
        private PullInterface pull;
    
        public GenPLCListener(PullInterface pull) {
            this.pull = pull;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            pull.pullSlot();
        }
    }
    
    0 讨论(0)
提交回复
热议问题