基于JComboBox值的Java JButton.setVisible

基于JComboBox值的Java JButton.setVisible,java,eclipse,windowbuilder,Java,Eclipse,Windowbuilder,好吧,我有麻烦了。我知道我是一个Java noob,我做过一些愚蠢的事情,但我讨厌每次被难倒时都跑到StackOverflow。不管怎样,下面是: 我正在用一个表单构建一个简单的小应用程序。填写表格,它会为您计算一些值。我有一个JComboBox,它在一系列JTextField中设置一些默认值。有两个按钮(此时不做任何事情),我希望根据JComboBox中的值显示或隐藏这些按钮。没什么大不了的,对吧 目前,我能够在JComboBox的actionPerformed()方法中查看和操作JTextF

好吧,我有麻烦了。我知道我是一个Java noob,我做过一些愚蠢的事情,但我讨厌每次被难倒时都跑到StackOverflow。不管怎样,下面是:

我正在用一个表单构建一个简单的小应用程序。填写表格,它会为您计算一些值。我有一个JComboBox,它在一系列JTextField中设置一些默认值。有两个按钮(此时不做任何事情),我希望根据JComboBox中的值显示或隐藏这些按钮。没什么大不了的,对吧

目前,我能够在JComboBox的actionPerformed()方法中查看和操作JTextField。但是,如果我试图更改JButton上的setVisible(true),就会得到一个NullPointerException

我会发布一些代码,但不是全部。我一直在用WindowBuilder在Eclipse开普勒中做这件事(我知道,我知道…),所以代码相当庞大

这里定义了一个JTextFields和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);
        }
    });
final JComboBox cboFacilityType=new JComboBox();
cboFacilityType.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
如果(isInitCBO)
返回;
String sql=“SELECT*FROM tblDefaultIndustry WHERE INDUSTRY=”+cboFacilityType.getSelectedItem()+”;
尝试
{
rs=stmt.executeQuery(sql);
while(rs.next())
{
txtLighting.setText(“”);
txtAC.setText(“”);
txtRefrig.SETEXT(“”);
txtEquip.setText(“”);
txtext.setText(“”);
如果(rs.getString(“行业”)等于(“”)
{
usageFieldsEditable(真);
btnAdd.setVisible(真);
txtLighting.requestFocusInWindow();
}否则{
setText(Integer.toString(rs.getInt(2));
setText(Integer.toString(rs.getInt(3));
setText(Integer.toString(rs.getInt(4));
setText(Integer.toString(rs.getInt(5));
setText(Integer.toString(rs.getInt(6));
usageFieldsEditable(假);
btnAdd.setVisible(假);
}
}
}捕获(SQLException e1)
{
e1.printStackTrace();
}
}
私有void usageFieldsEditable(布尔b){
//TODO自动生成的方法存根
txtLighting.setEditable(b);
txtAC.setEditable(b);
txtRefrig.setEditable(b);
txtEquip.setEditable(b);
TXT。设置可编辑(b);
}
});
起初,btnAdd表现得好像没有任何变量与之关联。我注意到WindowBuilder为JTextFields创建了私有变量,但没有为JButton创建。所以我加了一个,看看这是否有效。没有骰子。反正也没道理

希望你有足够的时间继续下去。actionPerformed()方法可以查看和操作JTextField,但不能查看和操作按钮,这是没有意义的。他们在同一个面板上,什么都在


感谢您的睿智。

为按钮设置一个字段绝对有意义,因为actionPerformed()方法需要访问按钮以使其可见或隐藏。但是您会得到一个NullPointerException,这意味着
btnAdd
字段未初始化。事实上,不是这样做

this.btnAdd = new JButton("Add");
或者干脆

btnAdd = new JButton("Add");
这将初始化btnAdd字段,您正在执行

JButton btnAdd = new JButton("Add");
它声明并初始化一个局部变量,该局部变量恰好与
btnAdd
字段同名,使该字段未初始化


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

您已将
JButton
初始化为

JButton btnAdd = new JButton("Add");
这是按钮变量的
本地初始化
,当您试图使用
JButton的对象时,它超出了范围

试试这个

声明
JButton btnAdd,然后使用初始化按钮的此引用

btnAdd = new JButton("Add");

那么,如何引用按钮来隐藏/显示actionPerformed()方法中的按钮呢?当它到达btnAdd.setVisible时(true);线路失败了,就像你现在做的一样。正如我在回答中所解释的,问题在于您没有初始化按钮字段。而是初始化具有相同名称的按钮局部变量。如回答中所述,替换
JButton btnAdd=new JButton(“添加”)
by
this.btnAdd=newjbutton(“添加”)不工作。这就是我努力自学的结果。:-)我确信我的代码有太多的问题,我将不得不回到绘图板上。