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.
- Start BlueJ
- Create a new, empty project
If you made no errors, the class should compile without problems and you are ready to run/execute the program.
- Create a new "class" in your project
- Make sure to name that class '"Junk", including the capitalization - you have no choice here, "Junk" is it!
- Open that class and erase everything
- Copy everything below the line --- COPY HERE --- into the "Junk" class
- Compile everything
------------------------ 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.
* 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
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.
super("My First Program");
* As our layout we define a 'FlowLayout', which means that everything
* is going to be arranged in a row.
* Now we add the various GUI elements.
* We activate the buttons via the 'addActionListener' command.
* Finally we define the window size and make the window visible.
* 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();