获取具有固定宽度的多行文本的高度,以使对话框正确resize

我想创建一个包含某种文本元素(JLabel / JTextArea等)的对话框,它是多行的并包装单词。 我希望对话框具有固定的宽度,但根据文本的大小来调整高度。 我有这个代码:

import static javax.swing.GroupLayout.DEFAULT_SIZE; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class TextSizeProblem extends JFrame { public TextSizeProblem() { String dummyString = ""; for (int i = 0; i < 100; i++) { dummyString += " word" + i; //Create a long text } JLabel text = new JLabel(); text.setText("" + dummyString + ""); JButton packMeButton = new JButton("pack"); packMeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pack(); } }); GroupLayout layout = new GroupLayout(this.getContentPane()); getContentPane().setLayout(layout); layout.setVerticalGroup(layout.createParallelGroup() .addComponent(packMeButton) .addComponent(text) ); layout.setHorizontalGroup(layout.createSequentialGroup() .addComponent(packMeButton) .addComponent(text, DEFAULT_SIZE, 400, 400) //Lock the width to 400 ); pack(); } public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new TextSizeProblem(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } 

运行程序时,它看起来像这样: alt text http://sofzh.miximages.com/java/multiline_size_1.png

但我希望对话框看起来像这样(当你按下包按钮时): alt text http://sofzh.miximages.com/java/multiline_size_2.png

我猜测问题是布局管理器在将文本显示到屏幕之前无法确定文本的正确高度。 我尝试了各种validate(),invalidate(),validateTree()等但没有成功。

这是对你的代码的改编,做你想做的事。 但是需要一点技巧来计算标签的大小并设置其首选大小。

我在这里找到了解决方案

 import static javax.swing.GroupLayout.DEFAULT_SIZE; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.text.View; public class TextSizeProblem extends JFrame { public TextSizeProblem() { String dummyString = ""; for (int i = 0; i < 100; i++) { dummyString += " word" + i; // Create a long text } JLabel text = new JLabel(); text.setText("" + dummyString + ""); Dimension prefSize = getPreferredSize(text.getText(), true, 400); JButton packMeButton = new JButton("pack"); packMeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pack(); } }); GroupLayout layout = new GroupLayout(this.getContentPane()); getContentPane().setLayout(layout); layout.setVerticalGroup(layout.createParallelGroup().addComponent(packMeButton) .addComponent(text,DEFAULT_SIZE, prefSize.height, prefSize.height)); layout.setHorizontalGroup(layout.createSequentialGroup().addComponent(packMeButton) .addComponent(text, DEFAULT_SIZE, prefSize.width, prefSize.width) // Lock the width to 400 ); pack(); } public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new TextSizeProblem(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } private static final JLabel resizer = new JLabel(); /** * Returns the preferred size to set a component at in order to render an html string. You can * specify the size of one dimension. */ public static java.awt.Dimension getPreferredSize(String html, boolean width, int prefSize) { resizer.setText(html); View view = (View) resizer.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey); view.setSize(width ? prefSize : 0, width ? 0 : prefSize); float w = view.getPreferredSpan(View.X_AXIS); float h = view.getPreferredSpan(View.Y_AXIS); return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h)); } } 

我想这就是你想要的:

 JLabel label = new JLabel("
Lots of text here...
"); // add the label to some Container.

这会将JLabel限制为200像素宽,并自动调整高度以适合文本。

我找到了解决问题的方法。 通过用JTextArea替换JLabel:

 JTextArea text = new JTextArea(); text.setText(dummyString); text.setLineWrap(true); text.setWrapStyleWord(true); 

然后调用pack(),然后调用布局管理器,再次布局组件,然后再打包另一个包:

 pack(); layout.invalidateLayout(this.getContentPane()); pack(); 

这将使布局管理器适应宽度。

完整的代码:

 import static javax.swing.GroupLayout.DEFAULT_SIZE; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class TextSizeProblem3 extends JFrame { public TextSizeProblem3() { String dummyString = ""; for (int i = 0; i < 100; i++) { dummyString += " word" + i; //Create a long text } JTextArea text = new JTextArea(); text.setText(dummyString); text.setLineWrap(true); text.setWrapStyleWord(true); JButton packMeButton = new JButton("pack"); packMeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pack(); } }); GroupLayout layout = new GroupLayout(this.getContentPane()); getContentPane().setLayout(layout); layout.setVerticalGroup(layout.createParallelGroup() .addComponent(packMeButton) .addComponent(text) ); layout.setHorizontalGroup(layout.createSequentialGroup() .addComponent(packMeButton) .addComponent(text, DEFAULT_SIZE, 400, 400) //Lock the width to 400 ); pack(); layout.invalidateLayout(this.getContentPane()); pack(); } public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new TextSizeProblem3(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } 

(你可以添加一些自定义(边框,颜色等),所以它看起来就像JLabel但我省略了)