JTextfield字体和属性问题

发布于 2024-11-27 21:29:44 字数 1764 浏览 6 评论 0 原文

因此,我正在创建一个程序,根据用户的选择来更改 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);
            }

关于如何进行更改的任何想法,以便新输入的字符可以具有与以前的字符不同的字体。

So I am creating a program that changes the JTextField depending on what the user Chooses. So its pretty much like a Word Document with fonts(from a JComboBox), sizes, and attributes(Bold...etc). Obviously mine is very small and only works with a single line(A JTextField). The Problem i'm getting is that After I've written some things down into the field with specific attributes and I want to add more words down with different attributes, it changes the whole text field rather than just the new part I added. I know the problem with it is

  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);
            }

Any ideas on how I can make the Change so it newly entered characters can have different fonts than the previous characters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无远思近则忧 2024-12-04 21:29:45

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.

摇划花蜜的午后 2024-12-04 21:29:45

这里有一个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.

看春风乍起 2024-12-04 21:29:44

样式化文本的 JComponents - 如何使用编辑器窗格和文本窗格,示例 此处此处,此论坛上的一些示例

JComponents for styled text - How to Use Editor Panes and Text Panes, examples here, here, some of examples on this forum

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文