簡體   English   中英

JTextPane換行問題

[英]JTextPane word wrapping issue

我在用於Wiki源代碼編輯的應用程序中使用JTextPane 我已經向它添加了一個簡單的拼寫檢查功能,該功能通過通過StyledDocument.setCharacterAttributes將字符屬性更改為其他樣式來強調拼寫錯誤的單詞。

僅使用了這兩種樣式:默認樣式和“拼寫錯誤”樣式。 文本編輯器控件進行自動換行,這是預期的行為。

我的問題是,在某些情況下(並非總是如此,但是可以通過特定的Wiki文檔重現),這種字符屬性更改會以某種方式禁用自動換行。 更具體地說,我從文檔中間刪除了三行,並在下次運行拼寫檢查器時,將字符屬性重置為默認樣式時(在重新運行拼寫檢查之前),自動換行功能被禁用,並且保留了方式。 如果撤消刪除操作,自動換行將恢復正常。

注釋掉重置樣式的單行:

editorPane.getStyledDocument().setCharacterAttributes(0, editorPane.getStyledDocument().getLength(), defaultStyle, true);

解決了這個問題。

編輯1

我已將問題提取到一個簡單的測試用例中。 很抱歉,很長的文字說明,該示例文本對於重現該錯誤很重要(已被隨機化):

package jtextpanebug;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.Style;


public class DemoFrame extends javax.swing.JFrame {

    private final JButton btResetStyle;
    private final JScrollPane scrollPane;
    private final JTextPane textPane;    
    private final Style defaultStyle;

    public DemoFrame() {
        // Creating a simple form with a scrollable text pane and a button
        scrollPane = new JScrollPane();
        textPane = new JTextPane();
        btResetStyle = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        // The text pane's text is the scrambled version of my original test data,
        // it is important because the problem depends on the pane's text
        // (not every text makes it wrong)
        textPane.setText("= Gernela Stuff Dogo to Wnko Obuat Oebrfe Disytung hte Iidnividal Oitpcs =\n\n== Elmyonrogit ==\n\n'''memidtaie-ngiebhro opnits''' - 2 points, \nwihhc nac eb ense ot eb mmiieadte hnigeorbs fo haec othre. \nThere si no strict | cxeat defiintoin. \nEth etipcur owebl shsow sa an example optins dna the rispa \nienbg mimedtiea iebnghsor ear ncnoetced.\n\n[[Amieg:einogrhb_pinsot.pgn]]\n\n'''enihgorb optnsi distacne''' - het avaeegr of sdntaisce \nderemitedn by het mimeidate-hieobngr tonpi ipras. \n\n'''lalw''' - a iotpntes nepgesnietrr a llwa, with toerh orwds: 2 apraelll, \nevyr sloce sraufce picsee. Heer is an xamelpe. \nIt is eualgttandri ofr eterbt zisiuaitovlan.\n\n[[Gimae:llwa.npg]]\n\n'''addtaiilon emmory reeueimtnqr of na laigorthm''' - \n(eth kepa mmeory suaeg fo teh nltpiaciapo ndirug the excteouin of eht grlaotihm) - \n(hte moeymr sueag fo hte loragitmh befoer ro ftrea eht ucxeeiont of the laogrihmt)\n\n== Het Input Pnoitset Ash to Repnrsete Ufscear Arsnoelbay Elwl ==\n\nIf tno efisciped toehrwsie yb hte cdoritnpsei of an aoglirthm, \nhetn hte eqtunrmeersi of it are heste:\n\n* Ifsrt fo all the poisentt umst reprseent urfseac, not urvec ro uvomel or nayithng eesl.\n* Awlls aym otn eb tniehnr tanh at least 3 * fo ienhbgro-tpoin-sidenact.\n* Dseeg amy ton be rhserap tnha 70 grdesee (as het agnle fo eht trmeaial) nda husdol be ta tleas 290 redeseg (ni caes fo cnvocae eedgs).\n* Onpti edintsy amy ont vayr oto humc \n** Het angre fo the coall ption desitnsei of a igsenl pisnotte nutip ushold eb sallm. Ahtt is: teh orait of het oclla oitnp idsentise oarund any 2 ipnost lsdhou eb lmitied.\n** Hte lcoal noipt deisynt ushlod otn ahencg sdduelyn (gliftyscaiinn ni a hotsr idnsteac). \n\nYreftntunaoul the largoimths cna tno yb ethmsevesl \nhcekc these qutenmeserir nda usjt yden rnuning, \nso it si eth rseu's iyponerissbtil to ton extucee an raltghomi no a itseopnt \nthat does ont mete het aogitmlhr's terieseurmnq.\n\nIf eth rmeteriuqen fo na airlgmoth on its npuit is ont mte, then tobh hte ueavbhior nad hte srluet fo hte alghoritms si dinfeuned. \nTeh loirgamth amy nru rfo iinfntie long imet or rodpuce evry abd rselut, ro a eruslt htat oolsk good btu is nicrtroec. Ni htis scea rtehe si tno nay aguntreee toabu the tmniatreion of the iralgtmho ro eht lqutaiy fo the sreltu ecxept htat the nptapalcioi iwll ont carsh.\n");
        scrollPane.setViewportView(textPane);

        getContentPane().add(scrollPane);

        btResetStyle.setText("Reset style");
        btResetStyle.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                btResetStyleActionPerformed(evt);
            }
        });
        getContentPane().add(btResetStyle);

        pack();

        // The default style, the problem happens when we reset the full document
        // to it:
        defaultStyle = textPane.addStyle("default", null);        
    }

    private void btResetStyleActionPerformed(java.awt.event.ActionEvent evt) {

        // When the button is pressed we reset the full document to the default
        // style. In the original application this was periodically done as 
        // part of the spell checking
        textPane.getStyledDocument().setCharacterAttributes(0, textPane.getStyledDocument().getLength(), defaultStyle, true);
    }

    public static void main(String args[]) {                
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DemoFrame().setVisible(true);
            }
        });
    }    
}

重現問題:

  1. 編譯並運行上面的類
  2. 嘗試調整框架大小-自動換行
  3. 找到並刪除我在下面復制的三行
  4. Reset style按鈕
  5. 自動換行功能已關閉
* Onpti edintsy amy ont vayr oto humc 
** Het angre fo the coall ption desitnsei of a igsenl pisnotte nutip ushold eb sallm. Ahtt is: teh orait of het oclla oitnp idsentise oarund any 2 ipnost lsdhou eb lmitied.
** Hte lcoal noipt deisynt ushlod otn ahencg sdduelyn (gliftyscaiinn ni a hotsr idnsteac).

編輯2

使用熒光筆代替樣式解決了我的問題,但是我仍然很好奇原始方法的問題。

這看起來像是我在這里問過的相同問題: 在Java 7的JTextPane中,奇怪的文本換行和樣式化文本換行

據我所知,這是Java 7中的錯誤,並且未在Oracle的Java Bug Parade中進行記錄 我仍然沒有找到解決方法(在我的情況下,不能使用熒光筆)。

暫無
暫無

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

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