JTextfield字体和属性问题
因此,我正在创建一个程序,根据用户的选择来更改 JTextField。所以它很像一个带有字体(来自 JComboBox)、大小和属性(粗体...等)的 Word 文档。显然我的非常小,只能使用单行(JTextField)。我遇到的问题是,在我将一些内容写入具有特定属性的字段后,并且我想添加更多具有不同属性的单词后,它会更改整个文本字段,而不仅仅是我添加的新部分。我知道它的问题是
Writer.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if((e.getKeyChar() >= e.VK_A && e.getKeyChar()<= e.VK_Z) || (e.getKeyChar() >= 'a' && e.getKeyChar()<='z')|| e.getKeyChar() == '\b' ) // Checks to make sure No Numbers
{
Writer.setEditable(true);
}
else
{
Writer.setEditable(false);
}
if(font.equals("Arial"))
{
if(size.equals("8"))
{
setSize = 8;
}
else if(size.equals("10"))
{
setSize = 10;
}
else if(size.equals("12"))
{
setSize = 12;
}
if(color.equals("Black"))
{
setColor = Color.BLACK;
}
else if(color.equals("Blue"))
{
setColor = Color.BLUE;
}
else if(color.equals("Red"))
{
setColor = Color.red;
}
Font font = new Font("Arial", setAttribute, setSize);
Writer.setFont(font); // I Know that this sets the font everytime, so i'm pretty sure this is where my problem is.
Writer.setForeground(setColor);
}
关于如何进行更改的任何想法,以便新输入的字符可以具有与以前的字符不同的字体。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JTextFields 允许使用 HTML。解析和插入新的 html 代码可能需要一些工作,但您也许可以这样做。
JTextFields allow the use of HTML. It might take a bit of work to parse and insert new html code, but you might be able to do it that way.
这里有一个Java 的所见即所得文本编辑器列表。我特别喜欢 中期编辑器,基于 Charles Bell 的
HTMLDocumentEditor
。There's a list of WYSIWYG text editors for Java here. I especially like metaphase editor, based on Charles Bell's
HTMLDocumentEditor
.样式化文本的 JComponents - 如何使用编辑器窗格和文本窗格,示例 此处,此处,此论坛上的一些示例
JComponents for styled text - How to Use Editor Panes and Text Panes, examples here, here, some of examples on this forum