【问题标题】:My JSpinner does not change or accept any changes that I made on it我的 JSpinner 不会更改或接受我对其所做的任何更改
【发布时间】:2015-09-14 13:54:57
【问题描述】:

我在一个项目中工作,我必须使用 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                   
}

【问题讨论】:

  • initComponents() 定义在哪里?
  • 我省略了这段代码。这是 Netbeans 生成的。
  • 我用 InitComponents() 方法编辑了帖子
  • 您分配了两次jSpinner1,但只有其中一个 JSpinner 被添加到您的框架中。框架中的那个不是您正在修改的那个。
  • @VGR 是对的。 jSpinner1 字段在 initComponents 方法中分配,然后在 TesingSpinner 构造函数中再次分配。

标签: java swing jspinner


【解决方案1】:

@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】:

    删除initComponents()中的JSpinner定义

    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);
    

    【讨论】:

    • 我认为initComponents方法中的代码是由NetBeans GUI builder生成的。
    • 是的,这就是为什么它搞乱了逻辑。
    【解决方案3】:

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

    这允许您“输入代码作为以下代码属性之一的一部分:创建前代码、创建后代码、初始化前代码、初始化后代码、监听后代码、预填充代码、人口后代码和全部设置代码。”

    【讨论】:

      猜你喜欢
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 2013-08-15
      • 1970-01-01
      • 2014-07-10
      • 2010-09-26
      相关资源
      最近更新 更多