2017-03-04 5 views
-3

私はJavaプログラマーであり、JavaFxの初心者です。仮想キーボードを作成したいです。私はボタン、レイアウト、ステージ、シーンのすべてのようなものを作ることができます。私は同じJavaアプリケーションでテキストを書くことができるsetText()メソッドを使用することも知っていますが、質問はどのように私はコンピュータやプログラムを理解する必要があります(javafxまたはjava (つまり、setOnAction()で)クリックすると、別のJavaアプリケーション(例えば、メモ帳、ワードパッドなど)に文字を書き込む必要があります。どのようなクラスやインタフェースがあるのですか?それぞれ拡張したり実装したりする必要がありますか?私はインターネットを調査しましたが、役に立つものを見つけることができませんでした。ボタンをクリックすると別のJavaアプリケーションに文字を書くことはできますか?

enter image description here

答えて

0

あなたはこのように行うことができますあなたのコントローラ内のすべてのボタンを設定している場合:

//I supposed you named you 'button_a' your 
@FXML 
Button button_a; 
@Override 
public void initialize(URL location, ResourceBundle resources) { 
    button_a.setOnAction(event->{ 
     BufferedWriter writer = null; 
     try { 
      writer = new BufferedWriter(new FileWriter("file.txt")); //or new File("c:/users/.../.../file.txt"); 
      writer.write(button_a.getText());  //will give the letter you write on the button : the letter of the keyboard 
     } catch (IOException e) { 
      System.err.println("IOError on write"); 
      e.printStackTrace(); 
     } finally { 
      if (writer != null) { 
       try { 
        writer.close(); 
       } catch (IOException e) { 
        System.err.println("IOError on close"); 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 
} 

簡単な方法がある可能性があり:あなたは確かに、コンテナ内のすべてのあなたのボタンを入れています、すべての要素を1つのコンテナに入れ(ボタンを入れたり、ループのたびにボタンであることを確認する必要があるため)、GridPaneの子(ボタン)を反復処理することができるので、GridPaneは良いでしょう。

@FXML 
GridPane grid; 
@Override 
public void initialize(URL location, ResourceBundle resources) { 
    String fileName = "test.txt"; 
    for(Node button : grid.getChildren()){ 
     ((Button)button).setOnAction(event->{ 
      BufferedWriter writer = null; 
      try { 
       writer = new BufferedWriter(new FileWriter(fileName)); //or new File("c:/users/.../.../file.txt"); 
       writer.write(((Button)button).getText());  //will give the letter you write on the button : the letter of the keyboard 
      } catch (IOException e) { 
       System.err.println("IOError on write"); 
       e.printStackTrace(); 
      } finally { 
       if (writer != null) { 
        try { 
         writer.close(); 
        } catch (IOException e) { 
         System.err.println("IOError on close"); 
         e.printStackTrace(); 
        } 
       } 
      } 
     }); 
    } 
} 

希望します。

関連する問題