Código Java (Main.java):
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Main extends JFrame {
public Main() {
String[] columnNames = {"Nombre", "Años", "Apto",};
Object[][] datos = {
{"Juan", 25, false},
{"Sonia", 33, true},
{"Pedro", 42, false}};
DefaultTableModel dtm = new DefaultTableModel(datos, columnNames);
final JTable table = new JTable(dtm);
// Agregar nueva columna
String[] columnaNueva1 = {"vago", "diestro", "normal",};
dtm.addColumn("Tipo", columnaNueva1);
// Agregar nueva fila
Object[] newRow = {"Maria", 55, false};
dtm.addRow(newRow);
// Modificar celda especifica
dtm.setValueAt("XXX", 3, 3); // Row/Col
table.setPreferredScrollableViewportSize(new Dimension(250, 100));
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
Main frame = new Main();
frame.pack();
frame.setVisible(true);
}
}
Resultado:
Codigo:
- public class Main {
- ..public static void main(String[] args) {
- ....new Tarea("Tarea1").start();
- ....new Tarea("Tarea2").start();
- ..}
- }
- class Tarea extends Thread {
- ..public Tarea(String str) {
- ....super(str);
- ..}
- @Override // Quita el aviso del Warning
- ..public void run() {
- ....int tar = 0;
- ....if (getName().equals("Tarea1")) tar = 1;
- ....if (getName().equals("Tarea2")) tar = 2;
- ....switch (tar) {
- ......case 1: // Tarea 1
- ........for (int a = 0; a < 5; a++){
- ..........System.out.println(getName()+": "+a);
- ........}
- ........break;
- ......case 2: // Tarea 2
- ........for (int b = 0; b < 5; b++){
- ..........System.out.println(getName()+": "+b);
- ........}
- ....}
- ..}
- }
Resultado:
- Tarea1: 0
- Tarea1: 1
- Tarea1: 2
- Tarea2: 0
- Tarea2: 1
- Tarea2: 2
- Tarea2: 3
- Tarea2: 4
- Tarea1: 3
- Tarea1: 4
Nota: Las 2 tareas se ejecutan al mismo tiempo, por eso en el resultado puede aparecer mezclados los resultados de la Tarea1 con el de la Tarea2.
Codigo:- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Main extends JFrame {
- ..FlowLayout v = new FlowLayout();
- ..JButton boton = new JButton("Aumentar nivel barra de progreso");
- ..JProgressBar barra = new JProgressBar(0,10); //min,max
- ..int i = 0;
- ..public Main() {
- ....add(boton);
- ....add(barra);
- ....getContentPane().setLayout(v);
- ....setDefaultCloseOperation(EXIT_ON_CLOSE);
- ....setSize(300,150);
- ....setVisible(true);
- // Evento al pulsar boton
- ....boton.addActionListener (new ActionListener(){
- ......public void actionPerformed(ActionEvent e)
- ......{
- ........i = i + 1;
- ........barra.setValue(i);
- ......}
- ....});
- ..}
- ..public static void main(String[] args) { new Main(); }
- }
Resultado:

Codigo:- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Main extends JFrame {
- .FlowLayout v = new FlowLayout();
- .JButton boton1 = new JButton(" Revertir ");
- .TextArea text = new TextArea("", 3, 30, 3);//row,col,nullScroll
- .public Main() {
- ...text.setText("Hola mundo");
- ...add(text);
- ...add(boton1);
- ...getContentPane().setLayout(v);
- ...setDefaultCloseOperation(EXIT_ON_CLOSE);
- ...setSize(300,150);
- ...setVisible(true);
- //Event al pulsar boton
- ...boton1.addActionListener (new ActionListener(){
- ....public void actionPerformed(ActionEvent e){
- .....StringBuffer str1 = new StringBuffer(text.getText());
- ........text.setText(str1.reverse().toString());
- ......}
- ....});
- ..}
- ..public static void main(String[] args) { new Main(); }
- }
Resultado:
Codigo:- public class Main{
- ..public static String texto (String a, String b)
- ..{
- ....return "resultado: " + a + b;
- ..}
- ..public static void main(String[] args)
- ..{
- ....String text1;
- ....text1 = texto("palabra1 ", "palabra2 ");
- ....System.out.println(text1);
- ..}
- }
Resultado:- resultado: palabra1 palabra2
Este código graba audio desde micrófono durante 10 segundos y lo guarda en un archivo de audio llamado "grabacion.wav".
Código Java (GrabarSonido.java):
package grabarsonido;
import java.io.File;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
public class GrabarSonido {
AudioFileFormat.Type aFF_T = AudioFileFormat.Type.WAVE;
AudioFormat aF = new AudioFormat(8000.0F, 16, 1, true, false);
TargetDataLine tD;
File f = new File("grabacion.wav");
public GrabarSonido() {
try {
DataLine.Info dLI = new DataLine.Info(TargetDataLine.class, aF);
tD = (TargetDataLine) AudioSystem.getLine(dLI);
new CapThread().start();
System.out.println("Grabando durante 10s...");
Thread.sleep(10000);
tD.close();
} catch (Exception e) { }
}
class CapThread extends Thread {
@Override
public void run() {
try {
tD.open(aF);
tD.start();
AudioSystem.write(new AudioInputStream(tD), aFF_T, f);
} catch (Exception e) { }
}
}
public static void main(String[] args) {
new GrabarSonido();
}
}
Resultado:
run:
Grabando durante 10s...
BUILD SUCCESSFUL (total time: 0 seconds)
Nota: Antes hay que crear una ruta y añadir el archivo de audio, quedando de la siguiente manera: "c:\sonidos\sonido1.wav"
Código Java:
import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class Sonido {
public static void main(String[] args) {
try {
Clip sonido = AudioSystem.getClip();
File a = new File("c:\\sonidos\\sonido1.wav");
sonido.open(AudioSystem.getAudioInputStream(a));
sonido.start();
System.out.println("Reproduciendo 10s. de sonido...");
Thread.sleep(10000); // 10000 milisegundos (10 segundos)
sonido.close();
} catch (Exception tipoError) {
System.out.println("" + tipoError);
}
}
}
Resultado:
Reproduciendo 10s. de sonido...
Código java:
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
JPanel panel = new JPanel();
ButtonGroup grupo = new ButtonGroup();
JRadioButton op1 = new JRadioButton("Opción 1");
JRadioButton op2 = new JRadioButton("Opción 2");
JRadioButton op3 = new JRadioButton("Opción 3");
// constructor
public Main() {
grupo.add(op1);
grupo.add(op2);
grupo.add(op3);
panel.add(op1);
panel.add(op2);
panel.add(op3);
getContentPane().add(panel);
getContentPane().setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 120);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
Resultado:
Código:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ventana extends JFrame {
JButton boton1 = new JButton("Pulsar aqui para cerrar ventana");
FlowLayout ventana1 = new FlowLayout();
public Main() {
boton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
getContentPane().add(boton1);
getContentPane().setLayout(ventana1);
setSize(250, 150);
setTitle("Titulo ventana");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
Resultado:
Nota: Al pulsar sobre el botón activa un evento, en este caso cierra la aplicación.