JSpinner:增加编辑器框的长度 [英] JSpinner: Increase length of editor box
问题描述
我有一个 JSpinner,它显示从 0.0 到 999.0 的十进制值.它似乎工作正常,除了在编辑器框中显示四位数长的数字时,例如 123.4;然后它剪掉了最后一位数字的一部分,因为它不够长.
I have a JSpinner that displays decimal values from 0.0 to 999.0. It seems to work fine, except for when it displays a number in the editor box that is four-digits long, such as 123.4; it then cuts off part of the final digit because it is not long enough.
所以我的问题是:有谁知道如何增加 JSpinner 的编辑器窗口的长度?
So my question is: Does anyone know how to increase the length of the editor window of a JSpinner?
谢谢!
推荐答案
您可以通过
- 首先在 JSpinner 上调用
getEditor()
以获取微调器的编辑器 - 将返回的对象转换为
JSpinner.DefaultEditor
- 然后调用
getTextField()
对此.然后你可以根据需要设置它的 preferredSize.
- First calling
getEditor()
on your JSpinner to get the spinner's editor - cast the returned object to
JSpinner.DefaultEditor
- Then call
getTextField()
on this. Then you can set it's preferredSize if desired.
不过,正如trashgod 所指出的,使用正确的布局是最重要的,确保您使用的布局是最好的可能是解决此问题的最佳方法.
as noted by trashgod though, using a proper layout is paramount and being sure that the layouts you use are the best is probably the best way to solve this issue.
编辑 2: 以上是错误的,因为设置文本字段的首选大小没有任何作用.但是,您可以设置编辑器本身的首选大小,这很有效.例如,
Edit 2: The above is wrong as setting the textfield's preferred size does nothing. You can however set the preferred size of the editor itself, and that works. e.g .,
import java.awt.Dimension;
import javax.swing.*;
public class SpinnerBigTextField {
private static void createAndShowGui() {
JSpinner spinner = new JSpinner(new SpinnerNumberModel(0.0, 0.0, 999.0,
0.5));
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 100));
panel.add(spinner);
JComponent field = ((JSpinner.DefaultEditor) spinner.getEditor());
Dimension prefSize = field.getPreferredSize();
prefSize = new Dimension(200, prefSize.height);
field.setPreferredSize(prefSize);
JFrame frame = new JFrame("SpinnerBigTextField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
这篇关于JSpinner:增加编辑器框的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!