Difference between revisions of "Java"

From Sinfronteras
Jump to: navigation, search
(Página creada con «'''GUI''' stands for Graphical User Interface. '''Swing:''' It's a '''GUI widget toolkit''' for '''Java'''. It is part of Oracle's Java Foundation Classes (JFC) – an AP...»)
 
Line 8: Line 8:
  
 
It's the user-interface widget toolkit preceding '''Swing'''. The AWT is part of the Java Foundation Classes (JFC) — the standard API for providing a graphical user interface (GUI) for a Java program. Some elements of this are still used.
 
It's the user-interface widget toolkit preceding '''Swing'''. The AWT is part of the Java Foundation Classes (JFC) — the standard API for providing a graphical user interface (GUI) for a Java program. Some elements of this are still used.
 
  
 
* '''On the Oracle website you can find a wealth of examples covering everything you will ever need to do using a Swing interface.'''
 
* '''On the Oracle website you can find a wealth of examples covering everything you will ever need to do using a Swing interface.'''
 
* '''You can find full tutorials and also API documentation for each component.'''
 
* '''You can find full tutorials and also API documentation for each component.'''
 
* '''The Oracle website contains so many examples, it really is worth taking the time to have a look through the website to get a feel for what can be done.'''
 
* '''The Oracle website contains so many examples, it really is worth taking the time to have a look through the website to get a feel for what can be done.'''
 +
  
 
==Introduction to Swing components==
 
==Introduction to Swing components==

Revision as of 22:36, 24 September 2017

GUI stands for Graphical User Interface.

Swing:

It's a GUI widget toolkit for Java. It is part of Oracle's Java Foundation Classes (JFC) – an API (Application programming interface) for providing a graphical user interface (GUI) for Java programs. Swing is currently the main user interface package which is used for creating desktop applications.

AWT (Abstract Window toolkit):

It's the user-interface widget toolkit preceding Swing. The AWT is part of the Java Foundation Classes (JFC) — the standard API for providing a graphical user interface (GUI) for a Java program. Some elements of this are still used.

  • On the Oracle website you can find a wealth of examples covering everything you will ever need to do using a Swing interface.
  • You can find full tutorials and also API documentation for each component.
  • The Oracle website contains so many examples, it really is worth taking the time to have a look through the website to get a feel for what can be done.


Introduction to Swing components

The JFrame can be considered to be base to which every other component is added.

Therefore if we wanted to create a simple interface with a text field and a button we would first need to create a JFrame to hold everything and then we would add the JTextField and the JButton to the JFrame.

Some of the components are:

Events

How do we make things happen?

  • In graphical user interfaces things happen and change because of events.
  • During this example we will create a button click event which means a message will be sent internally to say that a specific button has been clicked.
  • For our simple example, we want some text to be printed out to the console once a button has been clicked.
  • In order for this to happen, we need create an event generator and register a listener for the button in question.

Listeners

How do we make things happen?

  • Listeners are used to allow our program to "listen" for certain events in our program, e.g. when a button has been clicked.
  • The job of the listener is to wait for an event to happen, and once it has happened, to cause an event to happen.
  • For our example, we said that when a button is clicked an event will be "triggered". We will need to register a listener to wait and listen for this event to happen and then it will do what we tell it to do!

Making a Jar File

When developing an application we often need a method to package our software to give it to the users of our software.

A typical extension to files that we have always seen when working in a Windows environment is the .exe file extension. These files are a single binary executable file that when double clicked will open the program for the user.

In the Java world however things are a little different. There is no native .exe file in Java and we must use the .jar file extension instead. A jar file works exactly the same as a .exe file and when double clicked will run the program.

Jar files come in two different varieties:

  • Simple archive folder
  • Runnable archive folder

A jar file is an acronym for the title Java Archive, which is a filetype similar to that of the .zip extension. If we want, we can open up a jar file using a program such as WinZip or WinRar and check the contents inside of the jar.

The second variant of this is the ​runnable archive folder​. This type of file is exactly the same as the archive folder, just with one additional file. This extra file is used to define what happens when the user double clicks on the icon. In our case, we will point to the main method in our class file that we want to run. This will qick start the program to run when it is double clicked.