简体   繁体   中英

In Java, how can I access methods in separate classes without inheritance?

For example, I have a class, ImageViewer, that displays a picture in a panel. Then I have another class, ImageChooser, that lets you pick an image from your directory, then displays the file path string in a textfield. In that class, I have a private inner class that calls a method from ImageViewer to display the image when a button called displayButton is clicked.

private class displayButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        displayImage(imageTextField.getText());
    }
}

My problem is that the method displayImage is in the ImageViewer class and can't be accessed by the ImageChooser class.

public void displayImage(String imageFile) {
    ImageIcon picture = new ImageIcon(imageFile);
    imageLabel.setIcon(picture);
}

Both classes extend JPanel (I have a main class that uses each panel in a frame using the BorderLayout manager), so I'm unable to access the method in ImageViewer through inheritance.

I know I could combine both classes or use a different layout manager, but I wanted to know if their was another way to do it in case I run into something like this again.

If I understand your question you could keep an ImageViewer reference, and please follow Java naming conventions. So something like,

private class DisplayButtonListener implements ActionListener {
  public DisplayButtonListener(ImageViewer viewer) {
    this.iv = viewer;
  }
  private ImageViewer iv;
  public void actionPerformed(ActionEvent e) {
    if (iv == null) return;
    iv.displayImage(imageTextField.getText());
  }
}

Because you are trying to use class A's method with class B.

lets say class A has methods such as a1(), a2(),a3() and class B has methods b1(),b2(),b3()

you can not do something like this in one of the class B's methods. a1() --> which will refer to this.a1() --> B.a1()

class B has no methods called a1().

In your situation, i think you should instantiate an ImageViewer class in ImageChooser's constructor and then call it, such as:

//ctor of ImageChooser
ImageViewer a = new ImageViewer();

and then call displayImage as

a.displayImage();

Put import packagename.classname; (note: you don't have to put the import if they're in the same package) at the top of the class you want to access the method in and create a reference. Example: methodClass.java:

package net.snugglesstuff.methods;

public class methodClass() {
    public void sayHi() {
        System.out.println("Hi");
    }
}

useMethodClass.java:

package net.snugglesstuff.run;

import net.snugglesstuff.methods.methodClass;

public class useMethodClass() {
    private static methodClass mc = new methodClass();

    public static void main(String[] args) {
        mc.sayHi();
    }
}

The main class should be net.snugglesstuff.run.useMethodClass . The result should be (updated, does work) " Hi ".

Let's suppose you have the following classes:

public class Foo {
//...
    public void fooMethod() {
        Bar bar = new Bar();
        bar.barMethod();
    }
//...
}

public class Bar {
//...
    private class LoremIpsum {
        public void loremIpsumMethod() {/*...*/}
    }
//...
    public void barMethod() {
        LoremIpsum loremIpsum = new LoremIpsum();
        loremIpsum.loremIpsumMethod();
    }
}

In this case you can reach LoremIpsum 's loremIpsumMethod from Foo by instantiating a Bar object and calling its barMethod , which, in turn instantiates a LoremIpsum object and calls its loremIpsumMethod .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM