java - 如何创建人工光标线?
问题描述
我需要我的假光标始终在文本字段中可见,即使它在不调整任何大小的情况下移动。但我不知道该怎么做。每当您打字时,“¦”本质上应该充当闪烁的光标线。但我不知道是否可以使用不可编辑的文本字段添加它。
有没有办法移动“¦”的视图以充当人工光标?
还是有更好的方法来处理我的问题?
这是迄今为止我所拥有的非常淡化的版本。
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);
}
解决方案
我找到了答案。实际上没有人告诉我“插入符号”位置可以用代码移动。因此,经过一周的环顾后,我发现我可以equationText.positionCaret(i+1);
在更改i
为更新索引后做。每个动作都使用相同的代码。不幸的是,这个剂量的作用就像一条真正的光标线,而且只粘在最右边
推荐阅读
- java - 将 MD5 - base64 从 JAVA 转换为 PHP
- c++ - 如何在 C++ 中的模板中重载 + 运算符
- c - 在 Swift 中调用的 C 结构在调用本机代码时未更新
- php - 在 php 变量中设置 ajax 响应
- java - 如何拆分字符和整数的字符串,以便为字符串中存在的相同字符添加相应的数字?
- java - 从java中的ArrayList中删除在其他类中使用的对象
- matlab - 如何定义向量函数
- c++ - std::vector 是否有用于引用的复制构造函数?
- scala - 什么样的模式将有助于将这种类型的 json 解析为 Scala 中的 Spark SQL?
- php - 如何在php中更改此a href?