2015-08-25 48 views
1
import java.util.Scanner; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 
import javax.swing.WindowConstants; 


    class DD { 
    public static void main(String[] args){ 

    JFrame myFrame = new JFrame("#########"); 
    myFrame.setSize(640,480); 
    myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    myFrame.setVisible(true); 
    JTextArea textArea = new JTextArea(); 
    myFrame.add(textArea); 
    new JTextArea(); 
    textArea.setEditable(false); 

    //System objects 
    Scanner in = new Scanner(System.in); 
    boolean running = true; 
    textArea.append("\t\n########################################"); 
    textArea.append("\t\n>#############"); 
    textArea.append("\t\n>Th#############11!"); 
    textArea.append("\t\n>Typ#############enture!"); 
    textArea.append("\t\n########################################"); 
    String input = in.nextLine(); 
    if(input.equals("start")){ 
      { ///beginning of story. 
     if(running) 
     textArea.append("\t\nYo#############."); 
     textArea.append("\t\n#############"); 
     textArea.append("\t\n1.#############t."); 
     textArea.append("\t\n2.G#############t."); 
     String input1 = in.nextLine(); 
     if(input1.equals("1")){ 
      textArea.append("\n>Y#############"); 
      textArea.append("\n>#############"); 
      textArea.append("\n>A#############"); 
      textArea.append("\n>1.#############"); 
      textArea.append("\n>2.#############"); 
     if(input.equals("1")){ 
       textArea.append("\n>#############"); 
       textArea.append("\n>#############"); 
       textArea.append("\n>Ga#############d."); 
        } 
     if(input.equals("2")){ 
       textArea.append("\n>#############"); 
       textArea.append("\n>#############"); 
       textArea.append("\n>Y#############ars"); 
       textArea.append("\n>Y#############"); 
        } 
     } 
     else if(input1.equals("2")){ 
      textArea.append("\n>Y#############."); 
      textArea.append("\n>Y#############."); 
      } 
     } 
    } 
} 
} 

這是我的文本冒險遊戲,我被困在如何獲得用戶輸入。我已閱讀關於'動作監聽器'我不知道如何使用它,但我真的希望輸入像控制檯或終端/ cmd程序一樣輸入。用戶只需輸入1,2或3來執行一個動作。如何在文本冒險遊戲中獲取用戶輸入(Java/Applet)

+0

可能重複http://stackoverflow.com/questions/5287538/how-to-get-basic-user-input-for-java – Deh

+0

@Deh我不這麼認爲,我認爲(或者我希望)OP正嘗試使用GUI來獲取用戶輸入,但它們只是不明白事件驅動環境的基本概念 – MadProgrammer

+0

@MadProgrammer哦,是的,這是正確的被標題欺騙,那麼這一個可能是http://stackoverflow.com/questions/16390503/java-swing-getting - 從jtextfield輸入 – Deh

回答

2

轉儲Scanner開始。使用JTextField。你要明白,你在一個事件驅動的環境中運行,這意味着你需要利用Observer Pattern的通知時,一些通過採取一看Creating a GUI With JFC/SwingHow to Use Text FieldsHow to Write an Action Listeners更多細節改變

開始

+0

我有掃描儀,因爲我的遊戲在eclipse中的控制檯中運行。 – eels11

+2

那麼,如果你想使用GUI,那麼使用GUI。如果你想使用控制檯,那麼使用控制檯,而不是兩個 – MadProgrammer

+0

我忘了刪除它,因爲目前我正在學習讓我的遊戲在GUI中運行,當我測試我的遊戲以查看故事是否正常工作時使用控制檯並刪除Jframe代碼。我正在使我的遊戲在GUI中保持穩定。 – eels11

0

對於初學者這裏有一個泄漏:

myFrame.add(textArea); 
**new JTextArea();** 
textArea.setEditable(false); 

(該粗體位於的主第一行的行,並且需要將被修改或刪除)

關於您的實際Q,我認爲既然它是意​​味着是一個基於文本的遊戲,你可以做這樣的事情來接受用戶輸入:

(...) 

public static void main(String[] args) 
{ 
    (...) 
    // create a scanner to read the command-line input 
    Scanner scanner = new Scanner(System.in); 
    bool running = true; 

    init(); 

    //game loop 
    while (running) 
    { 
    if(scanner.next() == 1) 
    { 
     // do this 
    }else if (== 2) 
    { 
     // do that 
    }//etc 
    //and simply change running to false, when you want the game to end 
    } 
    (...) 
} 

//this is simply a function to present the initial choices and general info to the user. 
    private void init(){System.out.println("Add instructions for the user here, e.g. telling them that they have to press 1,2, or 3");} 
相關問題