import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*in main*/
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println('x');
}
});
import java.awt.event.ActionListener; //For action listener
import javax.swing.*; //For JButton
// Inside of function or method
JButton button = new JButton();
button.addActionListener(e -> {
System.out.println("Another way of writing an action listener.");
})
import javax.swing.*;
import java.awt.event.*;
public class JButtonExample
{
public static void main(String[] args)
{
JFrame frame = new JFrame("ActionListener Example");
JButton btn = new JButton("Click here");
btn.setBounds(70,80,100,30);
//Change button text on click
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
btn.setText("OK");
}
});
frame.add(btn);
frame.setSize(250,250);
frame.setLayout(null);
frame.setVisible(true);
}
}
JButton Button = new JButton();
Button.setSize(300,300);
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
System.out.println(BlockNumber)
}
});
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("hello world");
}
});
JButton b1 = new JButton("Some Text");
b1.addActionListener(new ActionListener() {
// The Method
}
// Remember! At The End Type This:
});
public class Main extends JFrame{
//initialize integer height/width values along with declaring
//Swing component variables
private final int W = 500,
H = 500;
private JMenu file, headMenu, bgMenu;
private JMenuBar menuBar;
private JMenuItem exitItem;
//constructor
public Main(){
setTitle("Move the Head");
setSize(W, H);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initializeElements();
}
//Initializes the elements, this part is missing from your code above.
public void initializeElements(){
menuBar = new JMenuBar();
file = new JMenu("File");
exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
System.exit(0);
}
});
headMenu = new JMenu("Heads");
bgMenu = new JMenu("Backgrounds");
}
public static void main( String[] args ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Main f = new Main();
f.setVisible(true);
}
});
}
}