0
자바에서 쿠키 크리커 복제본을 만들어 내 Java 기술을 연습하고 있는데 작은 문제가 있습니다. ActionListener 클래스에서 액세스하려는 기본 클래스에 선언 된 변수가 있습니다. 다음은 ActionListener 클래스의 일부 샘플 코드입니다. int 변수 (예 : clicks, grandamaCost) 및 JTextFields (예 : display, cpsDisplay)는 모두 주 클래스에 있습니다. 이 코드가 다른 클래스에서 작동 할 수 있도록 기본 클래스의 변수에 어떻게 액세스 할 수 있는지 궁금합니다. 감사!동일한 프로젝트의 다른 클래스에서 주 클래스의 변수에 어떻게 액세스합니까?
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
button(b.getText());
}
/**
*
* @param input the label of the buttons being clicked.
*/
public void button(String input) {
switch (input) {
case "Cookie":
clicks++;
display.setText("Cookies: " + clicks + "");
cpsDisplay.setText("CPS: " + cps);
break;
case "Buy grandma":
if (clicks >= grandmaCost) {
grandmas++;
clicks = clicks - grandmaCost;
grandmaCost = (int) ((.15 * grandmaCost) + grandmaCost);
cps++;
}
display.setText("Cookies: " + clicks + "");
prices[0].setText("$" + grandmaCost);
cpsDisplay.setText("CPS: " + cps);
break;
case "Buy monkey":
if (clicks >= monkeyCost) {
monkeys++;
clicks = clicks - monkeyCost;
monkeyCost = (int) ((.15 * monkeyCost) + monkeyCost);
cps = cps + 2;
}
display.setText("Cookies: " + clicks + "");
prices[1].setText("$" + monkeyCost);
cpsDisplay.setText("CPS: " + cps);
break;
case "Buy Teemo":
if (clicks >= teemoCost) {
teemos++;
clicks = clicks - teemoCost;
teemoCost = (int) ((.15 * teemoCost) + teemoCost);
cps = cps + 3;
}
display.setText("Cookies: " + clicks + "");
prices[2].setText("$" + teemoCost);
cpsDisplay.setText("CPS: " + cps);
break;
}
}
시간을 그것을 사용하도록 설정하려면 위의 방법을 사용 ttp : //stackoverflow.com/questions/19416446/how-do-i-access-variables-from-the-main-class-from-another-class-java –
이 질문을 이미 받았습니다. –