入力したテキストの高さを取得する方法はありJTextPane
ますか? ピクセルは素晴らしいですが、それを表すことができる値であれば何でも構いません。たとえば、これに入力しましたJTextPane
:
Lorem Ipsum は、印刷および植字業界の単なるダミー テキストです。Lorem Ipsum は、1500 年代に未知の印刷業者がタイプのギャレーを取り、それをスクランブルしてタイプ見本帳を作成して以来、業界の標準的なダミー テキストでした。それは 5 世紀だけでなく、電子植字への飛躍にも耐え、本質的に変わっていません。1960 年代に Lorem Ipsum のパッセージを含む Letraset シートがリリースされ、最近では Lorem Ipsum のバージョンを含む Aldus PageMaker のようなデスクトップ パブリッシング ソフトウェアで普及しました。
(行数ではなく)長さの値を取得する方法はありますか? これが必要なのは、行の 1 つに画像を入力すると、全体の高さが変化し、正確にどれだけ変化するかを知りたいからです。JTextPane
私が使用する には があり、htmlStyledDocument
はありません。を作成しましたSSCCE
が、画像の代わりに 2 つの異なるフォント サイズを使用して、 2 の高さの違いを示しましたJTextPanes
。
public class Fonts extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Fonts frame = new Fonts();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Fonts() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
textPane.setFont(new Font("Tahoma", Font.PLAIN, 11));
StyledDocument doc = textPane.getStyledDocument();
JTextPane textPane_1 = new JTextPane();
textPane_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
textPane_1.setEditable(false);
StyledDocument doc1 = textPane_1.getStyledDocument();
String s = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." +
" Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown" +
" printer took a galley of type and scrambled it to make a type specimen book. It has survived not" +
" only five centuries, but also the ";
try {
doc.insertString(0, s, null);
doc1.insertString(0, s, null);
} catch (BadLocationException e) {
e.printStackTrace();
}
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(textPane, GroupLayout.PREFERRED_SIZE, 214, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textPane_1, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
.addComponent(textPane_1, Alignment.LEADING)
.addComponent(textPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
.addContainerGap())
);
contentPane.setLayout(gl_contentPane);
}
}
2 つの JTextPanes の高さを取得する方法はありますか?