简体   繁体   English

Java Eclipse文字编辑器

[英]Java Eclipse text editor

Good day. 美好的一天。 So I'm working on this project and I'm having one question. 所以我正在从事这个项目,并且有一个问题。 I have an encyclopedia and I want to add a text editor. 我有一个百科全书,我想添加一个文本编辑器。 I have a text and a scrollpanel and I want, when I select a sentence of my text and I press one button, to change the font, make the text bold, italic, underlined etc. How can I do that? 我有一个文本和一个滚动面板,当我选择一个文本句子并按一个按钮时,我想要更改字体,使文本变为粗体,斜体,带下划线等。我该怎么做?

My code looks like this, the text.txt is a text file with "aaaa" in it. 我的代码如下所示,text.txt是其中包含“ aaaa”的文本文件。

package test;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Rectangle;
import javax.swing.JFrame;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class test extends JFrame {
    private static final long serialVersionUID = 1L;
    JFrame test = new JFrame("test");

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    test frame = new test();
                    frame.setVisible(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public test() {
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setBounds(new Rectangle(0, 0, 0, 0));
        getContentPane().setLayout(null);

        test.setName("frame");
        test.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        test.setBounds(300,0,800,800);
        test.setResizable(false);
        test.getContentPane().setLayout(null);

        JScrollPane text = new JScrollPane();
        text.setBackground(Color.DARK_GRAY);
        text.setBounds(0, 0, 500, 400);
        getContentPane().add(text);

        JTextArea textarea = new JTextArea();
        setBackground(Color.WHITE);
        textarea.setEditable(false);
        textarea.setWrapStyleWord(true);
        textarea.setLineWrap(true);
        try{
            FileInputStream fstream = new FileInputStream("D:\\Facultate\\anul 2\\Java Workspace\\test\\src\\text.txt");
            DataInputStream in = new DataInputStream(fstream);
            Reader reader = new InputStreamReader(in);
            textarea.read(reader, fstream);
        }catch(Exception e){System.err.println("Error: " + e.getMessage());}
        text.setViewportView(textarea);
    }
}

From the documentation: "A JTextArea is a multi-line area that displays plain text." 从文档中:“ JTextArea是显示纯文本的多行区域。” So if you want different fonts, etc. in one area, you'll have to use another control, probably RTFEditorKit 因此,如果要在一个区域中使用不同的字体等,则必须使用另一个控件,可能是RTFEditorKit

There is an amazing and Free Text Editor for Java. 有一个很棒的Java自由文本编辑器。 You can find it at Download a ready-to-use CKEditor package that best suits your needs. 您可以在“下载最适合您的需要的CKEditor软件包”中找到它。 It's a product distributed by Amazon Web Services. 它是由Amazon Web Services分发的产品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM