2015-06-27 102 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); 
... 
相關問題