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