gpt4 book ai didi

java - Gridbag 布局中的 JSpinner

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:07 24 4
gpt4 key购买 nike

我有一个简单的网格包布局,当我仅使用文本字段时,它可以正常工作。当我放入 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 - Gridbag 布局中的 JSpinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26224301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com