2017-09-11 4 views
1

私は現在、学校にいますが、キーボードのボタンを1つだけ書いて解決策を考案しています。主なタスクは、レポートを作成することですが、プロトタイプを作成する必要もあります。それが私が立ち往生している場所です。 私はa-å(ノルウェー語の文字)からすべてのボタンを使ってJavaFXを作っており、ランダムなボタン(キーボードのgキーと言う)を選んで、プッシュするたびに次の文字に移動します。ボタンをダブルクリックすると、文字をテキストボックスに印刷し、次のクリックは次の行に移動します。そうすれば、簡単なテキストメッセージを書くことができます。1つのボタンで書く

「Gキー」スイッチをさまざまなボタンから作成する方法と、テキストフィールドに印刷する方法については、今は本当に固まっています。また、私はプログラミングとJavaFXにはかなり新しいです、そして、どんなばかげた質問でもお詫び申し上げます。また、JavaFXがこれを行う最も簡単な方法であるかどうかはわかりませんが、私はそれをよく知っているので、それを選択してください。これまで

マイコード: sample.fxml:

<GridPane fx:controller="sample.Controller" 
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10"> 
      <TextField GridPane.columnIndex="1" GridPane.rowIndex="1"/> 
    <HBox spacing="10" alignment="bottom_right" 
      GridPane.columnIndex="1" GridPane.rowIndex="2"> 
     <Button text="A" fx:id="pushed" onAction="#write"/> 
     <Button text="B" /> 
     <Button text="C"/> 
     <Button text="D"/> 
     <Button text="E"/> 
    </HBox> 
    <HBox spacing="10" alignment="bottom_right" 
      GridPane.columnIndex="1" GridPane.rowIndex="3"> 
     <Button text="F"/> 
     <Button text="G"/> 
     <Button text="H"/> 
     <Button text="I"/> 
     <Button text="J"/> 
     <Button text="K"/> 
    </HBox> 
    <HBox spacing="10" alignment="bottom_right" 
      GridPane.columnIndex="1" GridPane.rowIndex="4"></HBox> 
    <HBox spacing="10" alignment="bottom_right" 
      GridPane.columnIndex="1" GridPane.rowIndex="5"> 
     <Button text="R"/> 
     <Button text="S"/> 
     <Button text="T"/> 
     <Button text="U"/> 
     <Button text="V"/> 
     <Button text="X"/> 
    </HBox> 
    <HBox spacing="10" alignment="bottom_right" 
      GridPane.columnIndex="1" GridPane.rowIndex="6"> 
     <Button text="Y"/> 
     <Button text="Z"/> 
     <Button text="Æ"/> 
     <Button text="Ø"/> 
     <Button text="Å"/> 
    </HBox> 
    <HBox spacing="10" alignment="bottom_right" 
      GridPane.columnIndex="1" GridPane.rowIndex="7"> 
     <Button text="Space"/> 
     <Button text="."/> 
    </HBox> 
</GridPane> 

Controller.java:

package sample; 

import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 

public class Controller { 

    public void write(ActionEvent event){ 

    } 
} 

Main.java:ここ

package sample; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 
     primaryStage.setTitle("Hello World"); 
     primaryStage.setScene(new Scene(root, 300, 275)); 
     primaryStage.show(); 
    } 


    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

通常のタスクではなくToggleButtonsを使用すると、タスクをすぐに簡略化できます。 –

+0

キーボード入力は "anykey"というidと "setOnMouseClicked"という1つの "any key"ボタンを作るので、MouseEventとisgetの 'getClickCount'を得ることができます。これで、ダブルクリックを検出できます。ボタンは出力として使用できます。ダブルクリックするとどのキーが使用されますか。トグルボタン(またはラジオボタン)を使用することもできます。 –

答えて

0

をすることができますサンプルアプリですと遊ぶ。私はBACKSPACETABENTERなどを実装していませんでした。私は、すべての文字を通過した後に文字サイクルを再開したことを正しく処理したかどうかをアプリでテストしませんでした。これは良い出発点であるはずです。このアプリでは、ワンクリックで文字の変更をコード化し、ダブルクリックでTextFieldにテキストを追加します。ダブルクリックを検出する方法は、@ JamesDの回答hereから来ています。

import java.util.ArrayList; 
import java.util.List; 
import javafx.animation.PauseTransition; 
import javafx.application.Application; 
import javafx.beans.property.IntegerProperty; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextArea; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

/** 
* 
* @author blj0011 
*/ 
public class OneButtonWriter extends Application { 

    final static String ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789;[email protected]#$%^&*()[email protected]#$%^&*()_+[]\\{}|;':\",./<>?"; 
    final static String[] ACTION_KEYS = {"TAB", "ENTER"}; 

    List<String> keys = new ArrayList();//Holds each key 

    int currentKey = 0; 

    @Override 
    public void start(Stage primaryStage) { 
     loadKeys();//Add all keys from ALPHABETS and ACTION_KEYS to keys arralist as individual keys. 

     TextArea textArea = new TextArea(); 

     Button button = new Button(); 

     //Code from Jame_D answer on double click 
     Duration maxTimeBetweenSequentialClicks = Duration.millis(500); 

     PauseTransition clickTimer = new PauseTransition(maxTimeBetweenSequentialClicks); 
     final IntegerProperty sequentialClickCount = new SimpleIntegerProperty(0); 
     clickTimer.setOnFinished(event -> { 
      int count = sequentialClickCount.get(); 
      if (count == 1) 
      { 
       System.out.println("Single click"); 
       textArea.appendText(button.getText());//if single click append text to textarea 
      } 
      if (count == 2) 
      { 
       System.out.println("Double click"); 
       currentKey++;//if double click increment currentKey 
       if(currentKey == keys.size())//If currentkey equal the keys size set current key back to A or index zero ***I HAVE NOT TESTED THIS*** 
       { 
        currentKey = 0; 
       } 

       button.setText(keys.get(currentKey)); 
      } 
      if (count == 3) System.out.println("Triple click"); 
      if (count > 3) System.out.println("Multiple click: "+count); 
      sequentialClickCount.set(0); 
     }); 


     button.setPrefSize(100, 100); 
     button.setText(keys.get(currentKey)); 
     button.setOnMouseClicked(event -> {    
      sequentialClickCount.set(sequentialClickCount.get()+1); 
      clickTimer.playFromStart(); 

     }); 

     VBox vbox = new VBox(); 
     vbox.getChildren().add(textArea); 
     vbox.getChildren().add(new StackPane(button)); 

     StackPane root = new StackPane(); 
     root.getChildren().add(vbox); 

     Scene scene = new Scene(root, 300, 250); 

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

    private void loadKeys() 
    { 
     for(int i = 0; i < ALPHABETS.length(); i++) 
     { 
      keys.add(Character.toString(ALPHABETS.charAt(i))); 
     } 

     for(String actionKey : ACTION_KEYS) 
     { 
      keys.add(actionKey); 
     } 
    } 

} 
+0

ありがとう、これは多くの助けになります! –

関連する問題