Java - Number Game - Multiple ActionListener in one class

I only learn Java for 6-7 weeks, so I apologize in advance if my code is sloppy or the terminology is disabled. I am trying to create a program that creates a random number and allows the user to guess until they get the correct number. For me, this has no real purpose other than learning experience.

I have a basic working program, I just want to add other elements in order to improve it and gain experience.

The program runs in JFrame and has a JTextField so that the user can enter his guess. I have an ActionListener setting for a JTextField. I would like to add a start button that appears at the beginning of the game. When the user clicks the start button, the JTextField should become active. In addition, when the user clicks on the correct answer, I would like to use the start button to reset the program. I experimented with several ways to do this without success. I believe this will require multiple ActionListeners in the same class. I'm not even sure if this is possible?

Here is my code. Thanks in advance for any help.

import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class JMyFrame2 extends JFrame implements ActionListener { Random num = new Random(); int computerGenerated = num.nextInt(1000); public int userSelection; JTextField numberField = new JTextField(10); JLabel label1 = new JLabel(); Container con = getContentPane(); int previousGuess; // constructor for JMyFrame public JMyFrame2(String title) { super(title); setSize(750, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); label1 = new JLabel( "I have a number between 1 and 1000 can you guess my number?" + "Please enter a number for your first guess and then hit Enter."); setLayout(new FlowLayout()); add(numberField); add(label1); System.out.println(computerGenerated); numberField.addActionListener(this); } public void actionPerformed(ActionEvent e) { userSelection = Integer.parseInt(numberField.getText()); con.setBackground(Color.red); if (userSelection == computerGenerated) { label1.setText("You are correct"); con.setBackground(Color.GREEN); } else if (userSelection > computerGenerated) { label1.setText("You are too high"); } else if (userSelection < computerGenerated) { label1.setText("You are too low"); } } } public class JavaProgram5 { public static void main(String[] args) { JMyFrame2 frame2 = new JMyFrame2("Assignment 5 - Number Guessing Game"); frame2.setVisible(true); } } 
+5
source share
2 answers

I am sure that you can have several action listeners. In fact, your class should not implement it.

Start by removing the actionPerformed method and replace this line:

  numberField.addActionListener(this); 

Wherein:

  numberField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { userSelection = Integer.parseInt(numberField.getText()); con.setBackground(Color.red); if (userSelection == computerGenerated) { label1.setText("You are correct"); con.setBackground(Color.GREEN); } else if (userSelection > computerGenerated) { label1.setText("You are too high"); } else if (userSelection < computerGenerated) { label1.setText("You are too low"); } } }); 

You can add another action listener to the launch button that you plan to add following this pattern using an anonymous class. (This should not be an anonymous class; it was just a demonstration.)

+5
source

As janos said, adding an action listener to each button does the job well, but in large code, when you need to add a lot of buttons, it doesn't look too neat, I suggest you use setActionCommand() for the JButton that you create, using simple, you implement the ActionListener in the JFrame just like you do, but after each button add

 button.addActionListener(this); button.setActionCommand("commandname") 

And you can do it just like buttons as you want, now, to run these commands correctly, your action, performed by you, should look like this:

 @Override public void actionPerformed (ActionEvent e) { String cmd = e.getActionCommand(); switch(cmd) { case "action1": // Do something break; case "action2": // Do something else break; case "potato": // Give Mr. chips a high five break; default: // Handle other cases break; } } 

And so on, again, another solution works fine, I just personally think it is very neat, especially in code where you have a lot of listeners for actions.

+4
source

Source: https://habr.com/ru/post/1232350/


All Articles