Difference between revisions of "Java"

From Sinfronteras
Jump to: navigation, search
(GUI Programming)
(Introduction to Swing components)
Line 84: Line 84:
 
** JButton
 
** JButton
 
** JTextField
 
** JTextField
 +
** JMenuBar
 +
*** JMenu
 +
*** JMenuItem
 +
** JPanel
 
** JTextArea
 
** JTextArea
 
** JCheckBox
 
** JCheckBox

Revision as of 23:53, 14 November 2017

Path de los códigos:

Variable definida con:
{{#vardefine:codespath|/home/adelo/1-disco_local/.archivos_programas/eclipse-workspace/}}

Para usar la variable:
{{#var:codespath}}

Instalación de Java

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.

Eclipse

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:

Combinaciones de teclas

  • Para que coloque los import (luego de haber ingresado un comando que necesite una librería en particular): Ctrl Shif o
  • Run: Ctrl 11
  • Comentar: Ctrl 7
  • Rastrea la variable seleccionada: CTRL k

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.

GUI Programming

GUI stands for Graphical User Interface. The use of pictures rather than just words to represent the input and output of a program.

A Bit of History:

  • Windowing systems started with the first real-time graphic display systems for computers, namely the SAGE Project and Ivan Sutherland's Sketchpad (1963).

... Se habla de esto en 3-GUI Programming Lecture 1 Introduction.pdf, no creo que sea importante...


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.

  • 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.


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.


Introduction to Swing components

The JFrame can be considered to be base to which every other component is added. The JFrame is basically the entire window before anything has actually been 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:

List of Swing components and how to use them: http://docs.oracle.com/javase/tutorial/uiswing/components/componentlist.html

  • JFrame
    • JButton
    • JTextField
    • JMenuBar
      • JMenu
      • JMenuItem
    • JPanel
    • JTextArea
    • JCheckBox
    • JComboBox
    • JList
    • JRadioButon
    • JTable
    • JTree
    • JLabel
    • JDialog

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!

Example 1

In the example below, this is a very simple GUI using Java. The most important step here is extending the JFrame. This turns our class into a JFrame that we can see.

En un sentido práctico, el JFrame representa nuestra primera ventana. Dentro de ésta podremos agregar todas las ventanas y opciones que queramos.

/home/adelo/1-disco_local/.archivos_programas/eclipse-workspace/GUITest/src/GUIaFirstExample.java
import javax.swing.JFrame;


public class GUIaFirstExample extends JFrame{
	
	public GUIaFirstExample(){
		this.setSize(500,500);
		this.setVisible(true);
	}
	
	public static void main(String[] args){
		// TODO Auto-generated method stub
		new GUIaFirstExample();
	}
}


En el siguiente código hemos ajustado el Layout del Frame. El Layout define la organización de los elementos que serán incorporados al Frame. En este primer ejemplo configurado un FlowLayout en nuestro JFrame. The FlowLayout is a very simple layout manager that simply places each component in a line from left to right on the JFrame.

/home/adelo/1-disco_local/.archivos_programas/eclipse-workspace/GUITest/src/GUIbSecondExample.java
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.FlowLayout;


public class GUIbSecondExample extends JFrame {
	
	public GUIbSecondExample() {
		this.setSize(500,500);
		this.setVisible(true);
		this.setLayout(new FlowLayout());               // Set the layout of the JFrame
		
		JButton test = new JButton( "Sample Text" );    // Making the button
		
		this.add(test);                                 // Adding the button to the JFrame
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GUIbSecondExample();
		
	}
	
}


Although we have buttons added to the JFrame, if we want to respond to clicks, we must add the implements ActionListener.

public void actionPerformed(). This is the method that will be used to handle click events that are fired

Refreshing the page: Whenever we add new buttons or components to a JFrame, when the program is run sometimes it will not be updated and show the correct GUI. To ensure that after we added components to a JFrame that we always see the correct output, we can call the validate() and repaint() method after the components have been added. This tells the JFrame to refresh and ensure that everything on the screen is correct.

/home/adelo/1-disco_local/.archivos_programas/eclipse-workspace/GUITest/src/GUIcActionListener.java
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class GUIcActionListener extends JFrame implements ActionListener {
	
	public GUIcActionListener() {
		this.setSize(500,500);
		this.setVisible(true);
		this.setLayout(new FlowLayout());
		
		JButton test = new JButton("Sample Text");    // Making the button
		
		test.addActionListener(this);                 // Turn the listening on
		test.setActionCommand("test");                // Give it an ID
		
		this.add(test);                               // Adding the button to the JFrame
		
		// This tells the JFrame to refresh after we added components (ver nota arriba):
		validate();
		repaint();
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GUIcActionListener();
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("test button clicked!");
		
	}
	
}


JMenuBa - JMenu: In Java, the “menu” across the top of the program is called a JMenuBar.

/home/adelo/1-disco_local/.archivos_programas/eclipse-workspace/GUITest/src/GUIeJMenuBar.java
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class GUIeJMenuBar extends JFrame{
      
      public GUIeJMenuBar() {
            
            this.setVisible(true);
            this.setTitle("Notepad");
            this.setSize(500,500);
            
            
            // Make the bar for the menu
            JMenuBar bar = new JMenuBar();
            this.setJMenuBar(bar);
            
            
            // First JMenu
            JMenu file = new JMenu("File");
            bar.add(file);
            
            JMenuItem open = new JMenuItem("Open");
            file.add(open);
            
            JMenuItem close = new JMenuItem("Close");
            file.add(close);
            
            
            // Second JMenu
            JMenu about = new JMenu("About");
            bar.add(about);
            
            JMenuItem aboutus = new JMenuItem("About us");
            about.add(aboutus);
            
            JMenuItem moreinfo = new JMenuItem("More info");
            about.add(moreinfo);
            
            
            repaint();
            
      }
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            new GUIeJMenuBar();
      }
      
}

Connecting to a MySQL database using Java

La conección a la base de datos MySQL la haremos a través de Connector/J. Esta librería no se encuentra por defecto en las librerías de Java y debemos por lo tanto agregarla a nuestro proyecto Java.

Entonces, para realizar la conección debemos:

  1. Crear la base de datos MySQL a la cual queremos conectarnos
  2. Create an Eclipse Java project
  3. Agregar la librería externa Connector/J a nuestro proyecto
  4. Creación de la Clase Java en donde se hará la conexión

Creación de la base de datos MySQL

Luego de haber instalado y configurado MySQL (Instalación de MySQL) debemos crear la base de datos a la cual queremos conectarnos con Java. Para más detalles ver Database#MySQL

En este caso vamos a crear la base de datos "graham" y la tabla "samplelogin".

CREATE DATABASE graham;

USE graham;

CREATE TABLE samplelogin
  (
  id	 	INT(10)		NOT NULL AUTO_INCREMENT,
  username 	VARCHAR(15)	NOT NULL,
  password 	VARCHAR(15)	NOT NULL,
  PRIMARY KEY(id)
  );
INSERT INTO samplelogin VALUES('001','Adelo Vieira','a2930');
INSERT INTO samplelogin VALUES('002','Rachel Smith','r1039');

Creating an Eclipse Java 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 ésta la carpeta srs
    • ...

Agregar la librería externa Connector/J a nuestro proyecto

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.

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 la Clase Java en donde se hará la conexión

Luego de haber creado el proyecto Java, debemos crear una clase de la siguiente forma:

  • src folder (clic d) > New > Class: enter name and Click OK!

La conexión con la base de datos la haremos a través de DriverManager.getConnection, método que se encuentra en la Librería Connector/J.

MyDBConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;

public class MyDBConnection{
	
	public MyDBConnection() throws SQLException{
		
		// La conexi'on con la base de datos se hace a trav'es de esta orden:
           	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/graham?"
    					             	       +"user=root&password=a1640774200");
		
		// Luego de establecer la conexi'on podemos realizar un query sobre la base de datos:
        	Statement stmt = conn.createStatement();
        	ResultSet rs = stmt.executeQuery("select * from samplelogin;");
			
	}

}

En el ejemplo anterior hemos realizado la conexión con la base de datos SQL «graham» y hemos almacenado el resultado de la query en la variable rs. Sin embago, no hemos desplegado el resultado de la query ni hemos hecho nada útil con ella. A continuación se muestran algunas clases que conectan con la base de datos y muestran ejemplos más elaborados.

Creación de la clase ShowingData.java

ShowingData.java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


public class ShowingData extends JFrame  {
	
    public ShowingData(){
    
    	setSize(500,500);
    	setVisible(true);
    	
    	Connection conn = null;
    	Statement stmt = null;
    	ResultSet rs = null;
    	
    	try {
    	    // La conexi'on con la base de datos se hace a trav'es de esta orden:
    	    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/graham?"
    	    				        +"user=root&password=a1640774200");

    	    // Luego de establecer la conexi'on podemos realizar un query sobre la base de datos:
    	    stmt = conn.createStatement();
    	    rs = stmt.executeQuery("select * from samplelogin;");

    	    // El reultado del query se encuentra en el objeto "rs" creado arriba.
    	    // Ahora queremos desplegar la data almacenada en "rs".
    	    // En este ejemplo vamos a desplegar la data en un JTable.
    	    
    	    // Lo primero que hay que hacer es crear una variable de tipo String (una matriz en este caso)
    	    // El n'umero de columnas debe, por supuesto, ser igual al n'umero de columnas de la tabla
    	    // almacenada en "rs". El n'umero de filas debe ser igual o mayor al n'umero de filas de la tabla.
     	    String[][] data = new String[100][3];
     	    
     	    // Ahora vamos a ingresar la data que se encuentra en el objeto "rs" a la matriz "data" creada arriba.
     	    // Para esto vamos a realizar un ciclo que va a recorrer el objeto "rs" e ingresar los valores a "data"
    	    int counter = 0;
    	    while(rs.next()){
    	    	String id = rs.getString("id");      
    	       
    	    	data[counter][0] = id;
    	      
    	        String un = rs.getString("username");
    	    	data[counter][1] = un;    	      
    	        
    	        String pw = rs.getString("password");
    	    	data[counter][2] = pw;
    	        
    	        counter = counter+ 1;
    	    }
    	    
    	    // Ahora que ya tenemos la data almacenada en la matriz "data", podemos desplegarla en una JTable.
    	    // S'ólo debemos antes crear un vector String que contenga el nombre de las columnas:
    	    String[] colNames = {"id" , "username" , "password"};
    	    
    	    // Ahora s'i podemos desplegar la data correctamente en el JTable:
    	    JTable table = new JTable(data , colNames);
    	    
    	    // Tenemos tambi'en que agregamos un JScrollPane a nuestra tabla para poder desplazarnos en ella:
    	    JScrollPane sr = new JScrollPane(table);
    	    
    	    // Por 'ultimo, agregamos nuestra Jtable al JFrame:
    	    this.add(sr);
    	        
    	} 
    	
    	// Handle any errors:
    	catch (SQLException ex) {
    	    System.out.println("SQLException: " + ex.getMessage());
    	    System.out.println("SQLState: " + ex.getSQLState());
    	    System.out.println("VendorError: " + ex.getErrorCode());
    	}
    	
    	
    }
    
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			new ShowingData();
	}

}

