【问题标题】:JComponent name cannot be resolved, when used in another method在其他方法中使用时,无法解析 JComponent 名称
【发布时间】:2014-02-25 01:11:55
【问题描述】:

我正在关注 youtube 教程 (http://www.youtube.com/watch?v=wpbQ0DCFF0M) 以使用数据库表填充名为“comboAccountName”的 JCombobox。 我的数据库连接是在另一个类中设置的。

代码如下——

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 个错误需要您的帮助

comboAccountName cannot be 

已解决

发生在while循环内,在下一行

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

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

执行

当我尝试调用 PopulateJCB(); 时发生在主方法中

我知道教程视频中的代码并不完全相同,但我在这里尝试做类似的事情。请帮忙。

【问题讨论】:

    标签: java database swing oop


    【解决方案1】:

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

    所以不是:

    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 置于紧身衣中,将其绘制在一个非常狭窄的角落并制作它很难扩展或增强。只是不要这样做。


    至于这个错误:

    无法从类型中对非静态方法 PopulateJCB() 进行静态引用

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

    所以不是:

    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
    

    【讨论】:

    • 成功了!!!谢谢!!!抱歉,我是 Java 初学者,我的学习来源是书籍、youtube 和 stackoverflow 而已
    • @Satnamxv63:见编辑回答。我确实建议您在尝试进行 Swing + 数据库编码之前先阅读初学者的 Java 书籍。你正在攀登一个非常陡峭的学习曲线山,我认为如果你稍微降低山坡的坡度,你会变得更好,并且不太可能退出。
    猜你喜欢
    • 2022-07-07
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2020-05-01
    • 2016-06-23
    • 1970-01-01
    相关资源
    最近更新 更多