실행하려고하면 닫을 수없는 빈 프레임이 계속 표시되고 Eclipse에서 프로그램을 종료해야합니다. 나는 문제가 while 루프 (코드 블록 내리막 길의 약 2/3)의 결과라고 꽤 확신하지만, 나는이while 루프에서 Java JFrame이 제대로 작동하지 않습니다.
public Draw()
{
super("Uno");
JMenuBar bar = new JMenuBar(); // create menu bar
JMenu gameMenu = new JMenu("Play Game");
JMenu quitMenu = new JMenu("Quit"); //Quit Program
bar.add(gameMenu); // add Add menu to menu bar
bar.add(quitMenu); // add quit button to menu bar
setJMenuBar(bar); // set menu bar for this application
window = new JDesktopPane();
add(window);
gameMenu.addMouseListener(
new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
Dimension size = window.getSize();
double height = size.getHeight();
double width = size.getWidth();
JInternalFrame frame = new JInternalFrame("Uno", true, true, true, true);
JFrame input = new JFrame();
input.setTitle(" ");
input.setSize(350, 155);
input.setLayout(new FlowLayout());
input.setLocationRelativeTo(null);
input.setVisible(true);
JLabel label1 = new JLabel("How many total players? (2-10)");
final JTextField text1 = new JTextField(3);
JLabel label2 = new JLabel("How many computer players? *");
final JTextField text2 = new JTextField(3);
JLabel label3 = new JLabel("* Computer players must be <= total number of players");
JButton confirm = new JButton("OK");
label1.setVisible(true);
label2.setVisible(true);
label3.setVisible(true);
text1.setVisible(true);
text2.setVisible(true);
confirm.setVisible(true);
input.add(label1);
input.add(text1);
input.add(label2);
input.add(text2);
input.add(label3);
input.add(confirm);
while(valid == false)
{
confirm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String players = text1.getText();
String computers = text2.getText();
int temp1 = Integer.parseInt(players);
System.out.println("Number of players = " + temp1);
int temp2 = Integer.parseInt(computers);
System.out.println("Number of computers = " + temp2);
if (temp1 >= 2 && temp1 <= 10)
if (temp2 <= temp1 && temp2 >= 0)
valid = true;
} //close actionPerformed
}); //close ActionListener
} //close while loop
} //close mouseClicked
} //close mouseListener
} //close Draw
를 해결하는 방법을 알아낼 수 없었다 처음에는 while 루프가 계속 false를 반환했기 때문에 무한히 프레임을 그리는 문제가 발생했습니다. 이 문제는 while 루프에서 구성 요소를 제거하고 위에있는대로 배치하여 해결되었습니다. 이제 실행하면 얻을 수있는 것은 다음과 같습니다. picture
이 프로그램의 목표는 우노 게임을 만드는 것이지만, 첫 번째 단계는 플레이어 수를 결정하는 것입니다. 이것이 내가하는 방법을 아는 유일한 방법이며, 이것은 그래픽으로 작업 한 두 번째 프로그램이므로 자바의 전문가는 아닙니다. 나는 내가 며칠 동안 붙어 있었기 때문에 내가 얻을 수있는 어떤 도움에 감사 할 것이다.
언제 'valid'의 값을 변경 하시겠습니까? (힌트 : 무한 루프 동안 CPU를 쿠셋 한 이후로 결코 호출 할 수없는 이벤트 핸들러에 있음) –
위의 유효 선언을 추가하는 것을 잊어 버렸습니다. 무승부의 바깥쪽에'private static boolean valid = false;를 선언했습니다. 그게 무슨 뜻입니까? – matthew65456
나는 그 세부 사항을 놓쳤다. 그러나 그것은 진짜 문제와는 무관하다. 아래 내 대답을 참조하십시오. –