Creación de la clase Register.java

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.

Register.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class Register extends JFrame implements ActionListener{
	
	// make the fields global so we can see them!!
	JTextField username = null;
	JTextField password = null;
	
	public Register(){		

		setSize(400,400);
		setVisible(true);
		this.setLayout(new GridLayout(3,1));

		JLabel un = new JLabel("Username:");
		this.add(un);
		username = new JTextField(20);
		this.add(username);
		
		JLabel pw = new JLabel("Password:");
		this.add(pw);
		password = new JTextField(20);
		this.add(password);
		
		JButton register = new JButton("Register");
		
		register.addActionListener(this);
		
		this.add(register);
		
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Register();
	}
	
	public void registerNewUser(){
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		}
		
		catch(Exception e ){}
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
			// En esta linea debemos ajustar: 
			// El IP (127.0.0.1 si es localhost) y el Port (generalmente el 3306) en donde corre MySQL.
			// El nombre de la base de datos a la cual queremos conectarnos: graham en nuestro ej.
			// El usuario y la clave de MySQL.
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/graham?user=root&password=a1640774200");
	    	
			// Do something with the Connection
			stmt = conn.createStatement();
			
			// or alternatively, if you don't know ahead of time that
			// the query will be a SELECT...
			String un = username.getText();
			String pw = password.getText();
			
			// En esta linea debemos ajustar el nombre de la base de datos y la tabla
			// en donde queremos agragar datos. En nuestro ejemplo: graham, samplelogin.
			if (stmt.execute("INSERT INTO `graham`.`samplelogin` (`username`, `password`) VALUES ('"+un+"', '"+pw+"');")) {
			}
		}
		
		// loop over results
		catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		registerNewUser();
	}
}

