About this site          Contents        Our Experts 
  Language:  eng/rus    

  Information
      Technologies
         for Enterprises

 Review, publications
 Efficiency
 Terms, Abbreviations
 Standards




Java Programming Language

Lesson 1. The window of the class “Frame”.

CONTENTS:
  • 1. Creating the Empty Frame.
  • 2. Adding Functionality to the Frame.
  • 3. Short Analysis and Adjunction.
  • 4. Running the Project.

 For to create the elementary window “Frame” it is not necessary to write a program code manually, because it is possible to use the graphic interface which is accessible in Netbeans IDE ®. Let's to execute the steps quoted below.

1. Creating the empty Frame.

  • Start “NetBeans IDE”.
  • Choose: File > New File.
  • In the New File dialog box, choose the category: “Swing GUI Forms”, and the file type: “Jframe Form”.
  • Click Next (Figure 1).

Jframe Form

Figure 1.



  • In the “Project” Name field” type: «TestFrame1».
  • Click Finish.


 As a result, a Java Application class «TestFrame1» would be created. And you can to see an empty frame in "Design" mode (Figure 2).

Class TestFrame1

Figure 2.



  If you want to look the class code (by selecting «Sourse» mode here), you can to see: it's really haven’t any label and button descriptions inside.

2. Adding Functionality to the Frame.

 Then, we must:
  • Open: «GUIFormExamples>Source Pakages>Examples>Find.java».
 This “Examples” we can to load from the “Start page” menu, if they are absent here.
 Now, we see our Class in “Design” mode.
  • In the frame select Label “Find what” and press “Copy”.
  • Return again to «TestFrame1», and press “Insert”


 Now, our Class has Label “Find what” (Figure 3).

Jframe Form

Figure 3.



Then, we pass back to a window “Find.java”, and:

  • Copy the Button “Find”.
  • Return to the "TestFrame1.java" and press “Insert”.
  • Move the inscription and the button to the necessary positions inside the frame.
  • To allocate text of the Label, and to write here: "Testing your first frame",
and to allocate the Button Name text, and to write here: "OK" (Figure 4).

 For referential use: The menu: “Palette>Swing Containers” allows you to add other components in this frame. The bottom menu “Properties” allows you to change their format and properties,
for example, any size, Horizontal Alignment, and others. You can to test it.


The Swing Containers menu

Figure 4.



 After you will do all these changes, save the changes by choosing File>Save.


3. Short analysis and adjunction.

 Select «Sourse» mode again, we can to see the listing:

… Public class TestFrame1 extends javax.swing.Jframe { … }

Thus and so, we have created our class “TestFrame1”.
We shall remind, that this name should coincide with a name of the file containing the initial text of this class necessarily!
The class “TestFrame1”, determined by us with the help of a keyword extends is inherited from class Jframe.
Class “Jframe” is determined in library of classes “javax.swing.Jframe”.
“Jframe” is an extended version of “java.awt.Frame”.

 And now we see two lines below:

  private javax.swing.Jbutton jButton1;
  private javax.swing.Jlabel jLabel1;

 Both of them belong to “Swing” class. “Jframe” is a one of the top-level components in “Swing”.
Both of them – from the Package named “javax.swing”. About packages,
about using of this and other packages, and adding the jar- libraries in our applets we would to discuss in other lessons.

 Now, we need to create an “HTML file” for our class. It will be necessary for us
for to start the program with this class in a browser. It’s very simple to do.

  • Click <default package> with the right button of your mouse.
  • Choose in the menu: New> HTML file.
  • Set a name of a file, for example: "TestJavahtml.html".
  • Press the button “Finish”.
  • To correct the text.

 The text of the HTML file must be as it is in Listing 1.

Listing 1. File TestJavahtml.html.

4. Running the Project.

 Let's to check up a configuration of our project. For this purpose we shall:.

  • Open the “Project Properties” window.
  • In the field “Main Class” set the main class: “TestFrame1.java” (Figure 5).

TestFrame1.java

Figure 5.



 Then we can to run our Project.
Choose: Run > Run Project. As a result, you should to see the next notification:
 run:
 BUILD SUCCESSFUL (total time: 1 minute 22 seconds).

 And the next figure shows (Figure 6):

Our Frame

Figure 6.



 Congratulations! Your program works!  If there are errors, they would be marked with red letters of the Source Editor.
The letters in the left margin indicate errors for the corresponding lines,
and ones in the right margin show all of the areas, including errors in lines that aren’t visible.
You can to move mouse over an error mark, for to get a description of the error.

NEXT >

  See also:
• 1. Preface.
• 3. Preparation for lessons.
• 4.  Other Lessons.





Return to a main page




    All rights reserved. © 2021   |
  Contact us for more information on this issue and copyrights.