2013-10-25 4 views
0

저는 프로그래밍에 익숙하지 않습니다. 사용자가 JRadioButton을 눌러 바위, 종이, 가위 (1 플레이어 또는 2 플레이어) 모드를 선택할 수있게하는 매우 간단한 메뉴를 프로그래밍하려고했습니다. 현재 코드가 어떤 버튼이 선택되어 있는지를 청취 한 다음 int를 1 또는 2로 설정합니다. 그런 다음 해당 숫자를 사용하여 주 방법에서 열려야하는 창을 결정하지만이 방법을 사용하여 무엇을해야하는지 모르겠습니다. static 필드가 아닌 정적 메소드를 참조한다.내 프로그램에서 사용자 입력을 기반으로 창을 열려면 어떻게해야합니까?

이 코드는 모드를 설정하고 해당 int를 기반으로 어떤 창을 열지를 결정합니다.

public void actionPerformed(ActionEvent e) 
{ 
    if(p1.isSelected()) 
     mode = 1; 
    else if(p2.isSelected()) 
     mode = 2; 
} 
public static void main(String args[]) 
{ 
    RPSMenu window = new RPSMenu(); 
    window.setBounds(300, 300, 400, 100); 
    window.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    window.setVisible(true); 
    if(mode == 1) 
    { 
    Rps window1 = new Rps(); 
    window1.setBounds(300, 300, 400, 160); 
    window1.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    window1.setVisible(true); 
    window.setVisible(false); 
    } 
    else if(mode == 2) 
    { 
    P2RPS window2 = new P2RPS(); 
    window2.setBounds(300, 300, 400, 150); 
    window2.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    window2.setVisible(true); 
    window.setVisible(false); 
    } 
} 

내 전체 코드를 보는 것은이 도움이된다면

가 있습니다 :

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

public class RPSMenu extends JFrame 
implements ActionListener 
{ 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
JRadioButton p1, p2; 
int mode; 

public RPSMenu() 
{ 
    p1 = new JRadioButton(" 1 Player "); 
    p2 = new JRadioButton(" 2 Player "); 

    ButtonGroup menu = new ButtonGroup(); 
    menu.add(p1); 
    menu.add(p2); 

    JButton go = new JButton(" Go! "); 
    go.addActionListener(this); 

    Container m = getContentPane(); 
    m.setLayout(new FlowLayout()); 
    m.add(go); 
    m.add(p1); 
    m.add(p2); 
} 

public void actionPerformed(ActionEvent e) 
{ 
    if(p1.isSelected()) 
     mode = 1; 
    else if(p2.isSelected()) 
     mode = 2; 
} 
public static void main(String args[]) 
{ 
    RPSMenu window = new RPSMenu(); 
    window.setBounds(300, 300, 400, 100); 
    window.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    window.setVisible(true); 
    if(mode == 1) 
    { 
    Rps window1 = new Rps(); 
    window1.setBounds(300, 300, 400, 160); 
    window1.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    window1.setVisible(true); 
    window.setVisible(false); 
    } 
    else if(mode == 2) 
    { 
    P2RPS window2 = new P2RPS(); 
    window2.setBounds(300, 300, 400, 150); 
    window2.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    window2.setVisible(true); 
    window.setVisible(false); 
    } 
} 
} 

답변

1

귀하의 문제는 당신이 정적 메인 메소드에서 비 정적 코드를 호출하기 위해 노력하고 있다는 점이다. 해결책은 프로그램을 설정하고 실행하는 메인 메소드를 사용하는 것이 아니라 생성자 또는 init 메소드에서 클래스의 코드 자체 내의 다른 모든 것을 호출하는 것입니다.

public static void main(String[] args) { 
    // to begin a Swing application in thread-safe mannter 
    SwingUtilities.invokeLater(new Runnable() { 
    public void run() { 
     Create your GUI class; 
     MyGui myGui = new MyGui(); 
     myGui.init(); // initialize its fields, call your if blocks 
     // etc... 
    } 
    }); 
} 

이 그런 다음 init() 방법, 당신은 플레이어의 수의 사용자의 선택을 유도 할 수있는 대화 상자를 표시하고, 초기화하고 비에 GUI를 표시 할 수 있습니다 : 같은

예를 들어, 기본은 볼 수 있었다 정적 방법. 나는 당신이하고있는 것처럼 창문을 바꿀 것이 아니라 만들어 하나 JFrame의를 표시 한 후 CardLayout을 사용 전망을 (여기가 JPanel의 수 있습니다) 교환하지 않을

참고.

0

이 시도 :

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

public class RPSMenu extends JFrame 
implements ActionListener 
{ 
/** 
* 
*/ 
/* it's typically considered bad practice to allow other classes to access a 
* classes' variables, they should be accessed though a method that returns them*/ 
private static final long serialVersionUID = 1L; 
private JRadioButton p1, p2; 
private int mode = 0;//you need to know if this has been set or if it is still at default value 

public RPSMenu() 
{ 
p1 = new JRadioButton(" 1 Player "); 
p2 = new JRadioButton(" 2 Player "); 

ButtonGroup menu = new ButtonGroup(); 
menu.add(p1); 
menu.add(p2); 

JButton go = new JButton(" Go! "); 
go.addActionListener(this); 

Container m = getContentPane(); 
m.setLayout(new FlowLayout()); 
m.add(go); 
m.add(p1); 
m.add(p2); 
} 

public void actionPerformed(ActionEvent e) 
{ 
if(p1.isSelected()) 
    mode = 1; 
else if(p2.isSelected()) 
    mode = 2; 
} 
public int getMode() 
{ 
return mode; 
} 
public static void main(String args[]) 
{ 
RPSMenu window = new RPSMenu(); 
window.setBounds(300, 300, 400, 100); 
window.setDefaultCloseOperation(EXIT_ON_CLOSE); 
window.setVisible(true); 
while(window.getMode() == 0){/*wait for someone to press a button*/} 
/* the non static type of mode should be accessed only from the object in which it is used 
* however, you could also just make it static and the program would also work but that's not your only problem*/ 
Rps window1 = null; 
P2RPS window2 = null;//initilize these here so you can use them later 
if(window.getMode() == 1) 
{ 
//I am assuming this class and the other class below are your windows 
window1 = new Rps(); 
window1.setTitle("Single Player RPS"); 
window1.setBounds(300, 300, 400, 160); 
window1.setDefaultCloseOperation(EXIT_ON_CLOSE); 
window1.setVisible(true); 
} 
else if(window.getMode() == 2) 
{ 
window2 = new P2RPS(); 
window2.setTitle("Two Player RPS"); 
window2.setBounds(300, 300, 400, 150); 
window2.setDefaultCloseOperation(EXIT_ON_CLOSE); 
window2.setVisible(true); 
} 
window.setVisible(false); 
while(window1 != null && window1.isVisible()){} 
while(window2 != null && window2.isVisible()){} 
} 
} 

당신은 당신의 프로그램이 자동으로 입력을 기다리는 생각을 갖고있는 것 같다,하지만 당신은 내가 위에서했던 방법처럼 자신이 할 수있는 방법을 마련 할 수 있고, 창을 통해 입력하는 것은 콘솔에서 가져 오는 것과는 다릅니다.