| enter = new JButton ("Click Me"); |
The initial button says "Click me" |
| enter.setActionCommand ("1"); |
The button will "scream" 1!!! when clicked |
| enter.addActionListener (this); |
Adds the action listener for the button to the applet |
| add (enter); | Adds the button to the applet |
| enter.setBackground(Color.yellow); |
sets background colour of button |
| enter.setForeground(Color.blue); | sets colour of words |
| enter.setFont(new Font("Arial", Font.BOLD, 18)); | sets the font of the words |
| JButton pretty = new JButton (createImageIcon ("img.gif")); | puts on a picture, assumes that createImageIcon is at the bottom of your code |
| JButton b1 = new JButton("Button text, could be blank", createImageIcon("gifname.gif")); | for text and a picture on your button, assumes that create image icon is at the bottom of your code |
| if (e.getActionCommand ().equals ("1")) | to access which action listener is screaming |
enter.getText ().equals ("Click Me") or just: enter.getText(); |
to get the text off the button |
| enter.setText ("Clicked"); | to set the text on the button |
| pretty.setIcon (createImageIcon ("new.jpg")); | to change the picture on the button, assumes it already was a picture |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class button extends Applet implements ActionListener
{
JButton enter;
public void init ()
{
enter = new JButton ("Click Me");
enter.setActionCommand ("1");
enter.addActionListener (this);
add (enter);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("1"))
{
if (enter.getText ().equals ("Click Me"))
enter.setText ("Clicked");
else
enter.setText ("Click Me");
}
}
/**
Makes an image icon to go on a button
@param path is a valid path to a jpg or gif image
@return an ImageIcon, or null if the path was invalid.
Directly from: http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/index.html#RadioButtonDemo
*/
protected static ImageIcon createImageIcon (String path)
{//change the red to your class name
java.net.URL imgURL = button.class.getResource
(path);
if (imgURL != null)
{
return new ImageIcon (imgURL);
}
else
{
System.err.println ("Couldn't find file: " + path);
return null;
}
}
}