Java 网格布局中的JSpinner
我有一个简单的网格包布局,当我只使用文本字段时,它工作正常。当我放入一个JSpinner时,整个布局会完全混乱。我的意思是,我的三列中有两列完全消失,第二列(唯一出现的列)占用了窗口的整个宽度,无论窗口大小如何。完整的代码如下 这个问题似乎与微调器有关,如果我使用其他类型的jcomponent,比如JLabel,问题就不会表现出来。我做错了什么导致JSpinner完全破坏了布局Java 网格布局中的JSpinner,java,layout,gridbaglayout,jspinner,Java,Layout,Gridbaglayout,Jspinner,我有一个简单的网格包布局,当我只使用文本字段时,它工作正常。当我放入一个JSpinner时,整个布局会完全混乱。我的意思是,我的三列中有两列完全消失,第二列(唯一出现的列)占用了窗口的整个宽度,无论窗口大小如何。完整的代码如下 这个问题似乎与微调器有关,如果我使用其他类型的jcomponent,比如JLabel,问题就不会表现出来。我做错了什么导致JSpinner完全破坏了布局 package product_data.ui; import java.awt.EventQueue; impo
package product_data.ui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class StrippedProductEditor extends JFrame {
/**
*
*/
private static final long serialVersionUID = 5429511033322948114L;
private JLabel lblManufacturer;
private JLabel lblSku;
private JTextField mfrText;
private JTextField skuText;
private JTextArea extraText;
private JLabel lblListPrice;
private JSpinner spinner;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new StrippedProductEditor();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public StrippedProductEditor() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
gridBagLayout.rowHeights = new int[] { 0, 0, 0, };
gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0 };
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0};
getContentPane().setLayout(gridBagLayout);
lblManufacturer = new JLabel("Manufacturer");
GridBagConstraints gbc_lblManufacturer = new GridBagConstraints();
gbc_lblManufacturer.insets = new Insets(0, 0, 5, 5);
gbc_lblManufacturer.gridx = 0;
gbc_lblManufacturer.gridy = 0;
getContentPane().add(lblManufacturer, gbc_lblManufacturer);
mfrText = new JTextField();
GridBagConstraints gbc_lblMfrText = new GridBagConstraints();
gbc_lblMfrText.insets = new Insets(0, 0, 5, 5);
gbc_lblMfrText.fill = GridBagConstraints.HORIZONTAL;
gbc_lblMfrText.gridx = 1;
gbc_lblMfrText.gridy = 0;
getContentPane().add(mfrText, gbc_lblMfrText);
lblSku = new JLabel("SKU");
GridBagConstraints gbc_lblSku = new GridBagConstraints();
gbc_lblSku.insets = new Insets(0, 0, 5, 5);
gbc_lblSku.gridx = 0;
gbc_lblSku.gridy = 1;
getContentPane().add(lblSku, gbc_lblSku);
skuText = new JTextField();
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_textField_1.insets = new Insets(0, 0, 5, 5);
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.gridx = 1;
gbc_textField_1.gridy = 1;
getContentPane().add(skuText, gbc_textField_1);
skuText.setColumns(10);
extraText = new JTextArea();
JScrollPane dictScrollPane = new JScrollPane(extraText);
GridBagConstraints gbc_dict = new GridBagConstraints();
gbc_dict.insets = new Insets(0, 0, 5, 5);
gbc_dict.gridx = 2;
gbc_dict.gridy = 0;
gbc_dict.gridheight = 2;
gbc_dict.fill = GridBagConstraints.BOTH;
getContentPane().add(dictScrollPane, gbc_dict);
lblListPrice = new JLabel("List Price");
GridBagConstraints gbc_lblListPrice = new GridBagConstraints();
gbc_lblListPrice.insets = new Insets(0, 0, 5, 5);
gbc_lblListPrice.gridx = 0;
gbc_lblListPrice.gridy = 2;
getContentPane().add(lblListPrice, gbc_lblListPrice);
double lp = 99.9;
SpinnerNumberModel m = new SpinnerNumberModel(lp, 0.0d,
Double.MAX_VALUE, 0.01d);
spinner = new JSpinner(m);
GridBagConstraints gbc_spinner = new GridBagConstraints();
gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
gbc_spinner.insets = new Insets(0, 0, 5, 5);
gbc_spinner.gridx = 1;
gbc_spinner.gridy = 2;
//commenting out this line will make the layout look
//correct, but then the spinner is missing.
getContentPane().add(spinner, gbc_spinner);
this.pack();
this.setVisible(true);
}
}
出现问题的原因是组件和布局约束不足,但关键问题似乎是微调器的首选大小与其他组件的配合不好。我加了一句,一切都好了很多
spinner.setPreferredSize(new Dimension(128, 16));
尽管第2列仍然以宽度0结束。您还应该在
JTextArea
上设置一个首选大小来进行排序。或者,您可以在整个JFrame
上设置一个首选大小。如果您使用eclipse,请尝试使用swing插件
它为您提供所有swing gui功能。就我个人而言,它帮助了我很多。因此,在对代码进行了一点挖掘之后,我发现用于支持
SpinnerNumberModel
的编辑器JSpinner.numberditor
正在使用
try {
String maxString = formatter.valueToString(model.getMinimum());
String minString = formatter.valueToString(model.getMaximum());
ftf.setColumns(Math.max(maxString.length(),
minString.length()));
}
catch (ParseException e) {
// TBD should throw a chained error here
}
现在,事实证明,maxString
等于0
(是的,我知道),而minString
等于
179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
这等于411
characters…opps
现在,您可以将模型的最大值降低到更真实的值(例如一百万),或者您可以使用以下内容手动调整微调器的大小
((JSpinner.NumberEditor) spinner.getEditor()).getTextField().setColumns(10);
就我个人而言,我更喜欢选项一;) 即使微调器被注释掉,布局对我来说也不太合适。第一次显示框架时,第2列的宽度为零。如果我调整框架的大小,它会恢复。看起来你的体重不稳定。您只有默认权重,没有每个组件的权重,但布局本身上指定的行和列权重大多为零。有关此答案可能被否决的原因,请参阅