gpt4 book ai didi

java - 处理Java : Cursor position of text box off

转载 作者:行者123 更新时间:2023-12-01 11:17:46 25 4
gpt4 key购买 nike

除了选择和文本光标的一些错误之外,我即将完成我的文本编辑器文本框。我必须从头开始实现它,因为其他库不适合我的编辑器的设计需求。每次用户完成一行并开始在下一行退格时,当用户开始键入时,光标和文本的位置不正确(光标不在右行)强>)。当用户一遍又一遍地这样做时,差距会变得更加明显。

enter image description here

您可以清楚地看到光标(浅蓝色)和文本未正确对齐。我附上了仅与此问题相关的代码。抱歉,如果文本框未处于最佳位置,因为我已将代码从文本编辑器转移到针对此问题的降级版本。

我认为罪魁祸首是:几个小时后,我发现光标位置取决于行和列(如标签上所示) - 我没有附加标签以这个问题中的例子为例。 行显示 2,但它应该是 1。当列为 1 并且用户退格时,该行应该减少 1,并且该列设置为前一行的长度。

如果您有任何问题,我很乐意回答。因为代码传输起来很复杂,所以很多代码都无法正常工作(随着用户输入而使光标水平移动),但我认为它足以解决问题。

如何解决问题:

  1. 在第一行输入一些文本
  2. 按回车键
  3. 尝试退格

这是处理 Java 中的文本框代码:

// Content
String content = "";
String[] adjustedLines = {
};

// Current position
int row = 1;
int line = 1;
int column = 1;

// Cursor length
float cursorLength = 12;

// Whether line has been subtracted and readjustment to text has been completed
boolean lineSubtracted = false;

// Positions of scrollbar
float cursorX = width/5 + 55;
float cursorY = 55;

void setup()
{
// Background and size
background(0);
size(1500, 700);
}

// Create set of line numbers given starting number and number of lines
void createLineNumbers(int startingNumber, int numberOfLines)
{
textAlign(LEFT);
String lineText = "";
textLeading(22);

for (int i = startingNumber; i <= startingNumber + numberOfLines; i++)
{
lineText += (str(i) + "\n");
}

fill(200);
text(lineText, width/5 + 12.5, 75);
textAlign(CENTER);
}

void draw()
{
background(0);

// Update cursor position
cursorX = width/5 + 55;
cursorY = 55;

textAlign(CENTER);

// Text Box
fill(80);
rect(width/5, 55, width*4/5, height-55);

textAlign(LEFT);
textLeading(22);
fill(255);

String[] contentLines = content.split("\n");
String display = "";
int lineDifference = 0;

display = content;
text(display, width/5+55, 75);

// Line Numbers
textAlign(CENTER);
fill(240);

createLineNumbers(1 + lineDifference, line + lineDifference);
cursorY = 55 + 22 * line;

textAlign(RIGHT);

// Cursor
stroke(149, 203, 250);
strokeWeight(4);
line(cursorX, cursorY, cursorX - cursorLength, cursorY);
noStroke();

textAlign(CENTER);
}

// Updates content and locations from user typing
void keyPressed()
{
String[] allLines = content.split("(?<=\n)");
boolean willPrint = false;

if (key == BACKSPACE)
{
if (column <= 1)
{
if (line > 1)
{
line--;
lineSubtracted = true;
finished = false;
}

column = 2;

if (lineSubtracted == true && allLines[allLines.length - 1].length() > 1 && allLines.length > 1)
{
column = allLines[allLines.length - 2].length();
}
}

if (content.length() > 0)
{
content = content.substring(0, content.length() - 1);
}

column--;
} else if (key == TAB)
{
column += 4;
content += " ";
} else
{
if (key == ENTER)
{
line++;
column = 0;
} else if (lineSubtracted == true && finished == false && line > 1)
{
if (line == allLines.length)
{
content = content.substring(0, content.length() - 1);
}

finished = true;
}

content += key;

column++;
}

column = allLines[allLines.length - 1].length();
}

最佳答案

就其值(value)而言,您只是为了显示一些可编辑的文本而跳过了很多麻烦。这是一个简化的示例,可以让处理为您完成工作:

String text = "";
String cursor = "_";

boolean blink = true;

void setup() {
size(500, 500);
}

void draw() {

if(frameCount % 30 == 0){
blink = !blink;
}

background(0);

if(blink){
text(text, 100, 100, 200, 200);
}
else{
text(text+cursor, 100, 100, 200, 200);
}
}

void keyPressed()
{
if (key == BACKSPACE)
{
if (text.length() > 0) {
text = text.substring(0, text.length()-1);
}
} else {
text += key;
}
}

关于java - 处理Java : Cursor position of text box off,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com