我的JSpinner不会更改或接受我对其所做的任何更改

时间:2015-06-27 20:09:52

标签: java swing jspinner

我正在一个项目中工作,我必须使用 javax.swing.JSpinner 来表示货币增量值。
我有以下问题:当我在我的类构造函数中构建它时,如下面的代码,JSpinner不会更改,而是继续默认行为。即使我修改了启用属性,也没有发生任何事情 如何让JSpinner按我的意愿行事?
这是Netbeans中构建的代码:

public class TesingSpinner extends javax.swing.JFrame {

SpinnerNumberModel model;
JSpinner.NumberEditor editor;

/**
 * Creates new form ProbandoSpinner
 */
public TesingSpinner() {
    initComponents();
    model = new SpinnerNumberModel(0.00, -1000.0, 1000.0, 0.01);
    jSpinner1 = new JSpinner(model);
    editor = new JSpinner.NumberEditor(jSpinner1);
    jSpinner1.setEditor(editor);
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jSpinner1.setEnabled(false);
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jSpinner1.setEnabled(true);
}  

 private void initComponents() {

    jSpinner1 = new javax.swing.JSpinner();
    jLabel1 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("El Spinner");

    jButton1.setText("Bloquear");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setText("Desbloquear");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton2)))
            .addContainerGap(164, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(41, 41, 41)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1)
                .addComponent(jButton2))
            .addContainerGap(42, Short.MAX_VALUE))
    );

    pack();
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new TesingSpinner().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JSpinner jSpinner1;
// End of variables declaration                   
}

3 个答案:

答案 0 :(得分:3)

删除JSpinner

中的initComponents()定义

jSpinner1 = new javax.swing.JSpinner();

并在构造函数

中更改它
public TesingSpinner() {
    model = new SpinnerNumberModel(0.00, -1000.0, 1000.0, 0.01);
    jSpinner1 = new JSpinner(model);
    editor = new JSpinner.NumberEditor(jSpinner1);
    jSpinner1.setEditor(editor);
    initComponents();
}

实际上发生的事情是你创造了两个JSpinner;一个在构造函数中,另一个在init中。 pack()之后你控制第二个,即init一个默认微调器。

如果您无法更改initComponents中的代码,也可以执行此操作

    initComponents();
    model = new SpinnerNumberModel(0.00, -1000.0, 1000.0, 0.01);
    jSpinner1.setModel(model);
    editor = new JSpinner.NumberEditor(jSpinner1);
    jSpinner1.setEditor(editor);

答案 1 :(得分:2)

@VGR,我按照你的注释,我做的是删除我的Constructor类中的定义并在initComponents()方法中对其进行编码。我想要的行为现在正在发生,组件会更改它的属性。

public TesingSpinner() {
    initComponents();
//        javax.swing.SpinnerNumberModel model;
//        javax.swing.JSpinner.NumberEditor editor;
//        model = new javax.swing.SpinnerNumberModel(0.00, -1000.0, 1000.0, 0.01);
//        jSpinner1 = new javax.swing.JSpinner(model);
//        editor = new javax.swing.JSpinner.NumberEditor(jSpinner1);
//        jSpinner1.setEditor(editor);
}

private void initComponents() {       
    javax.swing.SpinnerNumberModel model;
    javax.swing.JSpinner.NumberEditor editor;
    model = new javax.swing.SpinnerNumberModel(0.00, -1000.0, 1000.0, 0.01);
    jSpinner1 = new javax.swing.JSpinner(model);
    editor = new javax.swing.JSpinner.NumberEditor(jSpinner1);
    jSpinner1.setEditor(editor);
...

答案 2 :(得分:0)

您可以使用NetBeans添加自己的组件初始化,以与onView(withId(R.id.editTextUserInput)).typeText(STRING_TO_BE_TYPED); 方法一起使用。例如,请参阅:How to modify/add code to the initComponents() method in Java using NetBeans?

这允许您“输入代码作为以下代码属性之一的一部分:创建前代码,创建后代码,预启动代码,启动后代码,监听后代码,预填充代码,人口后编码和全套编码。“