簡體   English   中英

如何在Java中使JTextPane具有不同的選項卡大小?

[英]How do I make a JTextPane have a different tab size in Java?

我有一個JTextPane,但我注意到,如果使用.setText()所有選項卡都將被刪除。
要解決此問題,我看到兩個選項:

  1. 正確設置選項卡。
  2. 將所有標簽替換為html特殊字符  然后更改JTextPane中的標簽大小以匹配html標簽的寬度。

我不知道該怎么做#1,所以我正在嘗試使用原始的技巧#2。 所以我的問題是,如何使用HTMLDocument更改JTextPane的選項卡大小,或者如何設置setText()而不刪除選項卡?

我也使用getText()setText()來將文本保存在JTextPane中。

先感謝您。

為了解決沒有方法1或2的問題,我這樣做了:

textPane.getInputMap().put(KeyStroke.getKeyStroke("TAB"), "tab");
textPane.getActionMap().put("tab", new AbstractAction("tab"){
    private static final long serialVersionUID = -2621537943352838927L;

    public void actionPerformed(ActionEvent e){
        try {
            textPane.getDocument().insertString(textPane.getCaretPosition(), " ", null);//The " " is the html special character (tab) in plain text.
        } catch (BadLocationException e1) {
            e1.printStackTrace();
        }
    }
});

我發現這將覆蓋默認的選項卡輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM