package ejswing1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/*

1 – Creamos 1 botón, con JButton
2 – Usamos setBounds
3 – lo metemos en el frame con .add
4 – añadimos «extends JFrame implements ActionListener» a la clase principal
5 – Usamos .addActionListener con el botón
6 – Autocompletar cuando proceda

*/

public class EjSwing1 extends JFrame implements ActionListener {

JFrame ventana = new JFrame();

JLabel label1 = new JLabel(«Etiqueta1»);
JLabel label2 = new JLabel(«Etiqueta2»);
JLabel label3 = new JLabel(«Etiqueta3»);

JButton botonsalir = new JButton(«Salir»);
JButton boton2 = new JButton(«Boton2»);
JButton boton3 = new JButton(«Boton3»);

JButton boton4 = new JButton();

public void graficos() {

ventana.setLayout(null);
ventana.setSize(600,600);
ventana.setVisible(true);

label1.setBounds(30, 0, 100, 50);
label2.setBounds(30, 50, 100, 50);
label3.setBounds(30, 100, 100, 50);

botonsalir.setBounds(130, 0, 100, 50);
boton2.setBounds(130, 50, 100, 50);
boton3.setBounds(130, 100, 100, 50);
boton4.setBounds(130, 150, 100, 50);

boton4.setText(«Cambiar»);

ventana.add(label1);
ventana.add(label2);
ventana.add(label3);

ventana.add(botonsalir);
ventana.add(boton2);
ventana.add(boton3);
ventana.add(boton4);

// al escribir «sleep» se me ha autocompletado la siguiente línea
// y también la parte del main «throws Interrumped…»
/*Thread.sleep(3000);
System.exit(0);*/

// después de «implements» y del action perfomed, debemos añadir esto
botonsalir.addActionListener(this);
boton4.addActionListener(this);

}

public static void main(String[] args) {

EjSwing1 nuevo = new EjSwing1();
nuevo.graficos();

}

@Override
public void actionPerformed(ActionEvent ae) {

if (ae.getSource()==botonsalir) {
System.exit(0);
}

if (ae.getSource()==boton4) {
label3.setText(«Nuevo texto»);
}

}

}

Crea un fichero de texto con el nombre y contenido que tú quieras. Luego crea una aplicación que lea este fichero de texto carácter a carácter y muestre su contenido por pantalla sin espacios. Por ejemplo, si un fichero tiene el siguiente texto “Esto es una prueba”, deberá mostrar “Estoesunaprueba”.

public class Archivos1 {

public static void main(String[] args) throws FileNotFoundException, IOException {

File archivo1 = new File(«C:\\Users\\Jorge\\Desktop\\archivotexto.txt»);
// BR 1 redLine()
Reader reader1 = new FileReader(archivo1);
//System.out.println((char)br1.read());
try (BufferedReader br1 = new BufferedReader(reader1)
// read() lee un caracter, pero lo devuelve como entero
/*
int michar= br1.read();
char minuevochar= (char)michar;
*/
) {
//System.out.println((char)br1.read());

int comprobacion = br1.read();

while (comprobacion!=-1) {

// comillas simples para caracteres

if ((char)comprobacion!=’ ‘) {

System.out.print((char)comprobacion);

}

comprobacion = br1.read();

}
} catch (IOException io) {
System.out.println(io);
}

}

}

Un array es un objeto que contiene un número fijo de valores de un solo tipo. La longitud del array se fija tras su creación.

Cada objeto en un array se llama elemento, y se accede a él mediante su índice numértico. Es decir, un elemento de un array tiene un índice (un número entero) y un valor. Hemos de recordar que el primer elemento de un array siemnpre tiene índice 0, con lo que el 4º elemento tiene índice 3…


public static void main(String[] args) {

// declarar un array de enteros
int[] arrayDeEnteros;
// allocates memory for 10 integers
arrayDeEnteros = new int[4];

// initialize elements
arrayDeEnteros[0] = 100;
arrayDeEnteros[1] = 200;
arrayDeEnteros[2] = 300;
arrayDeEnteros[3] = 400;

System.out.println("Element at index 0: "+ arrayDeEnteros[0]);
System.out.println("Element at index 1: "+ arrayDeEnteros[1]);
System.out.println("Element at index 2: "+ arrayDeEnteros[2]);
System.out.println("Element at index 3: "+ arrayDeEnteros[3]);

System.out.println(arrayDeEnteros.length);

byte[] arrayDeBytes;
short[] arrayDeShorts;
long[] arrayDeLongs;
float[] arrayDeFloats;
double[] arrayDeDoubles;
boolean[] arrayDeBooleans;
char[] arrayDeChars;
String[] arrayDeStrings = new String[4];

// bucles con array

//for (int i=0; i<=3; i++) { for (int i=0; i