Java 如何重新创建元素(SWING)?
在我的表单中,我创建了JSpinner和JComboBox元素。根据JComboBox的变化,我必须使用不同的微调器型号。所以,在ComboBox listener中,我编写了spinner=newJSpinner(newModel),但它在表单上没有改变任何内容。 如何重新创建元素以查看差异Java 如何重新创建元素(SWING)?,java,swing,jspinner,Java,Swing,Jspinner,在我的表单中,我创建了JSpinner和JComboBox元素。根据JComboBox的变化,我必须使用不同的微调器型号。所以,在ComboBox listener中,我编写了spinner=newJSpinner(newModel),但它在表单上没有改变任何内容。 如何重新创建元素以查看差异 // Create default Spinner count = new JSpinner(); // Trying to replace spinne
// Create default Spinner
count = new JSpinner();
// Trying to replace spinner
product.addActionListener(e -> {
JComboBox source = (JComboBox) e.getSource();
String selectedItem = (String) source.getSelectedItem();
...
SpinnerNumberModel numberModel = getNewNumberModel(...)
count = new JSpinner(numberModel);
count.setModel(numberModel);
// repaint(); revalidate() - also don't working
});
您不应该每次都重新分配微调器。只要更换它的模型。您正在操作侦听器中分配一个新的
JSpinner
实例,并更改其模型。但此新实例不在面板中,也不可见。删除count=newjspinner(numberModel)来自动作侦听器的代码>。并更改现有微调器的模型