我需要我的假光标在文本字段中始终可见,即使它在不调整任何大小的情况下移动也是如此。但我不知道该怎么做。每当您打字时,“|”基本上应该充当闪烁的光标线。但我不知道如何添加不可编辑的文本字段。
有没有办法移动“|”的视图来充当人工光标?
或者有更好的方法来处理我的问题吗?
这是我目前所拥有的一个非常淡化的版本。
public class fake extends Application
{
TextField equationText=new TextField("¦sadfhoiqehre jnvlsvkslurgbqo938y28374 9whfo37282736kabviouah7y82");
public void start(Stage primaryStage)
{
equationText.setEditable(false);
equationText.setAlignment(Pos.CENTER_RIGHT);
equationText.setFocusTraversable(false);
equationText.setDisable(true);
equationText.setOpacity(1); //make text not look disabled
equationText.setStyle("-fx-border-color:black");
equationText.setPrefSize(360,30);
VBox pane=new VBox();
pane.getChildren().add(equationText);
Scene scene=new Scene(pane);
scene.setOnKeyPressed(event -> keyboardControls(event));
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.setTitle("Calculator FX");
primaryStage.show();
}
void moveIndex(char c)
{
if(c=='z')
{
//left
String pieThing=equationText.getText();
System.out.println("Ding\t\t\t"+pieThing);
System.out.println("Size of string "+pieThing.length());
System.out.println("index of thing "+pieThing.indexOf("¦"));
if(pieThing.indexOf("¦")>0)
{
int i=pieThing.indexOf("¦");
pieThing=pieThing.replace("¦","");
StringBuilder pieThingBuilder=new StringBuilder(pieThing);
pieThingBuilder.insert(i-1,"¦");
pieThing=pieThingBuilder.toString();
System.out.println("Done with right "+pieThing);
equationText.setText(pieThing);
}
else
{
System.out.println("left failed");
}
}
else
{
//right
String pieThing=equationText.getText();
System.out.println("Ding\t\t\t"+pieThing);
System.out.println("Size of string "+pieThing.length());
System.out.println("index of thing "+pieThing.indexOf("¦"));
if(pieThing.indexOf("¦")+1!=pieThing.length())
{
int i=pieThing.indexOf("¦");
pieThing=pieThing.replace("¦","");
StringBuilder pieThingBuilder=new StringBuilder(pieThing);
pieThingBuilder.insert(i+1,"¦");
pieThing=pieThingBuilder.toString();
System.out.println("Done with right "+pieThing);
equationText.setText(pieThing);
}
else
{
System.out.println("right failed");
}
}
}
void keyboardControls(javafx.scene.input.KeyEvent key)
{
if(key.getCode()==KeyCode.Z)
moveIndex('z');
else if(key.getCode()==KeyCode.X)
moveIndex('x');
}
public static void main(String[] args)
{
launch(args);
}
答案 0 :(得分:0)
我找到了答案。实际上没有人告诉我“插入符号”位置可以用代码移动。因此,经过大约一周的环顾四周后,我发现在更改 equationText.positionCaret(i+1);
以更新索引后,我可以只执行 i
。而且每个动作的代码都是一样的