想问一下用JAVA怎么实现类似于word的文字处理功能。老师叫弄一个这样的软件,就是比如在文本框里输入自己想处理的一段文字,然后点一些按钮就会自动把这段文字弄成相应的
格式。主要是用来处理毕业论文的格式的。主要功能大概有:
  段落方面:对齐方式,缩进,行间距
  文字方面:字体,大小,粗细大家都知道毕业论文的格式复杂,用JAVA弄起来会不会很麻烦呢? 
可以的话大概用哪些类就能实现呢?

解决方案 »

  1.   

    类似一个小型排版系统,需要编译原理相关知识,作一下词法分析和语法分析等。。
    可以用一下flex和bison
      

  2.   

    大家都知道毕业论文的格式复杂,用JAVA弄起来会不会很麻烦呢?   会啊
    换word吧
      

  3.   


    //JTextPane功能比较强大,参见如下程序
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    public class TTextPane
    {
        public static void main(String args[])throws BadLocationException
        {
            JFrame frm=new JFrame();
            JTextPane jtp=new JTextPane();
            frm.getContentPane().add(new JScrollPane(jtp),BorderLayout.CENTER);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final StyleContext sc=new StyleContext();
            final DefaultStyledDocument dsd=new DefaultStyledDocument(sc);
            jtp.setDocument(dsd);jtp.setDragEnabled(true);
            Style defaultStyle=sc.getStyle(sc.DEFAULT_STYLE);        final Style mainStyle=sc.addStyle("mainStyle", defaultStyle);
            StyleConstants.setForeground(mainStyle, Color.blue);
            StyleConstants.setFontSize(mainStyle, 10);
            StyleConstants.setLeftIndent(mainStyle, 20);
            StyleConstants.setFirstLineIndent(mainStyle, 10);        final Style secondStyle=sc.addStyle("secondStyle", null);
            StyleConstants.setForeground(secondStyle, Color.red);        final Style thirdStyle=sc.addStyle("thiredStyle", null);
            StyleConstants.setForeground(thirdStyle, Color.green);
            StyleConstants.setFontSize(thirdStyle, 30);        new Thread(new Runnable(){
                public void run(){
                   try{
                        
                        dsd.setLogicalStyle(0, mainStyle);
                        dsd.insertString(0, text, null);
                        dsd.setParagraphAttributes(0, 1, secondStyle, false);
                        dsd.setParagraphAttributes(1, 1, thirdStyle, false);
                    }catch(BadLocationException e){}
                }
            }).start(); 
            frm.setSize(400,300);
            frm.setVisible(true);System.out.println("####");
        }
        public static final String text =
              "Attributes, Styles and Style Contexts\n" +
              "The simple PlainDocument class that you saw in the previous " +
              "chapter is only capable of holding text. The more complex text " +
              "components use a more sophisticated model that implements the " +
              "StyledDocument interface. StyledDocument is a sub-interface of " +
              "Document that contains methods for manipulating attributes that " +
              "control the way in which the text in the document is displayed.\n " +
              "The Swing text package contains a concrete implementation of " +
              "StyledDocument called DefaultStyledDocument that is used as the " +
              "default model for JTextPane and is also the base class from which " +
              "more specific models, such as the HTMLDocument class that handles " +
              "input in HTML format, can be created. In order to make use of " +
              "DefaultStyledDocument and JTextPane, you need to understand how " +
              "Swing represents and uses attributes.\n";
    }
      

  4.   


    //呵呵,调整一下格式,让你看的清楚一些
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    public class TTextPane
    {
        public static void main(String args[])throws BadLocationException
        {
            JFrame frm=new JFrame();
            JTextPane jtp=new JTextPane();
            frm.getContentPane().add(new JScrollPane(jtp),BorderLayout.CENTER);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final StyleContext sc=new StyleContext();
            final DefaultStyledDocument dsd=new DefaultStyledDocument(sc);
            jtp.setDocument(dsd);jtp.setDragEnabled(true);
            Style defaultStyle=sc.getStyle(sc.DEFAULT_STYLE);        final Style mainStyle=sc.addStyle("mainStyle", defaultStyle);
            StyleConstants.setForeground(mainStyle, Color.blue);
            StyleConstants.setFontSize(mainStyle, 10);
            StyleConstants.setLeftIndent(mainStyle, 20);
            StyleConstants.setFirstLineIndent(mainStyle, 10);        final Style secondStyle=sc.addStyle("secondStyle", null);
            StyleConstants.setForeground(secondStyle, Color.red);        final Style thirdStyle=sc.addStyle("thiredStyle", null);
            StyleConstants.setForeground(thirdStyle, Color.green);
            StyleConstants.setFontSize(thirdStyle, 30);        new Thread(new Runnable(){
                public void run(){
                   try{
                        
                        dsd.setLogicalStyle(0, mainStyle);
                        dsd.insertString(0, text, null);
                        dsd.setParagraphAttributes(0, 1, secondStyle, false);
                        dsd.setParagraphAttributes(1, 1, thirdStyle, false);
                    }catch(BadLocationException e){}
                }
            }).start(); 
            frm.setSize(400,300);
            frm.setVisible(true);System.out.println("####");
        }
        public static final String text =
              "Attributes, Styles and Style Contexts\n" +
              "The simple PlainDocument class that you saw in the previous " +
              "chapter is only capable of holding text. The more complex text " +
              "components use a more sophisticated model that implements the " +
              "StyledDocument interface. StyledDocument is a sub-interface of " +
              "Document that contains methods for manipulating attributes that " +
              "control the way in which the text in the document is displayed.\n " +
              "The Swing text package contains a concrete implementation of " +
              "StyledDocument called DefaultStyledDocument that is used as the " +
              "default model for JTextPane and is also the base class from which " +
              "more specific models, such as the HTMLDocument class that handles " +
              "input in HTML format, can be created. In order to make use of " +
              "DefaultStyledDocument and JTextPane, you need to understand how " +
              "Swing represents and uses attributes.\n";
    }
      

  5.   

    不过我感觉比较奇怪,如果你只是想处理论文的格式,为什么不用word呢?
    其一,word功能强大.
    其二,用Java实现的话, 这个打印的话,貌似有点问题.
    其三,如果是提交电子档的话,那么你存储文件的格式就有问题了.你如何存储?就算你设计了一套存储及读取的办法,如果与word的格式不一样,别人如何看呢?
    再说一句,虽然JTextPane功能很多了,可以设定字体,各种颜色,背景色,缩进等等,但是它的功能肯定无法与word相比,我们设计可视化程序,如果使用系统或者是相关类库提供的现呈组件,目的是方便,但是其功能肯定有一定的局限性.如果你真的想设计很个性化的可视化程序,可能就需要自己设计UI组件,比如说使用最简单的GDI绘图函数,进行显示.