多个 JSpinner 以不同方式初始化
Posted
技术标签:
【中文标题】多个 JSpinner 以不同方式初始化【英文标题】:Multiple JSpinners Initialized differently 【发布时间】:2014-09-01 12:11:54 【问题描述】:我的应用程序中有 2 个 JSpinner,我为它们设置了模型、编辑器和数字格式。但是,当我运行应用程序时,第二个不显示小数部分 最初。这看起来像聚焦问题。
所以第一个初始化为 0.0 但第二个初始化为 0;我想要第二个和第一个一样。我想念这里的步骤吗?下面是我的代码。
这是我的代码
import java.text.DecimalFormat;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
public class NewJFrame extends javax.swing.JFrame
private final JSpinner.NumberEditor editor;
DecimalFormat format , format2 ;
private final JSpinner.NumberEditor editor1;
/**
* Creates new form NewJFrame
*/
public NewJFrame()
initComponents();
SpinnerNumberModel model1 = new SpinnerNumberModel(0.0,-1000.0 ,1000.0,0.1);
this.jSpinner1.setModel(model1);
editor = new JSpinner.NumberEditor(this.jSpinner1);
this.jSpinner1.setEditor(editor);
format = editor.getFormat();
format.setMinimumFractionDigits(1);
SpinnerNumberModel model2 = new SpinnerNumberModel(0.0,-1000.0 ,1000.0,0.1);
this.jSpinner2.setModel(model2);
editor1 = new JSpinner.NumberEditor(this.jSpinner2);
this.jSpinner2.setEditor(editor1);
format2 = editor1.getFormat();
format2.setMinimumFractionDigits(1);
/**
* This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
jSpinner1 = new javax.swing.JSpinner();
jSpinner2 = new javax.swing.JSpinner();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
public void mouseMoved(java.awt.event.MouseEvent evt)
formMouseMoved(evt);
);
jSpinner1.setFocusable(false);
jSpinner1.setOpaque(false);
jSpinner1.setRequestFocusEnabled(false);
jSpinner2.setFocusable(false);
jSpinner2.setOpaque(false);
jSpinner2.setRequestFocusEnabled(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(41, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(31, Short.MAX_VALUE))
);
pack();
// </editor-fold>
private void formMouseMoved(java.awt.event.MouseEvent evt)
// TODO add your handling code here:
/**
* @param args the command line arguments
*/
public static void main(String args[])
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
if ("Nimbus".equals(info.getName()))
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
catch (ClassNotFoundException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (InstantiationException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (IllegalAccessException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (javax.swing.UnsupportedLookAndFeelException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
public void run()
new NewJFrame().setVisible(true);
);
// Variables declaration - do not modify
private javax.swing.JSpinner jSpinner1;
private javax.swing.JSpinner jSpinner2;
// End of variables declaration
【问题讨论】:
请提供完整的可运行示例。说不定你自己去做了,你就会找到答案。 看到我认为第一个是重点,这就是为什么,如何设置重点初始或其他解决方案,我已经更新了我的代码 【参考方案1】:您可以直接将格式设置为编辑器的文本字段
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.text.InternationalFormatter;
public class NewJFrame extends javax.swing.JFrame
// private final JSpinner.NumberEditor editor;
DecimalFormat format , format2 ;
// private final JSpinner.NumberEditor editor1;
/**
* Creates new form NewJFrame
*/
public NewJFrame()
initComponents();
SpinnerNumberModel model1 = new SpinnerNumberModel(0.0,-1000.0 ,1000.0,0.1);
this.jSpinner1.setModel(model1);
SpinnerNumberModel model2 = new SpinnerNumberModel(0.0,-1000.0 ,1000.0,0.1);
this.jSpinner2.setModel(model2);
(((JSpinner.NumberEditor)this.jSpinner1.getEditor()).getTextField()).setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory()
@Override
public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf)
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(1);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
formatter.setMinimum(-1000.0);
formatter.setMaximum(1000.00);
return formatter;
);
(((JSpinner.NumberEditor)this.jSpinner2.getEditor()).getTextField()).setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory()
@Override
public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf)
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(1);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
formatter.setMinimum(-1000.0);
formatter.setMaximum(1000.00);
return formatter;
);
/**
* This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
jSpinner1 = new javax.swing.JSpinner();
jSpinner2 = new javax.swing.JSpinner();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
public void mouseMoved(java.awt.event.MouseEvent evt)
formMouseMoved(evt);
);
jSpinner1.setFocusable(false);
jSpinner1.setOpaque(false);
jSpinner1.setRequestFocusEnabled(false);
jSpinner2.setFocusable(false);
jSpinner2.setOpaque(false);
jSpinner2.setRequestFocusEnabled(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(41, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(31, Short.MAX_VALUE))
);
pack();
// </editor-fold>
private void formMouseMoved(java.awt.event.MouseEvent evt)
// TODO add your handling code here:
/**
* @param args the command line arguments
*/
public static void main(String args[])
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
if ("Nimbus".equals(info.getName()))
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
catch (ClassNotFoundException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (InstantiationException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (IllegalAccessException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (javax.swing.UnsupportedLookAndFeelException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
public void run()
new NewJFrame().setVisible(true);
);
// Variables declaration - do not modify
private javax.swing.JSpinner jSpinner1;
private javax.swing.JSpinner jSpinner2;
// End of variables declaration
【讨论】:
感谢您的回答,现在工作正常,我也想知道这个问题,因为我认为焦点问题是,如何消除对这些微调器的关注并设置这样的值,您对此有答案吗还有吗? 看起来您更改了格式,但该格式在一段时间后使用。焦点是一种“强制”刷新和重新绘制内容的方式。【参考方案2】:另一种选择是使用JSpinner.NumberEditor(JSpinner spinner, String decimalFormatPattern):
editor = new JSpinner.NumberEditor(this.jSpinner1, "0.0");
【讨论】:
以上是关于多个 JSpinner 以不同方式初始化的主要内容,如果未能解决你的问题,请参考以下文章