samedi 11 décembre 2021

How do I resolve swing GUI error due to compile error? [duplicate]

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on token "void", record expected void is an invalid type for the variable actionPerformed

at GuessingGame.<init>(GuessingGame.java:66)
at GuessingGame.main(GuessingGame.java:78)

This is the error I am getting and I have not had any luck fixing it.

Some context for what I am doing with this project. Full transparency I am using a window builder in the IDE. Though I have gone back into the source and manipulated a lot of the prebuilt code that gets dropped in from the builder. I am trying to learn how to build GUI in Java, and was hoping this builder would allow me to deconstruct the prebuilt code, however, I feel like I may have entered potential bad implimentation and I have not been able to identify. There are only 2 lines in this code throwing this error and I am unsure what the solution is.

```
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuessingGame extends JFrame {
    private JTextField txtGuess;
    private JLabel lblOutput;
    private int theNumber;
    public void checkGuess() {
        String guessText = txtGuess.getText();
        String message = "";
        int guess = Integer.parseInt(guessText);
        if (guess < theNumber)
            message = guess + " is too low. Try again.";
        else if (guess > theNumber)
            message = guess + " is too high. Try again.";
        else
            message = guess + " is correct. You win!";
        lblOutput.setText(message);;
    }
    public void newGame() {
        theNumber = (int)(Math.random() * 100 + 1);
    }
    public GuessingGame() {
        setBackground(new Color(240, 240, 240));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Arthor's Hi Lo Guessing Game");
        getContentPane().setLayout(null);
        
        JLabel lblNewLabel = new JLabel("Arthor's Hi Lo Guessing Game");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setBounds(10, 43, 414, 37);
        getContentPane().add(lblNewLabel);
        
        JLabel lblNewLabel_1 = new JLabel("Guess a number between 1 and 100:");
        lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
        lblNewLabel_1.setBounds(32, 106, 204, 37);
        getContentPane().add(lblNewLabel_1);
        
        txtGuess = new JTextField();
        txtGuess.setBounds(279, 114, 107, 20);
        getContentPane().add(txtGuess);
        txtGuess.setColumns(10);
        
        JButton btnNewButton = new JButton("Guess!");
        btnGuess.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                checkGuess();
            }
        });

        //btnNewButton.addActionListener(new ActionListener() {
            //public void actionPerformed(ActionEvent e) {
            //}
        btnNewButton.setBounds(172, 169, 101, 39);
        getContentPane().add(btnNewButton);
        
        lblOutput = new JLabel("Enter a number above and click Guess!");
        lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
        lblOutput.setFont(new Font("Tahoma", Font.PLAIN, 8));
        lblOutput.setBounds(133, 219, 192, 14);
        getContentPane().add(lblOutput);
    }
    public static void main(String[] args) {
        GuessingGame theGame = new GuessingGame();
        theGame.newGame();
        theGame.setSize(new Dimension(450,300));
        theGame.setVisible(true);
        // TODO Auto-generated method stub

    }
}
```



Aucun commentaire:

Enregistrer un commentaire