简体   繁体   中英

How to use variables set in an actionlistener

I am having problems understanding how to use an actionlistener to change the value of variables.

In my program I need to store the choices the user makes by selecting some radio buttons.

I have got a main class with a card layout, then several classes which each are different panels. In one of the panels I have some radio buttons, with an actionlistener as an inner class.

When I try to print the variable value in the main class, it is printed immediately, before the user has made a choice, as I instantiate the panel class and get the variable from it I get the variable before it has been changed by the user.

I know I should not think in a linear manner with Java, but how can I make sure that I fetch the variable after it has been changed by the user and not before? I will not be able to do that will I? I understand there is some flaw in my thinking but I haven't slept properly for ages and I just cannot get my head around this.

public class Screen3 extends JPanel{

JRadioButton addition = new JRadioButton("Addition");
JRadioButton subtraction = new JRadioButton("Subtraction");
JRadioButton multiplication = new JRadioButton("Multiplication");
JRadioButton division = new JRadioButton("Division");
JRadioButton all = new JRadioButton("All");

JRadioButton single = new JRadioButton("Single");
JRadioButton two = new JRadioButton("Double");
JRadioButton triple = new JRadioButton("Triple");
JRadioButton mix = new JRadioButton("Mix");

JRadioButton five = new JRadioButton("5");
JRadioButton ten = new JRadioButton("10");

private int type, digit, rounds;

public Screen3() {

JPanel firstButtonPanel = new JPanel();
    JPanel secondButtonPanel = new JPanel();
    ButtonGroup myFirstGroup = new ButtonGroup();
    ButtonGroup mySecondGroup = new ButtonGroup();

    myFirstGroup.add(addition);
    myFirstGroup.add(subtraction);
    myFirstGroup.add(multiplication);
    myFirstGroup.add(division);
    //myFirstGroup.add(all);

    mySecondGroup.add(single);
    mySecondGroup.add(two);
    mySecondGroup.add(triple);
    //mySecondGroup.add(mix);

    firstButtonPanel.setLayout(new FlowLayout());
    firstButtonPanel.add(addition);
    firstButtonPanel.add(subtraction);
    firstButtonPanel.add(multiplication);
    firstButtonPanel.add(division);
    //firstButtonPanel.add(all);

    secondButtonPanel.setLayout(new FlowLayout());
    secondButtonPanel.add(single);
    secondButtonPanel.add(two);
    secondButtonPanel.add(triple);
    //secondButtonPanel.add(mix);

    JPanel buttons = new JPanel();
    buttons.setLayout(new BorderLayout());
    buttons.add(selectionLabel, BorderLayout.NORTH);
    buttons.add(firstButtonPanel, BorderLayout.CENTER);
    buttons.add(secondButtonPanel, BorderLayout.SOUTH);

ButtonGroup myThirdGroup = new ButtonGroup();
    JPanel endButtons = new JPanel();

    myThirdGroup.add(five);
    myThirdGroup.add(ten);

    endButtons.add(five);
    endButtons.add(ten);

    endPanel.setLayout(new BorderLayout());
    endPanel.add(rounds, BorderLayout.NORTH);
    endPanel.add(endButtons, BorderLayout.CENTER);

    setLayout(new BorderLayout());
    add(buttons, BorderLayout.NORTH);

Selection sn = new Selection();

    addition.addActionListener(sn);
    subtraction.addActionListener(sn);
    multiplication.addActionListener(sn);
    division.addActionListener(sn);

    single.addActionListener(sn);
    two.addActionListener(sn);
    triple.addActionListener(sn);

    five.addActionListener(sn);
    ten.addActionListener(sn);
}

public int getType() {
    return type;
}

public int getDigit() {
    return digit;
}

public int getRounds() {
    return rounds;
}

public class Selection implements ActionListener {  
    public void actionPerformed(ActionEvent e) {
        if(addition.isSelected()) { 
            type = 1;
        }
        else if(subtraction.isSelected()) {
            type = 2;
        }
        else if(multiplication.isSelected())
            type = 3;
        else if(division.isSelected())
            type = 4;
        //else if(all.isSelected())
            //type = 5;

        if(single.isSelected()) {
            digit = 1;
            System.out.println("single");
        }
        else if(two.isSelected())
            digit = 2;
        else if(triple.isSelected())
            digit = 3;

        if(five.isSelected())
            rounds = 5;
        else if(ten.isSelected())
            rounds = 10;
    }
}

}

Here is the main class:

public class Driver {

public JFrame frame = new JFrame("Math Game");

public JPanel screens = new JPanel(new CardLayout());

int digit = 1;
int rounds = 1;
int type = 1;

Driver() {

}

public void show() {

    JPanel buttonPanel = new JPanel();
    JButton next = new JButton("Next");
    JButton previous = new JButton("Previous");
    buttonPanel.add(previous);
    buttonPanel.add(next);

    Screen1 screen1 = new Screen1();  
    Screen2 screen2 = new Screen2();
    Screen3 screen3 = new Screen3();

    screens.add(screen1, "welcome");
    screens.add(screen2, "next");
    screens.add(screen3, "selection");

    frame.add(screens);
    frame.add(buttonPanel, BorderLayout.PAGE_END);
    frame.setSize(400, 500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    next.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            CardLayout cl = (CardLayout)(screens.getLayout());
            cl.next(screens);
        }
    });

    previous.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            CardLayout cl = (CardLayout)(screens.getLayout());
            cl.previous(screens);
        }
    });
}

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Driver dr = new Driver();
            dr.show();
        }
    });
}

}

I just try a test print of System.out.println(screen3.getType()); either in show() or main

Use JOptionPane / JDialog which has modality .

Have a read on How to Make Dialogs

In example here is only printed after JDialog is closed:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JDialog jd = new JDialog();
            jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            jd.setModal(true);
            jd.pack();
            jd.setVisible(true);

            System.out.println("Here");
        }
    });
}

In this example here is only printed after JOptionPane is closed:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JPanel panel=new JPanel();
            panel.add(new JLabel("Hello, world!"));

            JOptionPane.showMessageDialog(null, panel, "Panel Message",JOptionPane.PLAIN_MESSAGE);

            System.out.println("Here");
        }
    });
}

I know I should not think in a linear manner with Java, but how can I make sure that I fetch the variable after it has been changed by the user and not before?

After using a modal JDialog / JOptionPane you would simply use public getters to access the variable contained within the class instance:

public class Test {
    public static void main(String[] args) {
       X x= new X();//will only return after dialog closed

       System.out.println(x.getY());
    }
}

class X {

    private int y=0;//will be assigned during dialog/joptionpanes life span

    public X() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
               //creates and shows the modal dialog/optionpane which will allow modification of variable y through some input/controls
            }
       });
    }

    public int getY() {
        return y;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM