Len*_*din 8 java swing layout-manager
我想创建一个包含某种文本元素(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("<html>" + dummyString + "</html>");
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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
运行程序时,它看起来像这样: alt text http://lesc.se/stackoverflow/multiline_size_1.png
但我希望对话框看起来像这样(当你按下包按钮时): alt text http://lesc.se/stackoverflow/multiline_size_2.png
我猜测问题是布局管理器在将文本显示到屏幕之前无法确定文本的正确高度.我尝试了各种validate(),invalidate(),validateTree()等但没有成功.
Lau*_*t K 10
这是对你的代码的改编,做你想做的事.但是需要一点技巧来计算标签的大小并设置其首选大小.
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("<html>" + dummyString + "</html>");
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));
}
}
Run Code Online (Sandbox Code Playgroud)
我找到了解决我的问题的方法。通过用 JTextArea 替换 JLabel:
Run Code Online (Sandbox Code Playgroud)JTextArea text = new JTextArea(); text.setText(dummyString); text.setLineWrap(true); text.setWrapStyleWord(true);
并调用 pack() 然后调用布局管理器以再次布局组件,然后是另一个包:
Run Code Online (Sandbox Code Playgroud)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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
(您可以添加一些自定义(边框、颜色等),使其看起来就像 JLabel,但我已将其省略)
我想这就是你想要的:
JLabel label = new JLabel("<html><div style=\"width:200px;\">Lots of text here...</div></html>");
// add the label to some Container.
Run Code Online (Sandbox Code Playgroud)
这会将JLabel限制为200像素宽,并自动调整高度以适合文本.

| 归档时间: |
|
| 查看次数: |
6959 次 |
| 最近记录: |