Getter返回0 [Java]

时间:2016-02-09 20:44:32

标签: java actionlistener getter

我正试图从另一个类调用一个Getter,当我这样做时,它每次返回并打印0。 单击easyButton时会生成一个随机数。单击该按钮后,在使用enterInputButton 调用getter后,它仍然显示为零。 这是代码。 我有打印声明,表明它不起作用。我在其他课程中这样做,它适用于我的其他小项目。 我只是不明白为什么它在print语句中调用它后返回0,当我点击enterInputButton时。 非常感谢! (这是我第一次在这里发帖)

public class UserInputPanel extends JPanel
{
    private DifficultyPanel grabDiff;
    private SpringLayout baseLayout;

    private JTextField userGuessField;
    private JButton enterInputButton;

    private int userGuess;

    //Class that needs the random Number
    public UserInputPanel() 
    {
        grabDiff = new DifficultyPanel();

        baseLayout = new SpringLayout();

        userGuessField = new JTextField(2);
        enterInputButton = new JButton("Enter Guess");

        buildPanel();
        buildWindow();
        buildListeners();
    }

    private void buildPanel()
    {
        setBackground(Color.RED);
        setPreferredSize(new Dimension(200,200));
        setLayout(baseLayout);
        add(userGuessField);
        add(enterInputButton);
    }

    private void buildWindow()
    {

    }

    private void buildListeners()
    {
        enterInputButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent clicked)
                {
                    parseInput();
                    System.out.println("MY INPUT " + userGuess);

                    //This line prints it to 0
                    System.out.println("GEN NUM : " + grabDiff.getSelectedNumber());
                }
            });
    }

    private void parseInput()
    {
        userGuess = Integer.parseInt(userGuessField.getText());
    }
}

这是具有变量的类。

public class DifficultyPanel extends JPanel
{
    private SpringLayout baseLayout;

    private JButton easyButton;

    private int selectedNumber;

    public DifficultyPanel()
    {
        baseLayout = new SpringLayout();

        easyButton = new JButton("Easy");

        buildPanel();
        buildListeners();
    }

    private void buildPanel()
    {
        setBackground(Color.BLUE);
        setPreferredSize(new Dimension(200,200));

        setLayout(baseLayout);
        add(easyButton);
    }

    private void buildListeners()
    {
        easyButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent click)
                {
                    selectedNumber = (int) (Math.random() * 51);
                    System.out.println(selectedNumber);
                }
            });
    }


    public int getSelectedNumber()
    {
        System.out.println(selectedNumber);
        return this.selectedNumber;
    }
}

1 个答案:

答案 0 :(得分:0)

您正在DifficultyPanel中创建一个名为grabDiff的{​​{1}}实例作为私有字段,并尝试从中获取该数字。但是您永远不会将UserInputPanel添加到grabDiff或任何其他容器,因此用户永远不能单击其中的按钮。我怀疑你在其他地方有另一个UserInputPanel实例(在某些代码中你没有发布),那就是那个设置了数字的实例。

[编辑]

如果您想将DifficultyPanel添加到grabDiff,可以将DifficultyPanel行添加到add,如下所示:

DifficultyPanel.buildPanel