簡體   English   中英

JTextPane 拒絕對長字符串進行自動換行

[英]JTextPane refuses to word wrap a long string

嗨,我正在創建一個簡單的 Java-Server 聊天,但我根本無法讓搖擺與長字符串一起玩得很好。

顯示文本未換行的圖像 .

我不想要水平滾動條,我希望單詞在需要時中斷並流到另一行。

我用來創建 JTextPane 的代碼是:

super("Message Server");
    userText = new JTextField();
    userText.setEditable(false);
    userText.addActionListener(
        new ActionListener(){
            public void actionPerformed(ActionEvent event){
                sendMessage(event.getActionCommand());
                userText.setText("");
            }
        }
    );
    getContentPane().add(userText, BorderLayout.SOUTH);
    chatWindow = new JTextPane();
    JScrollPane scrollPane = new JScrollPane(chatWindow);
    getContentPane().add(scrollPane);

    setSize(300, 450); //Sets the window size
    setVisible(true);

    chatWindow.setEditable(false);

您可以使用JTextArea和相應的包裝相關方法setLineWrap()setWrapStyleWord()而不是使用JTextPane來實現您想要的。

是的,您可以使用JTextArea例如設置字體。

JTextArea txtArea = new JTextArea();
Font font = new Font("Verdana", Font.BOLD, 12);
txtArea.setFont(font);
txtArea.setForeground(Color.BLUE);
txtArea.setText("Hellow World!");

嘗試使用 JTextArea 並調用 setWrapStyleWord(true); 在它的實例上,這應該可以滿足您的需要。

暫無
暫無

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

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