简体   繁体   English

使用ActionListener更改变量时遇到麻烦

[英]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. 我正在使用GUI,并且所有内容都可以正确显示,但是在按按钮时无法更改变量的值。 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. 该变量称为传递,它是一个私有int,它在0参数构造函数中初始化为1997的值,我有3个按钮,在按下时需要更改传递的值。

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. 我希望在按下nextPassoverButton时将传递的值提高一,在按下previousPassoverButton时将传递的值降低一,并在按下goToPassoverButton时将其更改为用户输入到goToField中的值。

My ActionListener class looks like: 我的ActionListener类看起来像:

    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. 该错误是一个私有访问错误,因此我认为我需要使用getter和setter,但是我不知道该怎么做。 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. 请改用.equals(Object)。

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

A getter and setter would be a public method in the same class as your private int passover. getter和setter将是与私有int逾越节在同一类中的公共方法。 The get method would just be a single line mehtod that returns the value of your private int. get方法只是返回您私有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 对于您的方法,如果要使用get set方法进行操作,则可以像

setPassover(getPassover() + 1);

If passover is not a class variable, you will take the 'static's out of the method heading. 如果passover不是类变量,则将从方法标题中删除“ static”。 Does this make sense? 这有意义吗?

you must set the new passover value to the passoverLabel in handler code after passover value change. 更改逾越值后,必须在处理程序代码中将新的passover值设置为passoverLabel otherwhise you cannot see the new value on the label. 否则,您将无法在标签上看到新值。

passoverLabel.setText(Integer.toString(passover))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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