通过阅读
source code
您可以看到一条解释此类行为的评论:
/**
77: * Returns preferredSize of the renderer
78: *
79: * @return preferredSize of the renderer
80: */
81: public Dimension getPreferredSize()
82: {
83: if (this.getText() != null && ! this.getText().equals(""))
84: return super.getPreferredSize();
85: else
86: {
87: // If the combo box option's text is empty or null, it won't size
88: // properly (ie, it'll be way too short)... so we throw in a dummy
89: // space to trick the superclass's sizing methods.
90: String oldText = this.getText();
91: this.setText(" ");
92: Dimension d = super.getPreferredSize();
93: this.setText(oldText);
94: return d;
95: }
96: }