How to call a Jlabel with its content from another Class?

Asked

Viewed 34 times

0

In a certain class I have a Jlabel thus:

public class Links {

private JLabel link;

    lblLink.addMouseListener(new MouseAdapter() {
                                        public void mouseClicked(MouseEvent arg0) {
    
                                            BareBonesBrowserLaunch.openURL("Link");
    
                                        }
                                    });
    }

And several others Jlabel also. How to call these Jlabel in another Class (Type in my Main Class)?

1 answer

1

It can be done like this:

import javax.swing.*;

public class ColecaoLabels {

    private JLabel linkGoogle = new JLabel("www.google.com");
    private JLabel linkYoutube = new JLabel("www.youtube.com");
    private JLabel linkGithub = new JLabel("www.github.com");

    public JLabel getLinkGoogle() {
        return linkGoogle;
    }

    public JLabel getLinkYoutube() {
        return linkYoutube;
    }

    public JLabel getLinkGithub() {
        return linkGithub;
    }
}

And its main class:

import javax.swing.*;

public class Principal {

    public static void main(String[] args) {
        ColecaoLabels colecao = new ColecaoLabels();

        JLabel google = colecao.getLinkGoogle();
        JLabel youtube = colecao.getLinkYoutube();
        JLabel github = colecao.getLinkGithub();
    }
}
  • Thank you Adriano Siqueira.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.