2017-09-20 2 views
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(); 
    } 
} 

아이디어는 다행스럽게도 필자는 큰 내 다른 프로젝트의 모든 허브 수있을 것입니다 지점에 도착하는 것입니다 이것은 더 나은 방법이 없다면 일련의 메소드를 사용할 수 있기를 원하는 이유입니다.

+1

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

답변

0

다른 클래스의 static 메서드에 액세스하려고합니다. 정적 메소드가 같이 호출해야

,

DefinedClassName.methodName(); 

따라서, 귀하의 경우,

GameCenter2.Lmao(); 

작동합니다.

정적이 아닌 메서드 인 경우 Object를 만들고 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); 
} 
관련 문제