gpt4 book ai didi

java - 新手问题: how to access a created JComboBox instance from another method in the same class or else

转载 作者:行者123 更新时间:2023-12-02 00:47:29 25 4
gpt4 key购买 nike

如果我有:

Class A {

    // bunch of stuff here

public void intitialize() {
// bunch of stuff here

JComboBox jBox = new JComboBox()

formatBox.addItem(//some objects added here))
//I do the rest of my business in this intitialize method

}

}

如果我想添加另一个在调用时可以在 jBox 上操作的方法,我该怎么做?此代码不起作用:

Class A {

// bunch of stuff here
public void intitialize() {
// bunch of stuff here
JComboBox jBox = new JComboBox()
formatBox.addItem(//some objects added here))
//I do the rest of my business in this intitialize method
}

//newly added method
public void anotherMethod(){

jBox.removeItem(//some item here)

}

}

允许从类或其他类的不同方法访问 jBox 的正确编写方法是什么?为什么它不能这样工作?

最佳答案

将 JComboBox 定义为 A 类中的字段,并删除 initialize 方法中的类型声明:

Class A {
private JComboBox jBox;
// bunch of stuff here
public void intitialize() {
// bunch of stuff here
jBox = new JComboBox()
formatBox.addItem(//some objects added here))
//I do the rest of my business in this intitialize method
}

//newly added method
public void anotherMethod(){

jBox.removeItem(//some item here)

}

}

关于java - 新手问题: how to access a created JComboBox instance from another method in the same class or else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4541054/

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