Difference between revisions of "Java"

From Sinfronteras
Jump to: navigation, search
(Creación de un proyecto Java en Eclipse=)
(Tag: New redirect)
 
(306 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Instalación de Java==
+
#REDIRECT [[Object-Oriented Concepts and Constructs]]
https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-16-04
 
 
 
Puede ser necesario la instalación de '''Oracle JDK'''. Para GUI Programming, por ejemplo, se necesitan librerías que se encuentran en Oracle JDK.
 
 
 
==Instalación de Eclipse==
 
Para estar seguro de que estamos instalando la última versión, es apropiado descargar el paquete desde la página oficial, y no instalar la versión que se encuentra en los repositorios de Ubuntu a través de apt-get (pues estar versiones generalmente están desactualizadas).
 
 
 
Página oficial de Eclipse: http://www.eclipse.org/downloads/
 
 
 
En la siguiente página se explica como instalar eclipse en Ubuntu 16.04:
 
http://ubuntuhandbook.org/index.php/2016/01/how-to-install-the-latest-eclipse-in-ubuntu-16-04-15-10/
 
 
 
Para instalarlo hay que simplemente ejecutar el instalador y seguir las instrucciones (como si se tratara de una instalación en Windows).
 
 
 
Luego, para agregar el ejecutable a la lista de comandos, he agregado la siguiente línea en el .bashrc:
 
PATH=$PATH:/home/adelo/.archivos_programas-ubuntu/eclipse/eclipse:
 
 
 
==Connecting to a MySQL database using Java==
 
 
 
===Connector/J===
 
We will need one additional library to allow us to make a connection from Java to the MySQL database. Java does not come with this library by default so it is important that we include this ourselves.
 
 
 
Para descargar Connector/J: https://dev.mysql.com/downloads/connector/j/
 
 
 
You will then be brought to a page where you will be asked if you would like to signup or login. This step is not needed, at the end of the page you will see a link that says "No thanks, just start my download". Click this link to skip ahead and begin the download.
 
 
 
Inside this folder you will see lots of different files, but we only need one file from this folder. The file we are interested in is the '''mysql-connector-java.bin.jar''' file. This is a single jar file that contains all the compiled source code we need to connect to a database.
 
 
 
===Creating an Eclipse Project to use Connector/J===
 
Before we make a connection to the database we first must create a new Eclipse project.
 
 
 
===Creación de un proyecto Java en Eclipse===
 
* File > New > Java Project: enter Project name
 
** Esto creará una carpeta con el nombre del proyecto y dentro de esta la carpeta '''srs'''
 
** ...
 
 
 
===Agregar una librería externa a nuestro proyecto===
 
'''En esta caso: Making a reference to Connector/J:'''
 
* Now that we have a new project, we can begin the process of adding the new library into the eclipse project. Right click on the project name and click "Properties". This will open a window showing you all of the project specific settings for the application you are developing.
 
* Inside the properties dialog that opens, click on the Java Build Path option.
 
* Once you have opened the Java Build Path option, next click the Libraries tab. This will show you a list of additional libraries you have already referenced. But because this is a new project, you will not see any libraries yet!
 
* On the right hand side of this window click "Add External Jar" and a dialog window will open. Use this window to locate the mysql-connector.bin.jar file that you extracted earlier.
 
* After you have found the jar file, you will see a new entry will be made under the libraries tab. This is telling you that when you go to compile the software you are building, it will also look for the .jar file you have just pointed to.
 
* Click Ok to finish this step.
 
* When you have finished making a reference to the library you will see that inside of your project under the Referenced Libraries menu, there will be a reference to the JAR file that you just pointed to in the previous step. You are now ready to start adding some code into your project to connect to the database.
 
 
 
===Creación de una clase Java en Eclipse===
 
Luego de haber creado el proyecto Java:
 
* src folder (clic d) > New > Class: enter name and click the checkbox beside "public static void main" to generate this for you.
 
 
 
En este caso vamos a crear la clase '''Register.java'', la cual permite ingresar datos a una base de datos MySQL utilizando la librería [[Connector/J]].
 
 
 
 
 
==GUI Programming==
 
'''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:
 
* JFrame
 
* JButton
 
* JTextField
 
* JTextArea
 
* JCheckBox
 
* JComboBox
 
* JList
 
* JRadioButon
 
* JTable
 
* JTree
 
* JLabel
 
* JDialog
 
* List of Swing components and how to use them: http://docs.oracle.com/javase/tutorial/uiswing/components/componentlist.html
 
 
 
===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.
 

Latest revision as of 21:06, 23 January 2020