Java NetBeans 7.0中的BindingGroup问题

时间:2011-07-13 19:10:20

标签: java

我在netbeans 7.0中创建了一个名为placementrecord的包。在这个包下我创建了几个表单,其中一个表单由于一些bindingGroup问题而没有运行。我对这个bindingGroup没有任何线索,所以我无法解决错误,也没有在其他任何地方得到适当的答案。

以下是 Login.java

下名为“Login”的表单的代码
public class Login extends javax.swing.JFrame {

    /**
     * Creates new form Login
     */
    public Login() {
        super("Placement Management System");
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form
     * Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 10, 140, -1));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel1.setForeground(new java.awt.Color(0, 0, 153));
        jLabel1.setText("STUDENTID:");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jLabel1, org.jdesktop.beansbinding.ObjectProperty.create(), jLabel1, org.jdesktop.beansbinding.BeanProperty.create("background"));
        bindingGroup.addBinding(binding);

        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 10, 90, 20));

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel2.setForeground(new java.awt.Color(0, 0, 153));
        jLabel2.setText("PASSWORD:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 40, 90, 20));

        jButton1.setForeground(new java.awt.Color(0, 0, 153));
        jButton1.setText("LOGIN");
        jButton1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 170, 80, 30));

        jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField1ActionPerformed(evt);
            }
        });
        getContentPane().add(jPasswordField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 40, 140, -1));

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/new_login.jpg"))); // NOI18N
        jLabel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 410, 300));

        jButton2.setBackground(new java.awt.Color(255, 255, 255));
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/homricon9.jpg"))); // NOI18N
        jButton2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 153, 0)));
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 253, 40, 40));

        bindingGroup.bind();

        pack();
    }// </editor-fold>

    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().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.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration
}

运行错误此代码如下:

run:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
    at java.lang.Class.cast(Class.java:2990)
    at org.jdesktop.beansbinding.Binding.convertForward(Binding.java:1312)
    at org.jdesktop.beansbinding.Binding.getSourceValueForTarget(Binding.java:844)
    at org.jdesktop.beansbinding.Binding.refreshUnmanaged(Binding.java:1222)
    at org.jdesktop.beansbinding.Binding.refresh(Binding.java:1207)
    at org.jdesktop.beansbinding.AutoBinding.tryRefreshThenSave(AutoBinding.java:162)
    at org.jdesktop.beansbinding.AutoBinding.bindImpl(AutoBinding.java:199)
    at org.jdesktop.beansbinding.Binding.bindUnmanaged(Binding.java:959)
    at org.jdesktop.beansbinding.Binding.bind(Binding.java:944)
    at org.jdesktop.beansbinding.BindingGroup.bind(BindingGroup.java:143)
    at Login.initComponents(Login.java:87)
    at Login.<init>(Login.java:20)
    at Login$3.run(Login.java:107)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
BUILD SUCCESSFUL (total time: 5 seconds)

1 个答案:

答案 0 :(得分:3)

您已使用以下语句将 jLabel1 组件绑定到自身:

org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jLabel1 ,org.jdesktop.beansbinding.ObjectProperty .create(), jLabel1 ,org.jdesktop.beansbinding.BeanProperty.create(“background”));

我建议删除 jLabel1 并在设计模式下重新创建。

或者,您可以右键单击标签并转到其“属性”。您应该看到一个名为binding的选项卡。单击它并在“绑定源”选项下选择“未绑定”,以取消绑定标签。

绑定可用于两个组件,例如,键入文本字段,并在两者绑定时将输入复制到textarea中。