2012-04-12 2 views
-1

나는 게임을 만들고 있는데 플레이어 1과 플레이어 2라는 두 개의 레이블이 있습니다. 게임을 시작하기 전에 플레이어 1과 플레이어 2의 이름을 삽입 할 수있는 대화 상자가 나타납니다. 아래 코드는 . 레이블을 삽입 된 이름으로 변경해야합니다.이름을 삽입하는 대화 상자를 만드는 방법은 무엇입니까?

public void gui(); 
{ 

    BorderLayout borderlayout = new BorderLayout(); 

    JFrame frame = new JFrame("Games"); 
    frame.setVisible(true); 
    frame.setSize(600,400); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Panel p2 = new Panel(); 
    p2.setLayout(new BorderLayout()); 
    p2.add(new Label("Player 1"), BorderLayout.WEST); //player 1 label 
    p2.add(new Label("Player 2"), BorderLayout.EAST); //player 2 label 
    p2.add(new Label("Board GAME"), BorderLayout.NORTH); 
    p2.setBackground(new Color(156, 93, 82)); 
    p2.setFont(new Font("sansserif", Font.BOLD, 18)); 
    frame.add(p2); 
} 
+1

HTTP를 확인하는 방법을 사용할 수 있습니다 : //docs.oracle.com/javase/tutorial/uiswing/components/dialog.html –

답변

1

:

JLabel player1Lbl = new JLabel("Player 1"); 
p2.add(player1Lbl, BorderLayout.WEST); 

그런 다음 포함한 JOptionPane를 사용하여 플레이어의 텍스트를 얻을

String player1Name = JOptionPane.showInputDialog("Player 1 enter your name"); 

그런 다음 사용자가 아무 것도 입력 한 경우

player1Lbl.setText(player1Name); 

이 방법은 확인하지 않습니다 레이블을 설정, 그래서 문자열은 공백 또는 널 (null)이 될 수있다, 당신은하지만 ...

+0

위대한 작품 :). 감사 –

2

당신은 카스 JOptionPane 쉽게 그것을 할 수 있습니다

당신은 먼저 JLabels에 변수를 할당 할 필요가
String p1name = JOptionPane.showInputDialog(frame,"Please enter player 1 name","Specify name",JOptionPane.PLAIN_MESSAGE,); 
관련 문제