简体   繁体   中英

Having trouble changing a variable with ActionListener

I'm working on a GUI and I have everything displaying correctly but I'm having trouble changing the value of a variable when I press a button. The variable is called passover and it is a private int that is initialized to the value of 1997 in the 0 argument constructor and I have 3 buttons that need to change the value of passover when pressed.

Code for passover:

    panel.setBorder(
    BorderFactory.createEmptyBorder(0, 0, 0, 0));
    panel.setLayout(new BorderLayout(0, 0));

    JPanel topPanel = getPanel();
    topPanel.setLayout(new GridBagLayout());
    topPanel.setBackground(new Color(173, 216, 230));
    JLabel moveLabel = getLabel("Move Data");
    moveLabel.setFont(new Font("Serif", Font.PLAIN, 20));
    addComp(topPanel, moveLabel, 0, 0, 1, 1, 0.5, 0.2,
            GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHEAST);
    JLabel passoverLabel = getLabel("       Passover : " + passover);
    passoverLabel.setFont(new Font("Serif", Font.PLAIN, 20));
    addComp(topPanel, passoverLabel, 1, 0, 1, 1, 0.5, 0.2,
            GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);

Code for the buttons:

    JPanel bottomRightPanel = getPanel();
    bottomRightPanel.setBorder(
               BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
    bottomRightPanel.setLayout(new GridBagLayout());
    bottomRightPanel.setBackground(new Color(255, 105, 180));
    JPanel passoverButtonPanel = getPanel();
    passoverButtonPanel.setBackground(new Color(255, 105, 180));
    passoverButtonPanel.setLayout(new BoxLayout(passoverButtonPanel, BoxLayout.Y_AXIS));
    nextPassoverButton = new JButton("Next Passover");
    goToPassoverButton = new JButton("Go To Passover: ");
    previousPassoverButton = new JButton("Previous Passover");
    JLabel filler = getLabel(" ");
    goToField = new JTextField(6);
    goToField.setPreferredSize(new Dimension(5, 30));


    theHandler handler = new theHandler();
    nextPassoverButton.addActionListener(handler);
    goToPassoverButton.addActionListener(handler);
    previousPassoverButton.addActionListener(handler);
    goToField.addActionListener(handler);

I want the value of passover to be raised one when nextPassoverButton is pressed, lowered one when previousPassoverButton is pressed, and to be changed to the value the user inputs into the goToField when goToPassoverButton is pressed.

My ActionListener class looks like:

    public class theHandler extends MyDataPalV2 implements ActionListener{

    public void actionPerformed(ActionEvent event)
    {


        if (event.getSource() == goToField)
        {
            this.passover = Integer.parseInt(String.format("%s", event.getActionCommand()));
        }

        if (event.getSource() == nextPassoverButton)
        {
            this.passover++;
        }

        if (event.getSource() == previousPassoverButton)
        {
            this.passover--;
        }
    }
}

The error is a private access error so I think I would need to use a getter and setter but I have no idea how I would do that. Maybe there is a complete different way to do this?

This is my first time posting here so sorry if I made any mistakes in my post.

You cannot compare two objects via '=='. Use .equals(Object) instead.

if(event.getSource().equals(goToField))

A getter and setter would be a public method in the same class as your private int passover. The get method would just be a single line mehtod that returns the value of your private int. Like

public static int getPassover(){
    return passover;
}

It works because it is in the same class as passover and thus has access to it.

The setter just straight up sets the value:

public static void setPassover(int num){
    passover = num;
}

Be careful with this if you are using threads.

For your method, if you want to do it with get set methods, you could increment it like

setPassover(getPassover() + 1);

If passover is not a class variable, you will take the 'static's out of the method heading. Does this make sense?

you must set the new passover value to the passoverLabel in handler code after passover value change. otherwhise you cannot see the new value on the label.

passoverLabel.setText(Integer.toString(passover))

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