【问题标题】:How to use a method in another class [duplicate]如何在另一个类中使用方法[重复]
【发布时间】:2018-03-01 19:28:36
【问题描述】:

我是 Java 和编码新手,但我学得很快。我一直在努力学习 JFrame 按钮,但我无法让我的按钮执行除打印行之外的任何操作。谁能解释一下如何让按钮运行方法“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();
    }
}

我们的想法是希望能够为我所有不同的项目建立一个伟大的中心,这就是为什么我希望能够使用一系列方法,除非有更好的方法。

【问题讨论】:

  • GameCenter2.Lmao() 应该可以工作。您正在尝试调用在另一个类中定义的方法。所以,你应该使用类名,因为方法是static。如果它是一个非静态方法,你应该创建一个 Object 并像 object.method() 这样使用。

标签: java methods


【解决方案1】:

您正在尝试从另一个类访问 static 方法。

静态方法应该像这样调用,

DefinedClassName.methodName();

所以,在你的情况下,

GameCenter2.Lmao();

应该可以。

如果它是一个非静态方法,你应该创建一个对象并像 object.method() 一样使用。

例子,

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

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

对代码的小改进,

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);   
}

【讨论】:

    猜你喜欢
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    相关资源
    最近更新 更多