سؤال

Is it possible to control instances like variables, jcomponents, timer in other external classes?

For example this is my Class1

public class Class1 extends JFrame {
    JLabel lbl = new JLabel("Hello");
    public Class1() {
        super("Class1");
        Container c = getContentPane();
        setLayout(null);
        c.add(lbl);
        lbl.setBounds(0,0,100,20);
        Class2.process();
        setSize(200,100);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    public static void main(String var[]) {
        new Class1();
    }
}

You can see there's Class2.process(); Here's the other class externally but in the same folder

public class Class2 {
    public static void process() {
        // I want to control lbl from Class1 class inside this method like
        // lbl.setVisible(false);
    }
    public static void main(String args[]) {
        //
    }
}

Is it possible? Sorry. I can't find answers on other website.

هل كانت مفيدة؟

المحلول

You have to pass the instance of JLabel e.g.;

JLabel lbl = new JLabel("Hello"););
Class2.process(lbl);


public class Class2 {
    public static void process(JLabel lbl) {
        // I want to control lbl from Class1 class inside this method like
        lbl.setVisible(false); // this will change your Class1 Jlabel
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top