String text; Icon image;
JButton btn = new JButton(text);
JButton btn = new JButton(text, image);
JButton btn = new JButton(image);
JButton btn; ActionListener listener; boolean b;
//--- Buttons always have action listeners. btn.addActionListener(listener); btn.setEnabled(b);
actionPerformed()
method is called for all of the button's listeners.
It is passed an ActionEvent, which is generally ignored,
but can be used to identify which component generated the event if
several share the same listener. The example below shows
the creation of a button, attaching a listener, and adding
the button to a container.
JButton mybtn = new JButton("Do Something");
mybtn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
doMyAction(); // code to execute when button is pressed
}
}
);
. . .
content.add(mybtn); // add the button to a JPanel (eg, content).