Let's look at this line:
panel.add(new Button("Press here :)"));
You create a new button and pass it to the add
method of panel
. If you want to do anything with the button, such as adding an ActionListener
to it, then first create the button and assign it to a variable, before passing it to panel.add
:
// Create a Button and assign it to a variable
JButton button = new JButton("Press here :)");
// Add an action listener to the button
button.addActionListener(...);
// Add the button to the panel
panel.add(button);
This is basic Java programming knowledge. See Oracle's tutorial, for example the one about variables, for more information on how to work with objects and variables.