A Simple Introductory Java Program


To copy this program into BlueJ, please do the following:
Name the project anything you wish, but you must start with a letter, and no spaces.
If you made no errors, the class should compile without problems and you are ready to run/execute the program.

------------------------ COPY HERE (below this point) ---------------------------
/*
* This is the program we discussed in class, but I added some commments to it.
* Comments are enclosed in "slash-star to star-slash" symbols and are simply
* ignored by the compiler.
*/

/*
* First, we have some 'import' statement to import those classes we will need in
* our program. You can use a "star" to import all classes in a group, or
* package, which is convenient sometimes, or import specifically named classes
* only, which is more efficient for the compiler.
*/
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;

/*
* Next comes the basic name of the 'public class'. It usually starts with a
* capital letter and can not include any spaces. In our case we define a new
* class called 'Junk' that extends, or "is a", JFrame, and implements, or
* "acts as" an ActionListener. Virtually every Java program starts out like
* this.
*/
public class Junk extends JFrame implements ActionListener
{
/*
* Next come the fieldds, used to store things. Everything that a class
* "has" should be defined in fields at the top of the class. In our case
* the class - aka program - has two buttons and one text field, properly
* initialized according to the Java API.
*/
JButton button1 = new JButton("Click me");
JButton button2 = new JButton("No, me!");
JTextField display = new JTextField("Some text goes here");

/*
* Next comes the 'constructor', i.e. a method that has the same name as
* the class name. This is where all initialization and layout code goes.
*/
public Junk()
{
super("My First Program");

/*
* As our layout we define a 'FlowLayout', which means that everything
* is going to be arranged in a row.
*/
getContentPane().setLayout(new FlowLayout());

/*
* Now we add the various GUI elements.
*/
getContentPane().add(button1);
getContentPane().add(button2);
getContentPane().add(display);

/*
* We activate the buttons via the 'addActionListener' command.
*/
button1.addActionListener(this);
button2.addActionListener(this);

/*
* Finally we define the window size and make the window visible.
*/
setSize(400, 400);
setVisible(true);
}

/*
* The next method is called when an activated button is clicked. In it
* we decide what is actually supposed to happen when a button is clicked.
* The 'if' statement inside this method allows us to react differently
* depending on which button was clicked.
*/
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
display.setText("it tickles ..");
else if (e.getSource() == button2)
display.setText("ohh, stop ...");
}

/*
* Last is the 'main' method. Every Java program must have one such
* method, which is usually short and creates an object of the current
* type, passing execution to the constructor.
*/
public static void main(String args[])
{
Junk j = new Junk();
}
}