簡體   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