Home > Enterprise >  How to get Information from new Objects
How to get Information from new Objects

Time:12-18

how can i get Information (like Locations, Name, Size, etc.) from new Objects.

public class Labelerzeugungklasse extends JLabel {

    public Labelerzeugungklasse(String name, int x, int y, ImageIcon icon) {
        new JLabel(name);
        this.setIcon(icon);
        this.setBounds(0, 0, 80, 60);
        this.setLocation(x, y);
        DragListener drag = new DragListener();
        this.addMouseListener(drag);
        this.addMouseMotionListener(drag);

    }

}


HintergrundRukkla.add(new Labelerzeugungklasse(LabelName, x, y, iiii));

Here i generate a new JLabel, but how can i get Information/Data from this Label after the generation.

Like: "generatedJLabel".getLocation();

Best Regards Justin

CodePudding user response:

You can create a handler that points to the new object:

Labelerzeugungklasse label = new Labelerzeugungklasse(LabelName, x, y, iiii);

Add the object, as you have in your code (I am assuming HintergrundRukkla is a Jframe):

HintergrundRukkla.add(label);

Since you now have a handler to the label, you can use it to call any method the Jlabel supports, like:

label.getText();
label.getIcon();
label.getHorizontalAlignment();

You can see the supported methods here: https://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html in the 'Method Summary' section

  • Related