好吧,我受阻了。我知道我是Java新手,并且做过一些愚蠢的事情,但是我讨厌每次遇到麻烦时都要运行StackOverflow。无论如何,这里是这样:

我正在用表单构建一个简单的小应用程序。填写表格,它会为您计算一些值。我有一个JComboBox,它在一系列JTextField中设置一些默认值。根据JComboBox中的值,有几个按钮(此时不执行任何操作)是可见的还是隐藏的。没关系,对吗?

目前,我可以在JComboBox的actionPerformed()方法中查看和操作JTextField。但是,如果我尝试在JButton上更改setVisible(true),则会收到NullPointerException。

我将发布一些代码,但不是全部。我一直在使用WindowsBuilder在Eclipse Kepler中进行此操作(我知道,我知道...),因此代码相当庞大。

这是定义JTextField和JButton之一的地方:

    txtAC = new JTextField();
    txtAC.setFont(new Font("Tahoma", Font.BOLD, 12));
    txtAC.setHorizontalAlignment(SwingConstants.CENTER);
    txtAC.setToolTipText("Enter the percentage of facility electric usage resulting from air conditioning.");
    txtAC.setForeground(new Color(0, 102, 204));
    txtAC.setBounds(143, 180, 53, 20);
    contentPane.add(txtAC);
    txtAC.setColumns(10);

    JButton btnAdd = new JButton("Add");
    btnAdd.setToolTipText("Click the Add button to include the custom facility type to the Facility Type dropdown list.");
    btnAdd.setBounds(573, 60, 89, 20);
    btnAdd.setVisible(false);
    contentPane.add(btnAdd);


这是JComboBox上的actionPerformed方法:

    final JComboBox cboFacilityType = new JComboBox();
    cboFacilityType.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if (isInitCBO)
                return;

            String sql = "SELECT * FROM tblDefaultIndustry WHERE INDUSTRY = '" + cboFacilityType.getSelectedItem() +"'";


            try
            {
                rs = stmt.executeQuery(sql);
                while (rs.next())
                {
                    txtLighting.setText("");
                    txtAC.setText("");
                    txtRefrig.setText("");
                    txtEquip.setText("");
                    txtResistive.setText("");

                    if (rs.getString("INDUSTRY").equals("<User Defined>"))
                    {
                        usageFieldsEditable(true);
                        btnAdd.setVisible(true);
                        txtLighting.requestFocusInWindow();
                    }else{
                        txtLighting.setText(Integer.toString(rs.getInt(2)));
                        txtAC.setText(Integer.toString(rs.getInt(3)));
                        txtRefrig.setText(Integer.toString(rs.getInt(4)));
                        txtEquip.setText(Integer.toString(rs.getInt(5)));
                        txtResistive.setText(Integer.toString(rs.getInt(6)));
                        usageFieldsEditable(false);
                        btnAdd.setVisible(false);

                    }
                }
            }catch (SQLException e1)
            {
                e1.printStackTrace();
            }

        }

        private void usageFieldsEditable(boolean b) {
            // TODO Auto-generated method stub
            txtLighting.setEditable(b);
            txtAC.setEditable(b);
            txtRefrig.setEditable(b);
            txtEquip.setEditable(b);
            txtResistive.setEditable(b);
        }
    });


最初,btnAdd的行为就像它没有任何关联的变量。我注意到WindowBuilder为JTextField创建了私有变量,但没有为JButton创建私有变量。因此,我添加了一个以查看是否可行。没有骰子。反正没道理。

希望您有足够的机会继续进行下去。 actionPerformed()方法可以看到和操作JTextField而不是按钮是没有意义的。它们在同一小组中,并且在所有内容中。

感谢您的圣人智慧。

最佳答案

绝对有必要为按钮添加一个字段,因为actionPerformed()方法需要访问按钮才能使按钮可见或隐藏。但是您会收到NullPointerException,这意味着btnAdd字段未初始化。实际上,与其做

this.btnAdd = new JButton("Add");


或简单地

btnAdd = new JButton("Add");


这将初始化btnAdd字段,您正在执行

JButton btnAdd = new JButton("Add");


这将声明并初始化一个本地变量,该变量与btnAdd字段具有相同的名称,而此字段未初始化。

旁注:您应该学习使用布局管理器,而不是对组件的边界进行硬编码,从而使应用程序在设置与您的设置不同的机器上很难看。而且,您还应该尝试不要将使用JDBC的数据访问代码与UI代码混合在一起。将数据访问委托给另一个对象。

10-07 19:21
查看更多