0

I'm new to Java, and coding, but I'm learning very quickly. I've been really trying to learn JFrame buttons, but I'm unable to get my button to do anything expect print lines. Can someone please explain how to get the button to run the method "Lmao()":

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;

public class GameCenter2

{
    public static void Lmao() {
        Scanner scan = new Scanner(System.in);
        boolean run = true;
        while (run) {
            int random = (int) (Math.random() * 100);
            System.out.println("Pick a number 1 - 100");
            int response = scan.nextInt();
            int difference = java.lang.Math.abs(response - random);
            if (random == response) {
                System.out.println("Congradulations, you win!  The number was " + random);
            } else {
                System.out.println("WRONG! You were " + difference + " numbers off. The number was " + random + ".");
            }
            System.out.println("Would you like to play again?  Yes or No.");

            String response1;
            response1 = scan.next();

            if (response1.equals("Yes")) {
                run = true;
            } else {
                run = false;
            }
        }
    }

    public static void main(String[] args) {
        Login();
        Button frm = new Button("GameCenter");

        frm.setSize(200, 100);
        frm.setVisible(true);

    }
}

class Button extends JFrame implements ActionListener {
    boolean guess;
    JButton bChange; // reference to the button object

    // constructor for ButtonFrame2
    Button(String title) {
        super(title); // invoke the JFrame constructor
        setLayout(new FlowLayout()); // set the layout manager

        // construct a Button
        bChange = new JButton("Guessing Game");

        // register the ButtonFrame2 object as the listener for the JButton.
        bChange.addActionListener(this);

        add(bChange); // add the button to the JFrame
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent evt) {
        Lmao();
    }
}

The idea is to hopefully get to the point where I will be able to great a Hub for all of my different projects, which is why I want to be able to use a series of Methods unless there is a better way.

Aldeguer
  • 801
  • 10
  • 31
  • 1
    `GameCenter2.Lmao()` should work. You're trying to call a method that is defined in another class. So, you should use the class name as the method is `static`. If it's a non-static method, you should create a Object and use like `object.method()`. – Sridhar Sep 20 '17 at 16:20

1 Answers1

0

You're trying to access a static method from another class.

Static methods should be called like,

DefinedClassName.methodName();

So, in your case,

GameCenter2.Lmao();

should work.

If it's a non-static method, you should create a Object and use like object.method().

Example,

class MyClass {
  public void myMethod() {
     // Do something
  }
}

class MyMainClass {
  public static void main(String[] args) {
    MyClass object = new MyClass();
    object.myMethod();
  }
}

A small improvement to your code,

public static void Lmao() {
    Scanner scan = new Scanner(System.in);
    boolean isContinue = true;
    do {   
         int random = (int)(Math.random() * 100);
         System.out.println("Pick a number between 1 - 100");
         int response = scan.nextInt();
         int difference = java.lang.Math.abs( response - random );
         if (random == response) {
             System.out.println("Congradulations, you win!  The number was " + random);
         } else {
             System.out.println("WRONG! You were " + difference + " numbers off. The number was " + random + ".");
         }

         System.out.println("Would you like to play again?  Yes or No.");

         isContinue = scan.next().trim().equalsIgnoreCase("Yes");
    } while(isContinue);   
}
Sridhar
  • 1,390
  • 14
  • 26