Java 从事件中的不同类获取值

Java 从事件中的不同类获取值,java,swing,instance,instance-variables,Java,Swing,Instance,Instance Variables,我有两门课: public jComboBox() { ... // this is a autocomplete jComboBox btw ... combo.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent ie) { if(ie.getStateChange() == 1) { String selectedI

我有两门课:

public jComboBox() {
... // this is a autocomplete jComboBox btw
...
   combo.addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent ie) {

            if(ie.getStateChange() == 1) {
                String selectedItem = (String)getSelectedItem();
                randomMethod(selectedItem);

         }
        }
    });
}

 private void randomMethod(String selectedItem){
    someClass sc = new someClass();
    String randomString = selectedItem;
    sc.getRandomString(randomString);

}


这个方法行吗?如果不是,我需要在这个问题上做一些选择,因为我遇到了一些问题,例如,使用defaultTableModel.setRowCount0是因为该表不会为空,除非我将setRowCount0放在某个类中的其他方法上。

基本java访问说明符东西。。。。。如何从randomMethod调用此私有方法getRandomString?类的私有方法的可见性仅限于该类,而不是其他任何地方。因此,请输入以下代码:

 private void randomMethod(String selectedItem){
    someClass sc = new someClass();
    String randomString = selectedItem;
    fs.getRandomString(randomString); // This will not work

}
由于访问说明符private,将无法工作。如果允许访问权限特定于您拥有的软件包,则可以将其更改为:

protected void getRandromString(String randromString) {...}
为了证明我的意思:

package com.stackoverflow.solutionmaker;

public class Aclass {

    public Aclass(){
        somePrivMethod();
    }

    public void aMethod(){
        System.out.println("Can see me from anywehre bcoz I am public");
    }

    private void somePrivMethod(){
        System.out.println("Cannot find me from anywhere because I am private t Aclass");
    }

}
现在是跑步者课程:

package com.stackoverflow.solutionmaker;

public class StackOverflowSolutionsRunner {

    public static void main(String[] args) {

        Aclass aClass = new Aclass(); // It will display"Cannot find me from anywhere because I am private t Aclass"

        aClass.aMethod(); // It will display "Can see me from anywehre bcoz I am public

        aClass.somePrivMethod(); // Will throw a compile-time error


    }
}

这是一个很好的练习,可以从命令行编译这两个命令,并查看得到的错误消息。或者,使用Eclipse smart IDE或Jcreator,您可以看到您的私有访问说明符导致出现红色消息。

第二个类肯定不好。如何将此函数放在类定义之外的同一文件中?它在类中..'“公共类”是一个constructor@meatno你能不能不使用速记并清楚你的意思?我现在正在使用eclipse。。我真的没有收到任何错误消息:|我不知道为什么NVM是在公共im上|
package com.stackoverflow.solutionmaker;

public class StackOverflowSolutionsRunner {

    public static void main(String[] args) {

        Aclass aClass = new Aclass(); // It will display"Cannot find me from anywhere because I am private t Aclass"

        aClass.aMethod(); // It will display "Can see me from anywehre bcoz I am public

        aClass.somePrivMethod(); // Will throw a compile-time error


    }
}