在其他方法中使用时,JComponent名称无法解析 [英] JComponent name cannot be resolved, when used in another method

查看:179
本文介绍了在其他方法中使用时,JComponent名称无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照YouTube教学课程( http://www.youtube.com/watch?) v = wpbQ0DCFF0M )使用数据库表填充名为comboAccountName的JCombobox。
我的数据库连接在另一个类中设置。

I'm following a youtube tutorial (http://www.youtube.com/watch?v=wpbQ0DCFF0M) to populate a JCombobox called "comboAccountName" with a database table. My database connection is setup in another class.

代码如下 -

public class Execute extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;




//---------------------------------------------------------------------------------------------------------------------


public Execute() 
{

.............other code...............
JComboBox comboAccountName = new JComboBox();
    comboAccountName.setBounds(313, 31, 302, 20);
    getContentPane().add(comboAccountName);

.............other code...............

}

void PopulateJCB()
{
    String queryString = "SELECT DISTINCT [Account Name] FROM main ORDER BY [Account Name]";
    try
    {

        Connection statJCBaccountname = DatabaseConnection.ConnectDB();
        Statement stmt = statJCBaccountname.createStatement();
        ResultSet rsJCBaccountname = stmt.executeQuery(queryString);

        while (rsJCBaccountname.next())
        {
            comboAccountName.addItem(rsJCBaccountname.getString(1));
            System.out.println(rsJCBaccountname.getString(1));
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

}

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Execute frame1 = new Execute();
    frame1.setVisible(true);
    PopulateJCB();

}

有2个错误, / p>

There are 2 errors where I'd like your help

comboAccountName cannot be 

已解决

在while循环内发生,位于

occurs inside the while loop, at following line

comboAccountName.addItem(rsJCBaccountname.getString(1));

AND

Cannot make a static reference to the non-static method PopulateJCB() from the type 

执行

当我尝试调用PopulateJCB();在主方法

occurs when I'm trying to call PopulateJCB(); in the main method

我知道教程视频中的代码不完全相同,但我想在这里做一个类似的事情。请帮助。

I know the code in the tutorial video isn't exactly the same, but I'm trying to do a similar thing here. Please help.

推荐答案

范围!您在构造函数中声明了comboAccountName,因此它只在构造函数内部可见。尝试在其他地方使用它,它失败。解决方案:在类级别的构造函数之外声明它。

Scope! You declare your comboAccountName inside of the constructor and so it is visible only inside of the constructor. Try to use it elsewhere and it fails. Solution: declare it outside of the constructor on the class level.

因此:

public class Execute extends JFrame {

  public Execute() 
  {
    JComboBox comboAccountName = new JComboBox(); // this guy is visible only in here
    comboAccountName.setBounds(313, 31, 302, 20);  // don't do this!
    getContentPane().add(comboAccountName);
  }

而是:

public class Execute extends JFrame {
  private JComboBox comboAccountName = new JComboBox();

  public Execute() 
  {
    comboAccountName.setBounds(313, 31, 302, 20);
    getContentPane().add(comboAccountName);
  }






关于你使用空布局, setBounds(...)和绝对定位。虽然对新手来说,这似乎是创建复杂的GUI的最好的方法,你处理的Swing GUI创建越多,你会发现,这将把你的GUI在一个直筒夹克,画在一个非常狭窄的角落,它很难扩展或增强。


Next we'll talk about your use of null layouts, setBounds(...) and absolute positioning. While to a newbie this seems the best way to create complex GUI's, the more you deal with Swing GUI creation, the more you will find that doing this will put your GUI in a straight-jacket, painting it in a very tight corner and making it very hard to extend or enhance. Just don't do this.


无法对类型

Cannot make a static reference to the non-static method PopulateJCB() from the type

您必须创建一个类的实例,并在实例上调用方法,而不是类本身。

You must create an instance of the class and call the method on the instance, not on the class itself.

不会:

public static void main(String[] args) {
// TODO Auto-generated method stub   // please clean your code of this before posting here
Execute frame1 = new Execute();
frame1.setVisible(true);
PopulateJCB(); 

但:

public static void main(String[] args) {
Execute frame1 = new Execute();
frame1.setVisible(true);
frame1.PopulateJCB(); // call it on the Execute instance, frame1

这篇关于在其他方法中使用时,JComponent名称无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