无论使用何种字体,设置(两个)JEditorPanes的高度

时间:2014-11-18 04:24:38

标签: java swing jeditorpane

在Java Swing中是否有一种方法可以找到显示一行文本所需的确切高度,而不管使用的字体/字体大小是什么?

我使用JEditorPane,我希望高度与同一面板中的另一个编辑器窗格同步。基本上,整个想法是让两个编辑器窗格具有相同的高度,具体取决于它们中的文本。

注意:我有一个监听器,它根据文本中的文本不断更改这两个窗格的高度,这样如果我在其中一个窗格中编辑代码并且高度变得比另一个高,那么新的高度(两个窗格)应该是最高的高度。

请参阅以下代码,了解我遵循的方法..:

//find the height of the pane based on the contents that is to go into them. This will
//count the number of newlines in the both the strings and will return the maximum.
//For eg if string 1 contains 3 newline chars and string 2 contains 5 chars the
//function will return 5.

int height= findheight(String str1,String str2);

JEditorPane pane1= new JEditorPane();
JEditorPane pane2= new JEditorPane();

pane1.setText(str1);  
pane2.setText(str2);

pane1.setSize(new Dimension((int) pane1.getPreferredSize().getWidth(),16*height));
pane2.setSize(new Dimension((int) pane2.getPreferredSize().getWidth(),16*height));

如果我们更改显示器不适合的字体或字体大小,上面代码中的值16用于表示字体大小为x的{​​{1}}字体。是否有解决方法或是否有内置函数来确定所需的高度?

0 个答案:

没有答案
相关问题