2015-06-27 98 views
1

我正在项目中工作,我必须使用javax.swing.JSpinner来表示货币增量值。
我遇到以下问题:当我像下面的代码一样将其构建在类构造函数中时,JSpinner不会更改,而是继续执行默认行为。即使我修改了启用属性,也没有任何反应
我如何才能使JSpinner按照我的意愿行事?
这是内置在Netbeans中的代码:
我的JSpinner不会更改或接受我对它所做的任何更改

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     
} 
+0

initComponents()定义在哪里? –

+0

我省略了这段代码。这是Netbeans生成的。 –

+0

我使用InitComponents()方法编辑了文章 –

回答

3

删除JSpinner定义initComponents()

jSpinner1 =新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); 
+0

我认为'initComponents'方法中的代码是由NetBeans GUI构建器生成的。 –

+1

是的,这就是为什么它搞乱了逻辑。 –

0

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

这使您可以“输入代码作为以下代码属性之一的一部分:预创建代码,创建后代码,预初始代码,后启动代码,后置侦听程序代码,前人口代码,后人口代码和后置代码。“

2

@VGR,我遵循你的注释,我做的是删除我的构造函数类中的定义,并在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); 
... 
相关问题