gpt4 book ai didi

java - 多个 JSpinners 以不同方式初始化

转载 作者:搜寻专家 更新时间:2023-11-01 01:21:23 25 4
gpt4 key购买 nike

我的应用程序中有 2 个 JSpinner,我为它们设置了模型、编辑器和数字格式。但是,当我运行应用程序时,第二个应用程序不显示小数部分原来。这看起来像是聚焦问题。

所以第一个初始化为 0.0 但第二个初始化为 0;我想要第二个和第一个一样的。我想念这里吗?下面是我的代码。

enter image description here

这是我的代码

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
}

最佳答案

您可以直接在编辑器的文本字段中设置格式

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
}

关于java - 多个 JSpinners 以不同方式初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25605594/

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