Threads

Threads are used to allow our program to do more than one thing at any one time.

We picture a process which has two tasks that aren’t necessarily dependent on each other. They can occur at the same time, each handled by a “thread”

Thread.jpg

So, a process (think program) can contain a number of different threads. As the program is running, the processor switches between each of the threads allowing each of them to have some CPU time to run.

For dual (or more) core machines, each thread can be executed on a separate core, so no switching is neceessary. The threads truly run concurrently.

In the previous picture you can also see that Thread number 2 has died off and thread 1 continued on. This is ok.

Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.

Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files.

Every application has at least one thread — or several, if you count "system" threads that do things like memory management and signal handling. From the programmer’s point of view, you start with just one thread, called the main thread. This thread has the ability to create additional threads

The difficulty with threads

  • Threads can be difficult to work with because their behavior can be unpredictable.
  • The order in which they are running may change unless synchronized.

Some methods available in the Thread class

// Changes the name of this thread to be equal to the argument name.
setName(String name) 

// Changes the priority of this thread. Valid values are 1 through 10.
setPriority(int newPriority)

// Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds
sleep(long millis)

// Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
start()

// Causes the current thread to block (pause) until the specified thread terminates.
Thread.join()

// Causes the current thread to block until the specified thread terminates or the specified number of milliseconds passes
Thread.join(long millis)

Creating Threads

When we want to create threads, two options are available to us:

  • Extend the thread class, or
  • We can implement the Runnable interface

Extend the thread class: By doing like this we have access to all the methods in the thread class.

We need to extend the thread class and add a run method:

MyThread.java:
public class MyThread extends Thread {

	public void run(){
		
		// This is the code that is executed when the thread is started
		// Este es s'olo un c'odigo ejemplo. Aqu'i podr'ia haber cualquier c'odigo
		for (int i=0 ; i < 100000000 ; i++){
			System.out.println(this.getName() + ": " + i);
			
		}
		
	}
	
}


Implement the Runnable interface:

MyRunnable.java
public class MyRunnable implements Runnable {
	
	public void run(){
		
		for (int i=0 ; i < 100000000 ; i++){
			System.out.println("My Tread implementing the Runnable interface: " + i);
			
		}
		
	}
	
}

Then, in our standard class, when we want to use our thread, we just need to create an instance of the thread in case we extended the thread class, then start it.

If we implemented the Runnable interface, we need to create a Thread object and pass the Runnable Tread object we created to it, and then start it.

MyProgram.java:
import javax.swing.JFrame;

public class MyProgram extends JFrame{
	
	public MyProgram(){

		setSize(500,500);
		setVisible(true);
		
//		Object created extending the thread class:
		MyThread t = new MyThread();
		t.setName("t1");
		t.setPriority(10);
		t.start();

//		Object created implementing the runnable interface:		
		MyRunnable r = new MyRunnable();
		Thread tr = new Thread(r);
		tr.start();
		
	}
	public static void main(String[] args) {
		new MyProgram();
	}

}

En el ejemplo anterior hemos creado un JFrame que cuando se abre se ejecuta también el Thread.

Cuando se ejecuta MyProgram.java se debe notar que la ejecución no sigue el orden del código. Es decir, en nuestro ejemplo nuestras Threads sólo imprimen un valores en pangalla. Note que no necesariamente se debe terminar la impresión de todos los valores de la primera Thread en el código antes de comenzar la segunda. Esta característica impredecible de las Threads fue mencionada en The difficulty with threads

A continuación se muestra un ejemplo un poquito más elaborado en el cual la Thread se ejecuta una vez que se hace clic en un botón desplegado en el JFrame. Para esto se implementa el ActionListener

GUIThread.java:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GUIThread extends JFrame implements ActionListener{

	public GUIThread(){
		
		setSize(300,300);
		setVisible(true);
		
		JButton btn = new JButton("Button");
		btn.addActionListener(this);
		this.add(btn);
		
	}
	
	public static void main(String[] args) {
	    // TODO Auto-generated method stub
            new GUIThread();
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("Button");
		
		MyThread x = new MyThread();
		x.start();
			
	}

}

Ejemplos clásicos para comprender el concepto de Thread

Los siguientes problemas fueron abordados por el Prof. para explicar el concepto the Thread.