我正在項目中工作,我必須使用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
}
initComponents()定義在哪裏? –
我省略了這段代碼。這是Netbeans生成的。 –
我使用InitComponents()方法編輯了文章 –