Java Swing的文本编辑器 [英] Java Swing Text Editor

查看:384
本文介绍了Java Swing的文本编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/3156809/what-components-should-i-use-for-building-a-java-wysiwyg-html-editor\">What组件我应该使用构建Java所见即所得的HTML编辑器

我在Java编程总新手。我必须做在Swing / AWT文本编辑器,我对此有一个问题。我怎样才能编辑一个选定的字,例如改变其颜色?哪个组件和功能,我应该使用?

I'm a total newbie in Java programming. I have to do text editor in Swing/AWT and I have one question about it. How can I edit one selected word, for example change its color? Which component and which functions should I use?

推荐答案

有关初学者的Java Swing,尽量保持这个项目简单。为了显示多种颜色和多种尺寸在同一个文件需要大量复杂的编码和渲染的HTML。

For a beginner to Java Swing, try to keep this project simple. To show multiple colors and multiple sizes in the same document requires a lot of complex coding and rendering html.

尝试只提供基本的复制,剪切,粘贴功能,因为它们更容易实现。

Try to just provide the basic copy,cut,paste features because they are easier to implement.

要提供这些功能,JTextArea是足够了。

To provide those features, a JTextArea is sufficient.

试试这个。这是一个pretty简单的文本编辑器

Try this. It's a pretty simple text editor

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Document extends JFrame implements ActionListener
{
private JTextArea ta;
private int count;
private JMenuBar menuBar;
private JMenu fileM,editM,viewM;
private JScrollPane scpane;
private JMenuItem exitI,cutI,copyI,pasteI,selectI,saveI,loadI,statusI;
private String pad;
private JToolBar toolBar;
public Document()
{
    super("Document");
    setSize(600, 600);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());

    count = 0;
    pad = " ";
    ta = new JTextArea(); //textarea
    menuBar = new JMenuBar(); //menubar
    fileM = new JMenu("File"); //file menu
    editM = new JMenu("Edit"); //edit menu
    viewM = new JMenu("View"); //edit menu
    scpane = new JScrollPane(ta); //scrollpane  and add textarea to scrollpane
    exitI = new JMenuItem("Exit");
    cutI = new JMenuItem("Cut");
    copyI = new JMenuItem("Copy");
    pasteI = new JMenuItem("Paste");
    selectI = new JMenuItem("Select All"); //menuitems
    saveI = new JMenuItem("Save"); //menuitems
    loadI = new JMenuItem("Load"); //menuitems
    statusI = new JMenuItem("Status"); //menuitems
    toolBar = new JToolBar();

    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);

    setJMenuBar(menuBar);
    menuBar.add(fileM);
    menuBar.add(editM);
    menuBar.add(viewM);

    fileM.add(saveI);
    fileM.add(loadI);
    fileM.add(exitI);

    editM.add(cutI);
    editM.add(copyI);
    editM.add(pasteI);        
    editM.add(selectI);

    viewM.add(statusI);

    saveI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
    loadI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
    cutI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
    copyI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
    pasteI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
    selectI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));

    pane.add(scpane,BorderLayout.CENTER);
    pane.add(toolBar,BorderLayout.SOUTH);

    saveI.addActionListener(this);
    loadI.addActionListener(this);
    exitI.addActionListener(this);
    cutI.addActionListener(this);
    copyI.addActionListener(this);
    pasteI.addActionListener(this);
    selectI.addActionListener(this);
    statusI.addActionListener(this);

    setVisible(true);
}
public void actionPerformed(ActionEvent e) 
{
    JMenuItem choice = (JMenuItem) e.getSource();
    if (choice == saveI)
    {
        //not yet implmented
    }
    else if (choice == exitI)
        System.exit(0);
    else if (choice == cutI)
    {
        pad = ta.getSelectedText();
        ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
    }
    else if (choice == copyI)
        pad = ta.getSelectedText();
    else if (choice == pasteI)
        ta.insert(pad, ta.getCaretPosition());
    else if (choice == selectI)
        ta.selectAll();
    else if (e.getSource() == statusI)
    {
        //not yet implmented
    }
}
public static void main(String[] args) 
{
    new Document();
}

这篇关于Java Swing的文本编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