每当我按Tab键时,我希望我的JTextPane插入空格.目前它插入制表符(ASCII 9).
反正是有自定义JTextPane的选项卡策略(除了捕获"tab-key"事件和插入空格本身似乎)?
我有一个包含JTextPane的JFrame.这个JTextPane的目的是在我输入单词时突出显示单词,这与程序员的文本编辑器一致.为了实现这一点,我扩展了JTextPane,我实现了KeyListener接口,并将其设置为自我的关键监听器.执行一些重要工作的方法是keyReleased.问题是,我可以突出显示我输入的第一个单词,但在此之后,我继续得到BadLocation,即使开始和结束都在文档限制内.我发布了一些代码片段:
// this is my highlight method
private void highlight(int start,int end) throws BadLocationException {
Document doc = getDocument();
Color c = Color.red;
String text = doc.getText(start,end);
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
setCharacterAttributes(aset, true);
setSelectionStart(start);
setSelectionEnd(end);
replaceSelection(text);
}
//this is my keyReleased method
public void keyReleased(KeyEvent arg0) {
char character = arg0.getKeyChar();
if(wordStarted) { // have I started typing a new word ?
if(character == …Run Code Online (Sandbox Code Playgroud) 我有一个基本的文本编辑器,可以设置文档样式(粗体,下划线,斜体,下划线,对齐左/右/中心,颜色,字体大小/系列),这一切都很好.我遇到的问题是我希望样式将我的按钮和盒子设置为插入符号所在的正确样式.
比方说,我有字符串
快速的棕色狐狸 跳过懒狗
当我快速点击你和我之间时,我想要切换我的斜体按钮,以指示插入符号的文本具有斜体样式.
我正在使用JTextPane来保存文本,并使用StyledEditorKit进行样式设置.
谢谢.
我正在使用Java程序在WYSIWYG Html编辑器上做.我需要当我在Paragraph元素上单击鼠标时,它应该显示
在JOption对话框中标记.请指教我如何做到这一点?
我将如何继续在 JTextPane 中创建水平线元素?只是一个可能由 View.paint(Graphics) 绘制的矩形,它覆盖了 JTextPane 的整个宽度,并且具有任意高度。我还需要能够动态删除这些元素。
我试图通过创建一个自定义编辑器套件来解决这个问题,该套件具有一个自定义视图工厂,它返回行元素的“HorizontalLineView”...但我必须承认这一切都有点超出了我的想象!例如,我如何为此 HorizontalLineView 创建元素?到目前为止我只使用过 insertString()...而且我在任何地方都没有看到“addElement”方法...任何指向正确方向的指针都会很棒。
我会偷偷提出另一个问题:任何人都可以推荐一本深入介绍 JEditorPane/JTextPane 的好书吗?
我想在 Java Swing 中启用 HTML 链接的键盘导航JEditorPane,这可能吗?
请考虑以下代码:
import java.awt.*;import javax.swing.*;import javax.swing.text.*;
public class Main extends JFrame {
private final StyleContext cont = StyleContext.getDefaultStyleContext();
private final AttributeSet normal = cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.BLACK);
private final AttributeSet bold = cont.addAttribute(cont.getEmptySet(),StyleConstants.Bold,Color.BLACK);
public Main() {
setSize(800,600);
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout()); // this is used to make the textpane take up the entire space
getContentPane().add(pane); // adds pane to the frame
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
try {
doc.insertString(0,"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",normal/*change to …Run Code Online (Sandbox Code Playgroud) 我正在制作一个相当简单的文本编辑器,我对我的样式按钮有疑问.当我突出显示文本并单击"粗体"按钮时,文本按预期加粗,但我的选择不再可见.我仍然可以解开选择,使其变为斜体或加下划线,但是你无法看到所选内容.所以,我想知道是否有一个设置允许我点击按钮,但保留我的选择?我尝试了一个JMenuItem而不是JButton,这似乎有用,但随后它让我的工具栏看起来很糟糕.示例代码如下.
//frame and pane creation up here
JToolBar tool = new JToolBar();
JToggleButton boldButton = new JToggleButton("Bold");
boldButton.addActionListener(new StyledEditorKit.BoldAction());
tool.add(boldButton);
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
我Utilities.getRowStart用来找出a中的行数JTextPane.但它给出了BadLocationException我按下回车键的时间:
javax.swing.text.BadLocationException:未由视图表示的位置
任何的想法?
int offset = pane.getText().length();
while(offset > 0) {
try {
offset = Utilities.getRowStart(pane, offset) - 1;
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
lineCount++;
}
Run Code Online (Sandbox Code Playgroud) 好的,所以这里是我的代码片段,其中包含问题:
private JTextField userText;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
private JTextPane images;
private JScrollPane jsp = new JScrollPane(images);
public Server(){
super(name+" - IM Server");
images = new JTextPane();
images.setContentType( "text/html" );
HTMLDocument doc = (HTMLDocument)images.getDocument();
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
add(jsp);
add(images, BorderLayout.CENTER);
images.setEditable(false);
try {
doc.insertString(0, "This is where images and text will show up.\nTo send an image, …Run Code Online (Sandbox Code Playgroud)