gpt4 book ai didi

java - 在另一个方法中使用时,无法解析 JComponent 名称

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:51 24 4
gpt4 key购买 nike

我正在按照 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() 时发生;在主要方法中

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

最佳答案

范围!您在构造函数内声明您的 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 穿上紧身衣,将其绘制在一个非常狭窄的角落并使它很难扩展或增强。只是不要这样做。


至于这个错误:

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

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

所以不是:

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 - 在另一个方法中使用时,无法解析 JComponent 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22002636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com