E
E
Evgeny Velichko2020-02-24 19:33:05
Java
Evgeny Velichko, 2020-02-24 19:33:05

How does this work here?

import javax.swing.*;
import java.awt.event.*;

public class SimpleGuilB implements ActionListener {
    JButton button;
    public static void main(String [] args){
        SimpleGuilB gui = new SimpleGuilB();
        gui.go();

    }
    public void go(){
        JFrame frame = new JFrame();
        button = new JButton("click me");

        button.addActionListener(this); //ЧТО ЗДЕСЬ ДЕЛАЕТ THIS?

        frame.getContentPane().add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(100,300);
        frame.setVisible(true);
    }
    public void actionPerformed(ActionEvent event){
       button.setText("I ve been clicked");
        System.out.println("fjgji");
    }

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-02-24
@daemon117733

POINTS TO AN INSTANCE OF AN ENCOUNTERING CLASS!

D
Dmitry Alexandrov, 2020-02-24
@jamakasi666

You have created the SimpleGuilB class and implemented the ActionListener interface into it, which tells you to implement the actionPerformed() method.
Further in your SimpleGuilB class you say button.addActionListener() i.e. you tell the button where the events from the button should be passed and as an argument you tell this. This says that, roughly speaking, "I \ me" i.e. it was this created instance of the SimpleGuilB class that implemented the ActionListener .
Those. simply put, this inside the class says that it is "I\me\mine\mine" of the instance.
Otherwise, you could create an ActionListener implementation like this

public class SimpleGuilB{ 
... код
public void go(){
...
button.addActionListener(new ButLister()); 
}
}

public class ButLister implements ActionListener{
public void actionPerformed(ActionEvent event){
        System.out.println("fjgji");
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question