2

そのため、JTextField を使用してユーザー入力を文字列の形式で取得する必要がありますが、この部分は実行できますが、メソッドから文字列を取得してプログラムの他の場所で分析するのに問題があります。私はこれまでに持っています:

 import javax.swing.* ;
 import java.awt.event.* ;
 import java.util.* ;
 class Games extends JFrame implements ActionListener
{
  JPanel pnl = new JPanel() ;
  JTextField input = new JTextField(38) ;
  JTextArea output = new JTextArea(6, 37) ;
  JScrollPane pane = new JScrollPane(output) ;
  String inputString ;

public Games()
{
    super("Text Based RPG") ;
    setSize(600,200) ;
    setDefaultCloseOperation(EXIT_ON_CLOSE) ;
    output.setLineWrap(true) ;
    output.setWrapStyleWord(true) ;
    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) ;
    output.setEditable(false) ;
    pane.getVerticalScrollBar().setValue(100); //scrollbar always at bottom
    input.requestFocus() ;

    /*Edit: Should I call for the input string here, e.g. output.append(inputString)
     i get the compilation error message:
    Games.java:29: error: cannot find symbol
    output.append(inputString) ;
                  ^
    symbol:   variable inputString
    location: class Games
    */

    add(pnl) ;
    pnl.add(pane) ;
    pnl.add(input) ;

    input.addActionListener(this) ;

    setVisible(true) ;
}

public void actionPerformed(ActionEvent event) 
{
    inputString = input.getText() ;
    input.setText("") ;
}         
/*The string i need to analyse is inputString, but I cannot, get it out of this method
  any help you guys can give me would be greatly appreciated. */

public static void main(String[] args) 
{
    Games gui = new Games() ;
}
}

**********************************
    try
    {
        a = reader.readLine() ;
    }
    catch (IOException e ) 
    {
        System.out.println("An Input Error Has Occured") ;
    }
    if (a.indexOf("hide") != -1 ) switchvariable = 'b' ;
    if  ((a.indexOf("exit") != -1 ) || (a.indexOf("leave") != -1) || (a.indexOf("out") != -1)) switchvariable = 'a' ;
    if ((a.indexOf("exit") == -1) && (a.indexOf("hide") == -1) && (a.indexOf("leave") == -1) && (a.indexOf("out") == -1)) switchvariable= 'c' ;
    String outcome = "" ;
    switch (switchvariable) 
    {
    case 'a' : outcome = "You exit the tavern to be discovered by\na group of bandits!\n" ; i = -1 ; break ;
    case 'b' : outcome = "You hide behind the bar for 10 minutes until\nyou can no longer hear the group." ; i = -1 ; break ;
    case 'c' : outcome = "You must choose between the two given options, exit the tavern, or hide." ; break ; // the variable i will still be greater
    //than 1 so the loop will continue until you reach an agreeable option, which in this case is either hiding, or addressing the group of people
    }
    System.out.println(outcome) ;           

私が理想的にしたいのは、ユーザー入力にテキストフィールドを使用することです。そのため、出力テキストエリアにテキストを配置する必要があります。これは、ユーザー入力に応じて変更する必要があります。使用したいコードの例アスタリスクの下に表示されます

4

1 に答える 1

3

私の理解が正しければ、ユーザーがテキスト フィールドで Enter キーを押したときに、テキスト フィールドに入力されたテキストを出力テキスト領域に追加したいと考えています。

Enter キーを押すと、テキスト フィールドでアクション イベントが発生します。クラスでこのイベントを聞きます。したがって、現時点では、値をテキスト領域に追加するだけです。

Swing はイベントベースです。

public void actionPerformed(ActionEvent event) {
    String inputString = input.getText() ;
    output.append(inputString);
    input.setText("");
}

コンストラクター内のコードは、実行時にのみ実行されます。したがって、テキスト フィールドの値を取得すると、もちろん空の文字列が返されます。これは、作成時にはテキスト フィールドがまだ表示されていないためです。

編集:あなたが望むことをするには、次のものを用意してください:

public void actionPerformed(ActionEvent event) {
    String inputString = input.getText() ;
    printOutcome(inputString);
    input.setText("");
}

private void printOutcome(String input) {
    char switchvariable = ' ';
    if (input.indexOf("hide") != -1) {
        switchvariable = 'b' ;
    }
    if ((input.indexOf("exit") != -1 ) || (input.indexOf("leave") != -1) || (input.indexOf("out") != -1)) {
        switchvariable = 'a';
    }
    if ((input.indexOf("exit") == -1) && (input.indexOf("hide") == -1) && (input.indexOf("leave") == -1) && (input.indexOf("out") == -1)) {
        switchvariable= 'c';
    }
    String outcome = "" ;
    switch (switchvariable) {
        case 'a' : 
            outcome = "You exit the tavern to be discovered by\na group of bandits!\n"; 
            break;
        case 'b' : 
            outcome = "You hide behind the bar for 10 minutes until\nyou can no longer hear the group." ; 
            break ;
        case 'c' : 
            outcome = "You must choose between the two given options, exit the tavern, or hide."; 
            break;
    }
    System.out.println(outcome);
}

ただし、この方法は複雑すぎます。if 句を使用して char 変数を設定してから、この char 変数を切り替えることには意味がありません。また、if 句もクリーンアップする必要があります。

于 2012-12-18T20:00:50.540 に答える