0
저는 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는()'작동합니다. 다른 클래스에 정의 된 메서드를 호출하려고합니다. 따라서 클래스 이름은 메서드가 '정적'이므로 사용해야합니다. 그것이 비 정적 메소드라면, 당신은 Object를 생성하고'object.method()'와 같이 사용해야합니다. – Sridhar