2017-03-04 1 views
-3

저는 Java 프로그래머이며 JavaFx에 익숙합니다. 가상 키보드를 만들고 싶습니다. 나는 버튼, 레이아웃, 무대, 장면 everything 같은 모든 것을 만들 수있다. 나는 또한 같은 자바 applicatton에 텍스트를 쓸 수있는 setText() 메서드를 사용하여 알고 있지만 질문은 어떻게 이해할 수있는 컴퓨터 또는 프로그램 (javafx 또는 java (예 : setOnAction()) 버튼을 클릭 할 때마다 다른 '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(); 
       } 
      } 
     } 
    }); 
} 

쉬운 방법은 그 수 : 당신은 용기에 모든 버튼을 넣어 반드시있다, 하나의 컨테이너에 모든 것을 넣을 수 있기 때문에 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(); 
        } 
       } 
      } 
     }); 
    } 
} 

희망이 있습니다.

관련 문제