Alex Melillo
Alex Melillo

Reputation: 3

Issues with ActionListener

I'm trying to write a program that asks you to click on a button to generate a random number and then makes the user try to guess what that number is.

Right now I'm working on getting a button click to generate a random number and to tell the user that the random number has been generated through a JTextPane. For whatever reason this part of the code doesn't seem to be executing correctly.

I have already instanced the object in the main class and I've added an actionlistener to the button. I have no idea what's causing this not to work appropriately:

package GUIs;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JToggleButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Principal extends JFrame {
//Elements
private JPanel contentPane;
private JTextField attemptField;
private JTextPane hint;
private JButton btnGenerate;
private JButton btnEnter;

//Events
private GenerateRandomNumber genRanNum;

//Misc variables
int ranNum = 1;
private JTextField textField;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Principal frame = new Principal();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Principal() {

    //Generates JPanel
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    //Generates text field where the user will write the number
    attemptField = new JTextField();
    attemptField.setBounds(130, 59, 130, 26);
    contentPane.add(attemptField);
    attemptField.setColumns(10);

    //Generates the hint
    hint = new JTextPane();
    hint.setBackground(SystemColor.window);
    hint.setText("First, generate a new number!");
    hint.setBounds(130, 110, 192, 19);
    contentPane.add(hint);

    //Generates the button that should call for the creation of the new number
    btnGenerate = new JButton("Generate number");
    btnGenerate.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
    btnGenerate.setBounds(165, 153, 117, 29);
    contentPane.add(btnGenerate);
    btnGenerate.addActionListener(genRanNum);

    //Generates button that will be used to enter the number
    btnEnter = new JButton("Enter");
    btnEnter.setBounds(269, 59, 58, 29);
    contentPane.add(btnEnter);
}

public class GenerateRandomNumber implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        ranNum = (int) (Math.random() * 101);

        hint.setText("The number has been created!");
    }

}

}

Upvotes: 0

Views: 32

Answers (1)

camickr
camickr

Reputation: 324098

private GenerateRandomNumber genRanNum;

Your random number generator is null. I don't see where you ever create an instance of it.

btnGenerate.addActionListener(genRanNum);

The above statement does nothing because the variable is null. (ie. no ActionListener has been added to the button)

I don't even know how your GenerateRandomNumber class would compile because it doesn't have access to the "hint" variable of your "Principal" class.

Don't make the GenerateRandomNumber class a public class. Instead make it an inner class of the "Principal" class.

Read the section from the Swing tutorial on How to Use Actions for an example showing how to use inner classes. Note you can even use an Action instead of an ActionListener.

Upvotes: 1

Related Questions