Java 以方式设置 JTextField 宽度以包装给定的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22128813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set a JTextField width in maner to wrap a given text
提问by RiadSaadi
I have an uneditable and disabled JtextField
in which I will put a String
obtained after requesting a Database, and I want that this JtextField
wraps All my String.
我有一个不可编辑和禁用JtextField
的,我将String
在请求数据库后放置一个获取的,我希望它JtextField
包含我所有的字符串。
I saw the setColumn()
and setSize()
methods but I don't know first my String
length.
我看到了setColumn()
和setSize()
方法,但我首先不知道我的String
长度。
thank you very much.
非常感谢您。
采纳答案by Hovercraft Full Of Eels
- JTextFields can only display a single line of text, period.
- If you need a simple text component that wraps text, use a JTextArea.
- Set its columns and rows.
- Put it into a JScrollPane (if you feel the text will go beyond the number of rows specified).
- Call
setLineWrap(true)
on it - Call
setWrapStyleWord(true)
on it.
- JTextFields 只能显示一行文本,句点。
- 如果您需要一个用于包装文本的简单文本组件,请使用 JTextArea。
- 设置其列和行。
- 将其放入 JScrollPane(如果您觉得文本会超出指定的行数)。
- 呼唤
setLineWrap(true)
它 - 打电话
setWrapStyleWord(true)
给它。
Edit
You ask about resizing the JTextField as text is being added or removed. I suppose that you could always override a JTextField's getPreferredSize()
method, and revalidate/repaint its container on any change in text, but doing so does carry risk.
编辑
您询问在添加或删除文本时调整 JTextField 的大小。我想您总是可以覆盖 JTextField 的getPreferredSize()
方法,并在文本发生任何更改时重新验证/重新绘制其容器,但这样做确实存在风险。
For example:
例如:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class VaryingTextFieldSize {
protected static final int GAP = 5;
public static void main(String[] args) {
final JTextField textField = new JTextField(1) {
@Override
public Dimension getPreferredSize() {
Dimension superPref = super.getPreferredSize();
FontMetrics fontMetrics = getFontMetrics(getFont());
String text = getText();
if (text.length() > 0) {
int width = fontMetrics.charsWidth(text.toCharArray(), 0, text.length()) + GAP;
return new Dimension(width, superPref.height);
}
return superPref;
}
};
textField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
reSize();
}
@Override
public void insertUpdate(DocumentEvent e) {
reSize();
}
@Override
public void changedUpdate(DocumentEvent e) {
reSize();
}
private void reSize() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Container container = textField.getParent();
container.revalidate();
container.repaint();
}
});
}
});
JPanel enclosingPanel = new JPanel();
enclosingPanel.setPreferredSize(new Dimension(300, 100));
enclosingPanel.add(textField);
JOptionPane.showMessageDialog(null, enclosingPanel);
}
}