我有一个简单的网格袋布局,当我仅使用文本字段时,它可以正常工作。当我放入一个JSpinner时,整个布局被完全弄乱了。我的意思是说我的三列中的两列完全消失了,第二列是出现的唯一一列,无论窗口的大小如何,它都会占用整个窗口的宽度。完整的代码如下。

这个问题似乎与微调器特别相关,如果我使用其他类型的JComponent(如JLabel),则问题不会自行显现。我做错了什么导致JSpinner完全破坏了布局?

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);
    }

}

最佳答案

因此,在深入研究代码之后,我发现JSpinner.NumberEditor(用于支持SpinnerNumberModel的编辑器)正在通过使用...来计算字段columns属性。

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个字符... opps ...

现在,您可以将模型的最大值减小到更切合实际的水平(例如,一百万),或者可以使用以下方法手动调整微调器尺寸:

((JSpinner.NumberEditor) spinner.getEditor()).getTextField().setColumns(10);


就个人而言,我更喜欢选择一种;)

关于java - JSpinner在gridbag布局中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26224301/

10-10 13:57