重置java字段未解决的复杂问题

vshtjzan  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(235)

我试图计算用户在使用swing的javagui程序中尝试猜测数字的次数。
我创建了这些变量来跟踪尝试次数:

JLabel lblNumberOfTries = new JLabel("Number of tries:");
    lblNumberOfTries.setBounds(136, 219, 90, 13);
    getContentPane().add(lblNumberOfTries);

    txtNumberOfTries = new JTextField();
    txtNumberOfTries.setBounds(223, 219, 42, 13);
    getContentPane().add(txtNumberOfTries);
    txtNumberOfTries.setColumns(10);

我需要在游戏重启时重置它们。出于某种原因,我可以重置java字段,但不能重置标签:

txtNumberOfTries.setText(""); // this is ok
lblNumberOfTries.setText(""); // this gives an error

这就是我得到的错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    lblNumberOfTries cannot be resolved

    at GuessingGame.newGame(GuessingGame.java:50)
    at GuessingGame.main(GuessingGame.java:110)

这是我的程序中标签和字段所在的类和函数:

import javax.swing.JFrame;      

    public class GuessingGame extends JFrame {
        public GuessingGame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Tim's Hi-Lo Guessing Game");
        getContentPane().setLayout(null);

        JLabel lblTitle = new JLabel("Tim's Hi-Lo Guessing Game");
        lblTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
        lblTitle.setBounds(0, 10, 436, 32);
        getContentPane().add(lblTitle);

        JLabel lblGuessANumber = new JLabel("Guess a number between 1 and 100:");
        lblGuessANumber.setBackground(new Color(240, 240, 240));
        lblGuessANumber.setHorizontalAlignment(SwingConstants.RIGHT);
        lblGuessANumber.setBounds(147, 54, 215, 13);
        getContentPane().add(lblGuessANumber);

        txtGuess = new JTextField();
        txtGuess.addActionListener((ActionEvent e) -> {
            checkGuess();
        });
        txtGuess.setHorizontalAlignment(SwingConstants.RIGHT);
        txtGuess.setBounds(260, 122, 27, 19);
        getContentPane().add(txtGuess);
        txtGuess.setColumns(10);

        JButton btnGuess = new JButton("Guess!");
        btnGuess.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                checkGuess();
            }
        });
        btnGuess.setBounds(172, 121, 85, 21);
        getContentPane().add(btnGuess);

        lblOutput = new JLabel("Enter a number above and click Guess!");
        lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
        lblOutput.setBounds(58, 196, 350, 13);
        getContentPane().add(lblOutput);

        JLabel lblNumberOfTries = new JLabel("Number of tries:");
        lblNumberOfTries.setBounds(136, 219, 90, 13);
        getContentPane().add(lblNumberOfTries);

        txtNumberOfTries = new JTextField();
        txtNumberOfTries.setBounds(223, 219, 42, 13);
        getContentPane().add(txtNumberOfTries);
        txtNumberOfTries.setColumns(10);
}

public void newGame() {
    theNumber = (int) (Math.random() * 100 + 1);
    txtNumberOfTries.setText("");
    lblNumberOfTries.setText("");

}

}

我做错什么了?

vnzz0bqm

vnzz0bqm1#

好的,在看到完整的文件之后,有两个要点:
您需要定义一个字段 private JLabel lblNumberOfTries; 在班上
您还需要从guessinggame构造函数(定义的方法)的第98行中删除'jlabel' public GuessingGame() 因此它成为 lblNumberOfTries = new JLabel("Number of tries:"); . 这将阻止您重新说明变量。
在某个时候,您还需要添加一个名为 numberOfTries

gt0wga4j

gt0wga4j2#

从代码的结构来看,lblnumberoftries似乎是全局定义的(这允许您在 newGame 函数)。
在这种情况下,我猜这个问题是由于 JLabel lblNumberOfTries = new JLabel("Number of tries:"); 重新说明变量。
试着把这行改成 lblNumberOfTries = new JLabel("Number of tries");

相关问题