1

JPanel に収まらない長い文字列があります。テキストは JPanel 幅よりロガーです。文字列に「\n」を付けて文字列を分割することはできません。実際、文字列の長さと内容を制御することはできません。ユーザーが入力した文字列です。私がやりたいのは、JPanel にテキストを配置するときに、JPanel に収まらないテキストを次の行に流し込むことです。

説明するのは難しいです。詳細が必要な場合はお知らせください。

ありがとうございました

4

3 に答える 3

2

タグ内にテキストを<html></html>入れるとうまくいきます。長い行は自動的に単語で折り返されます。

JLabel label = new JLabel("<html>"+ reallyLongString + "</html>");  
label.setPreferredSize(new Dimension(1, 1); 
于 2012-06-09T20:04:12.110 に答える
2

簡単なGoogle検索から、論理的には、すべてのOSにはさまざまな改行文字があり、Javaはプラットフォームに依存しないため、最初に次を使用して相対区切り記号を見つける必要があることを読みました:

lineSeparator = (String) java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("line.separator"));

次に、文字列を連結しますlineSeparator

例えば:

JLabel label = new JLabel("Hello"+lineSeparator+"world");

この方法は、私が試したこともテストしたこともありません。単に私の研究の結果です。

オーバーフローしたテキストの処理に関しては、私の個人的な経験では、フレームからはみ出す前に文字の最大長を見つけてから、lineSeparator

于 2012-06-09T19:44:38.517 に答える
1

代わりに JTextPane を使用してみてください。ワードラップが処理されます。

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

public class Wordwrap extends JFrame {

    public Wordwrap() {
        String s = "I have a long string which doesn't fit to the JPanel i am putting it. text is logger than the JPanel width. I can not put \n to the string to break the string, in fact i don't have the control over the length and content of the string. It user inputted string. What i want to do is when i am putting the text on JPanel i want any text that doesn't fit in to the JPanel to flow in to the next Line.";

        JTextPane textPanel = new JTextPane();
        textPanel.setText(s);
        textPanel.setPreferredSize(new Dimension(500, 100));

        JPanel p = new JPanel();
        p.add(textPanel);
        getContentPane().add(p);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
        pack();
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                new Wordwrap();
            }
        });
    }
}
于 2012-06-09T20:19:49.023 に答える