我目前正在上学,我们的任务是发明一种仅用键盘上的一个按钮进行书写的解决方案。主要任务是写一份报告,但也需要制作一个原型(prototype)。这就是我被困住的地方。 我制作了一个 JavaFX,其中包含 a-å(挪威语字母)中的所有按钮,计划是随机选择一个按钮(例如键盘上的 g 键),每次按下它都会移动到下一个字母。双击该按钮,它应该将字母打印到文本框,然后单击下一步,移动到行中的下一个字母。这样您应该能够编写简单的短信。
我现在真的很困惑如何使“G键”从不同的按钮切换,以及如何使其打印到文本字段。另外,我对编程和 JavaFX 还很陌生,对于任何愚蠢的问题深表歉意。也不确定 JavaFX 是否是执行此操作的最简单方法,但选择它是因为我最熟悉它。
到目前为止我的代码: 样本.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);
}
}
最佳答案
这是一个您可以使用的示例应用程序。我没有实现 BACKSPACE
、TAB
、ENTER
等。我也没有测试该应用程序以查看在遍历所有字符后是否正确处理了重新启动字符循环。这应该是一个很好的起点。在此应用程序中,我在单击时编码了字符更改,并在双击时将文本附加到 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;~!@#$%^&*()_+~!@#$%^&*()_+[]\\{}|;':\",./<>?";
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);
}
}
}
关于java - 一键书写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46154907/