gpt4 book ai didi

java - 如何更新 JTextPane 中已存在的行?

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:39 25 4
gpt4 key购买 nike

我想向 JTextPane 添加几行,例如 Joseph Red、Clarita Red、Bob Red,然后我想更新特定行的名称和颜色,例如,我想将 Joseph Red 更改为 Rudo Blue,或将 Bob Red 更改为 Molly Blue。有办法这样做吗?我想在向 JTextPane 添加一行时记录每一行,并引用该特定行以便稍后更新,但想不出办法。

String color = "Red";
JTextPane textPanel = new JTextPane();

public void addToTextPane(String name) throws BadLocationException //Add each line to JTextPane
{
document = (StyledDocument) textPanel.getDocument();
document.insertString(document.getLength(), name + "" + color, null);
document.insertString(document.getLength(), "\n", null);
}

我正在尝试执行以下操作(更新 JTextPane 中已有的特定行的名称和颜色):

if(...){ 
status = "Blue";
try
{
addTextPane("Jospeh"); //If I do this, it would not update the already exiting line and
//simply just add a new line with the name 'Joseph' and color 'Blue'
}
catch (BadLocationException e)
{
e.printStackTrace();
}
}

最佳答案

for-loopDocument#getTextDocument#remove Document#insertString 结合使用应该可以解决问题...

Text

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JTextPane textPane;
private String fruit[] = {"Bananas", "Apples", "Oranges", "Kiwis"};
private int index;

public TestPane() {

StringBuilder text = new StringBuilder(64);
text.append("Bananas in pajamas are coming down the stairs\n").
append("Bananas in pajamas are coming down in pairs\n").
append("Bananas in pajamas are chasing teddy bears\n").
append("Cause on tuesdays they try to catch their man-o-wears");

textPane = new JTextPane();
textPane.setText(text.toString());
setLayout(new BorderLayout());

add(new JScrollPane(textPane));

JButton btn = new JButton("Update");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
index++;
String find = fruit[(index - 1) % fruit.length];
String replace = fruit[index % fruit.length];
System.out.println("Find: " + find);
System.out.println("Replace: " + replace);

Document doc = textPane.getDocument();

try {
for (int pos = 0; pos < doc.getLength() - find.length(); pos++) {

String text = doc.getText(pos, find.length());
if (find.equals(text)) {
doc.remove(pos, find.length());
doc.insertString(pos, replace, null);
}

}
} catch (BadLocationException exp) {
exp.printStackTrace();
}

}
});

add(btn, BorderLayout.SOUTH);

}

}

}

关于java - 如何更新 JTextPane 中已存在的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963231/

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