Java: Creating a screen

In this tutorial I will show you how to create a screen for your program. In this tutorial I will show you the basic empty screen. Later tutorials will also be about how to fill them with certain things.
This is the code I will start with.


package net.tutorial.src;

public class Tutorial
{
       public static void main(String[] args)
       {
       }
}

The first thing you need to do is make this file extend Canvas. To do that you will need this code.


public class Tutorial extends Canvas


When you add this code to your file you will get an error under Canvas. To fix that you will have to import the Canvas file. There are 3 ways of importing a file.
1. Hover your mouse over the error and click Import.
2. Press Ctrl, Shift, O.
3. Type it yourself.
The code you will get or have to write has to be below package and above the public class line. It will look like this.


import java.awt.Canvas;


When you do this you will get a yellow line under the class name. This is not an error, but a problem. You can run the application with this, but it shouldn’t be in there.
To fix this problem you have to hover your mouse over the class name and click the Add default serial version ID. This will add these lines of code to your file.


/**

        *
        */
       private static final long serialVersionUID = 1L;

The first 3 lines are comments. There are 2 types of comments. This is the first one. The second one can be used by adding // to your code. Anything behind that or inside of the comments above will not be read as code and will simply be skipped. This is useful when you have some test code and you want to remove it from a release, but not lose the code. It can also be used to explain what certain things in your code do.
Right now you can simply remove these comments and the file should now look like this.


package net.tutorial.src;

import java.awt.Canvas;
public class Tutorial extends Canvas
{
       private static final long serialVersionUID = 1L;
       public static void main(String[] args)
       {
       }
}

Now when you run it nothing will happen.
To make something happen you will have to add this line of code between the start of the class and the start of the main method.


static JFrame frame = newJFrame();


You will get an error under JFrame which can be fixed by importing the file like you did earlier with Canvas. It will add this line of code.


import javax.swing.JFrame;


JFrame is the file you will be using to create the screen. frame is the variable name you give it and the = new JFrame() part means that you use that file when you refer to frame. The word static simply means you can use it inside of a static method like main.
Next up you will have to add 2 lines of code to the main method to make the screen show up. The first one looks like this.


frame.setSize(640, 480);


setSize is the method you use to set the size of your screen. The first parameter in here is the width and the second one the height of your screen. frame is simply the file where this method is used from. This line of code will create a screen of 640 width and 480 height.
If you run it now you will still not see anything. That is because we haven’t told frame to show the window. To make it show up you will have to add this line of code.


frame.setVisible(true);


Once again setVisible is the method we will use which is inside of the frame file. The parameter in here can be true or false. If it’s true it will show up and if it is false it will not.
The file should look like this.


package net.tutorial.src;

import java.awt.Canvas;
import javax.swing.JFrame;
public class Tutorial extends Canvas
{
       private static final long serialVersionUID = 1L;
      
       static JFrame frame = new JFrame();
      
       public static void main(String[] args)
       {
             frame.setSize(640, 480);
             frame.setVisible(true);
       }
}

Now when you run the application by hitting the green arrow on the top in Eclipse you will get a 640 by 480 white box.
In the next tutorial I will show you how to change some of the things in that screen.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">