2 Replies - 186 Views - Last Post: 03 February 2018 - 10:56 PM Rate Topic: -----

#1 NotAJavaPro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-February 18

JButton not responding when clicked

Posted 03 February 2018 - 09:02 PM

Hi, I'm working on a cow and bulls game (although we're using a different name for it). This is the first time I'm using an array to make the buttons and for some reason my button when clicked isn't responding and I don't know why. Everything seems normal and I'm not getting an error.
All buttons other than 'new game' is supposed to be disabled until new game is clicked and the it should produce a random unique three digit number and enable all the number keys but nothing happens when I click the 'new game' (button 11). Here is my code:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;



class Game extends JFrame{

    //there will be 10 number buttons
    public JButton[] button;
    //checks for win/lose
    boolean checkWin = false;
    //will store guesses
    String guess;
    //random number generator
    int num1;
    int num2;
    int num3;
    String threeDigits;
    
    
    Container contentPane = getContentPane();
    GamePanel panel = new GamePanel();
    JTextField display = new JTextField("Play Ball!!!");
    
    //non numeric buttons
    JButton button11 = new JButton("New Game");
    JButton button12 = new JButton("Clear");
                
      
    
    
    
    void initialize() {
        
        //setting metal look for Mac
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        //new instance for observer
        ButtonObserver observer = new ButtonObserver();
        
        //font of the textfield
        Font font1 = new Font("Courier New", Font.BOLD, 18);
        
        //display text on the North with font1 and editing disabled
       display.setFont(font1);
       display.setEditable(false);
       contentPane.add(display, BorderLayout.NORTH);
        
        //10 number buttons
        button = new JButton[10];
        
        //12 button grid on the south of content pane
        panel.setLayout(new java.awt.GridLayout(3, 4));
        contentPane.add(panel, BorderLayout.SOUTH);
        
        //adding all 12 buttons on the south of the content pane
        for (int i = 0; i < 10; i++) {
            button[i] = new JButton("" + i);
            button[i].addActionListener(observer);
            panel.add(button[i]);
            button[i].setEnabled(false);
            System.out.println(button[0]);
        }
        panel.add(button11);       
        panel.add(button12);
        button12.setEnabled(false);
        
   
        
    }
    
    
    public class ButtonObserver implements ActionListener{

        @Override
        
        public void actionPerformed(ActionEvent event){
            //Object source = event.getSource();
            //JButton button = (JButton) source; // type casting
            
            //new game button function
            if(event.getSource() == button11){
                    System.out.println("Button pressed");
                    //generating random number
                    int n = (int) (Math.random() * 1000);
                    num1 = n/100;
                    num2 = (n - (num1 * 100)) / 10;
                    num3 = n - ((num1 * 100) + (num2 * 10));
                    
                    //checking for duplicates amd regenrating if not unique
                    while(num1 == num2 || num2 == num3 || num1 == num3){
                    n = (int) (Math.random() * 1000);
                    num1 = n/100;
                    num2 = (n - (num1 * 100)) / 10;
                    num3 = n - ((num1 * 100) + (num2 * 10));
                    }
                    display.setText("Key = " + n);
                    //threeDigits = Integer.toString(n);
                    //enable buttons
                    button[0].setEnabled(true);
                    for(int i = 0; i < 10; i++){
                        button[0].setEnabled(true);
                    }                                    
            }
            
        }
    }
    
}
 


Is This A Good Question/Topic? 0
  • +

Replies To: JButton not responding when clicked

#2 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 801
  • View blog
  • Posts: 3,271
  • Joined: 13-June 14

Re: JButton not responding when clicked

Posted 03 February 2018 - 10:41 PM

You aren't adding a listener for either of button11 or button12.

The names of your buttons aren't very meaningful either. Using meaningful names help readability.
Was This Post Helpful? 0
  • +
  • -

#3 NotAJavaPro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-February 18

Re: JButton not responding when clicked

Posted 03 February 2018 - 10:56 PM

I cannot believe I missed that. Thank you so much, I've been stuck on it longer than I should have. Also my buttons are literally going to say 0-9 and new game and clear. You basically click on a number to input your guess. I figured those were pretty relatable button names.

This post has been edited by ndc85430: 03 February 2018 - 11:03 PM
Reason for edit:: Removed quote of previous post. Please just press "Reply" in future.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1