• We just launched and are currently in beta. Join us as we build and grow the community.

Introduction to javax.swing Library

Khanh1690

Social Retargeting Pro
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
137
Likes
112
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial you will find a step by step guide about creating your first Java Application with Graphical User Interface using Eclipse IDE.

Swing library is one of the most used libraries to create applications with Graphical User Interface (GUI). This library has a large set of different GUI components. We will start from the simplest application - "Hello World!" application.
  1. Open the Eclipse IDE. Choose "File" -> "New" -> "Project" from menu.
    1_1.jpg

  2. Next step is to enter the project's name. We will name our project "HelloWorld". After entering project's name, press "Finish" to create project
    2_0.jpg

  3. Now we will add a class to our project. This class will have the main method. Right click on the project's name in Package explorer and choose "New" -> "class".
    3_2.jpg

  4. Now, enter the name of class and choose option, which will create the main method automatically.
    4_1.jpg

  5. After creating the Hello_World class, Eclipse will generate the following code
    1. public

      class

      Hello_World {

    2. public

      static

      void

      main(

      String

      [

      ]

      args)

      {
    3. // TODO Auto-generated method stub

    4. }
    5. }

    First of all, we have to know the fact that if we want to create a frame application, our class must be inherited from JFrame class. That's why we have to add next import import

    javax.swing.JFrame

    ;

    and then class can extend JFrame class import

    javax.swing.JFrame

    ;

    Now it looks like this code:

    1. import

      javax.swing.JFrame

      ;

    2. public

      class

      Hello_World extends

      JFrame

      {

    3. public

      static

      void

      main(

      String

      [

      ]

      args)

      {
    4. // TODO Auto-generated method stub

    5. }
    6. }
  6. The next step is to add GUI components to our frame.
    We want to have "Hello World!" message in the frame. Often, class, called JLabel it's used to show different text in frame. So, we have to add another import to the code: import

    javax.swing.JLabel

    ;

    Traditionally, all GUI elements are initialized in the constructor. We will act in the same way.

    To create an instance of JLabel, write the following code:
    1. JLabel

      helloLabel;
    2. helloLabel =

      new

      JLabel

      (

      "Hello World"

      )

      ;

    The constructor of JLabel class takes a string as an argument. This string will be displayed on the label. To find more information about JLabel class, read this
  7. Next step is to modify some properties of the text, displayed in the label. To change the color of the text call setForeground

    method of the JLabel object helloLabel.setForeground

    (

    Color

    .RED

    )

    ;

    The parameter of this method is the color of the text. To use color constants, please, add import

    java.awt.Color

    ;

    import.

    By default, text in the label is aligned to the left part. We will change the alignment to the center using setHorizontalAlignment

    method:

    helloLabel.setHorizontalAlignment

    (

    SwingConstants

    .CENTER

    )

    ;

    SwingConstants class it's a set collection of different constants used in Java. CENTER

    constant is used to align text to the center of component.

    Now we will change the font of the text, displayed on label. We wil call setFont

    method to change the font.
    It will look like this helloLabel.setFont

    (

    new

    Font

    (

    "Arial"

    , Font

    .PLAIN

    , 36

    )

    )

    ;

    where "Arial" is font's name, "Font.PLAIN" is font's style(It can be also Font.Bold and Font.Italic) and 36 - size of font. We have finished the process of initialization. Now, the constructor looks like this:
    1. Hello_World(

      )

      {
    2. JLabel

      helloLabel;
    3. helloLabel =

      new

      JLabel

      (

      "Hello World"

      )

      ;
    4. helloLabel.setForeground

      (

      Color

      .RED

      )

      ;
    5. helloLabel.setHorizontalAlignment

      (

      SwingConstants

      .CENTER

      )

      ;
    6. helloLabel.setFont

      (

      new

      Font

      (

      "Arial"

      , Font

      .PLAIN

      , 36

      )

      )

      ;
    7. add(

      helloLabel)

      ;
    8. }

    The last step is to add helloLabel to the frame. For this we will call add

    method. in the line 7.
  8. Now, let's create an instance of our class in the main method:
    1. Hello_World HW =

      new

      Hello_World(

      )

      ;

    Next, we have to set size of the frame. For this we call SetSize

    method:
    HW.setSize

    (

    320

    , 240

    )

    ;

    After this, we make the frame visible:
    HW.setVisible

    (

    true

    )

    ;

    The full project's code is:
    1. import

      java.awt.Color

      ;
    2. import

      java.awt.Font

      ;

    3. import

      javax.swing.JFrame

      ;
    4. import

      javax.swing.JLabel

      ;
    5. import

      javax.swing.SwingConstants

      ;

    6. public

      class

      Hello_World extends

      JFrame

      {
    7. Hello_World(

      )

      {
    8. JLabel

      helloLabel;
    9. helloLabel =

      new

      JLabel

      (

      "Hello World"

      )

      ;
    10. helloLabel.setForeground

      (

      Color

      .RED

      )

      ;
    11. helloLabel.setHorizontalAlignment

      (

      SwingConstants

      .CENTER

      )

      ;
    12. helloLabel.setFont

      (

      new

      Font

      (

      "Arial"

      , Font

      .PLAIN

      , 36

      )

      )

      ;
    13. add(

      helloLabel)

      ;
    14. }
    15. public

      static

      void

      main(

      String

      [

      ]

      args)

      {
    16. // TODO Auto-generated method stub
    17. Hello_World HW =

      new

      Hello_World(

      )

      ;
    18. HW.setSize

      (

      320

      , 240

      )

      ;
    19. HW.setVisible

      (

      true

      )

      ;
    20. }
    21. }

    And that's all for our application. Let's run it!
    5.jpg


In the next article we will speak about other swing components and events handling model in Java.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top