我是要实现像命令行的功能,在提示符下输入一条命令,系统会给一个反应也就是这条命令的下一行输出一行字符串,在字符串的下一行显示提示符并且光标定位在提示符的后面。
现在问题来了,我输入一条命令之后,光标不是停在提示符后面,而是它的下一行的开头(是不是回车会自动在最后加一个回车换行符啊),如何解决(我用的jtextpane,因为要对字符的各个属性进行控制,所以用jtextpane)。如题,谢谢各位。

解决方案 »

  1.   

    我记得,好像必须把JTextPane放到JScrollPane中,在JScrollPane中设置为只有横向光标,就行了~~你可以试试。再说JTextPane好像不能实现你说的那个功能吧???(好久不用Swing了)
    用JTextArea + JScrollPane试试。
      

  2.   

    这段代码应该可以实现你的要求吧????final JScrollPane scrollPane = new JScrollPane();
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    getContentPane().add(scrollPane, BorderLayout.CENTER);textArea_1 = new JTextArea();
    textArea_1.setLineWrap(true);
    textArea_1.setAutoscrolls(false);
    scrollPane.setViewportView(textArea_1);
      

  3.   

    我是要实现想命令行一样的东西,用jtextarea功能不行,要用jtextpane去实现上面说的
      

  4.   

    // 关键点:setCaretPosition ,以及 addCaretListener(new CaretListener()
    //addKeyListener(new KeyAdapter()
    import java.awt.BorderLayout;
    import java.awt.event.InputMethodEvent;
    import java.awt.event.InputMethodListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JFrame;
    import javax.swing.JTextPane;
    import javax.swing.event.CaretEvent;
    import javax.swing.event.CaretListener;
    import com.boco.fram.login.LoginDlg;public class TextPaneTest extends JFrame
    {
    /**
     * Launch the application
     * 
     * @param args
     */
    public static void main(String args[])
    {
    try
    {
    LoginDlg dlg = new LoginDlg();
    dlg.setModal(true);

    TextPaneTest frame = new TextPaneTest();
    frame.setVisible(true);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    } /**
     * Create the frame
     */
    public TextPaneTest()
    {
    super();
    setTitle("JTextPane 测试");
    setBounds(100, 100, 500, 375);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JTextPane inputCommandTextPane = new JTextPane();
    inputCommandTextPane.addKeyListener(new KeyAdapter()
    {
    public void keyPressed(KeyEvent e)
    {
    if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
    {
    if (((JTextPane) e.getSource()).getCaretPosition() < 14)
    {
    ((JTextPane) e.getSource()).setCaretPosition(14);
    return;
    }
    }
    }
    });
    inputCommandTextPane.addCaretListener(new CaretListener()
    {
    public void caretUpdate(CaretEvent e)
    {
    if (e.getDot() < 14)
    {
    ((JTextPane) e.getSource()).setCaretPosition(14);
    return;
    }
    }
    });
    inputCommandTextPane.setText("Input Command>");
    inputCommandTextPane.setCaretPosition(14);
    getContentPane().add(inputCommandTextPane, BorderLayout.CENTER);
    //
    }
    }
      

  5.   

    [转帖http://topic.csdn.net/t/20050518/19/4017591.html]JTextPane没有setLineWrap(boolean   t);方法,StyledDocument控制着JTextPane中的显示,自动换行,当你把JTextPane设置成text/html格式,html语法将控制它的显示,这时换行将通过<br>来实现,想要自动换行就需要用到表格,一个设置好宽度的表格可以使其中的文字自动折行,这完全和网页上的做法相同。
    如果确定采用text/html格式,你可以使用JTextPane的父类JEditorPane,建议服务器端采取推技术。
    用JTextPane显示不同色彩的文字,又想自动折行,请参照下面的代码:import   javax.swing.*;
    import   java.awt.*;
    import   java.awt.event.*;
    import   javax.swing.text.*;
    import   java.io.*;public   class   Test   {
        JFrame   frame;
        JTextPane   textPane;
        //File   file;
        //Icon   image;    public   Test(){
            frame   =   new   JFrame("JTextPane");
            textPane   =   new   JTextPane();
            //file   =   new   File("./classes/test/icon.gif");
            //image   =   new   ImageIcon(file.getAbsoluteFile().toString());
        }    public   void   insert(String   str,   AttributeSet   attrSet)   {
            Document   doc   =   textPane.getDocument();
            str   ="\n"   +   str   ;
            try   {
                doc.insertString(doc.getLength(),   str,   attrSet);
            }
            catch   (BadLocationException   e)   {
                System.out.println("BadLocationException:   "   +   e);
            }
        }    public   void   setDocs(String   str,Color   col,boolean   bold,int   fontSize)   {
            SimpleAttributeSet   attrSet   =   new   SimpleAttributeSet();
            StyleConstants.setForeground(attrSet,   col);
            //颜色
            if(bold==true){
                StyleConstants.setBold(attrSet,   true);
            }//字体类型
            StyleConstants.setFontSize(attrSet,   fontSize);
            //字体大小
            insert(str,   attrSet);
        }    public   void   gui()   {
            //textPane.insertIcon(image);   //插入图片
            setDocs("第一行的文字文字文字文字文字文字文字文字文字文字",Color.red,false,20);   //折行测试
            setDocs("第二行的文字",Color.BLACK,true,25);
            setDocs("第三行的文字",Color.BLUE,false,20);
            frame.getContentPane().add(textPane,   BorderLayout.CENTER);
            frame.addWindowListener(new   WindowAdapter()   {
                public   void   windowClosing(WindowEvent   e)   {
                    System.exit(0);
                }});
            frame.setSize(200,300);
            frame.setVisible(true);
        }
        public   static   void   main(String[]   args)   {
            Test   test   =   new   Test();
            test.gui();
        }