繁体   English   中英

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

[英]Having trouble changing a variable with ActionListener

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

逾越节代码:

    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);

按钮的代码:

    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);

我希望在按下nextPassoverButton时将传递的值提高一,在按下previousPassoverButton时将传递的值降低一,并在按下goToPassoverButton时将其更改为用户输入到goToField中的值。

我的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--;
        }
    }
}

该错误是一个私有访问错误,因此我认为我需要使用getter和setter,但是我不知道该怎么做。 也许有一种完全不同的方式来做到这一点?

这是我第一次在这里发帖,如果我在发帖中有任何错误,敬请原谅。

您不能通过'=='比较两个对象。 请改用.equals(Object)。

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

getter和setter将是与私有int逾越节在同一类中的公共方法。 get方法只是返回您私有int值的单行方法。 喜欢

public static int getPassover(){
    return passover;
}

它之所以有效,是因为它与逾越节属于同一类,因此可以访问它。

设置器直接设置值:

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

如果您正在使用线程,请当心。

对于您的方法,如果要使用get set方法进行操作,则可以像

setPassover(getPassover() + 1);

如果passover不是类变量,则将从方法标题中删除“ static”。 这有意义吗?

更改逾越值后,必须在处理程序代码中将新的passover值设置为passoverLabel 否则,您将无法在标签上看到新值。

passoverLabel.setText(Integer.toString(passover))

暂无
暂无

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

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