Cargar imagen de archivo
Hola, necesito cargar una foto de mis imagenes, pero no se como hacerlo, mediante una interfaz el usuario debe presionar un boton de "Buscar Imagen" y le debe aparecer la carpeta mis imagenes, para que seleccione la imagen que quiera, despues debo serializarla y enviarla a un servidor, porfavor si alguien me puede ayudar, este programa es solo para hacer la interfaz, despues la debo hacer las cosas de serializar.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class Action_listener_matrix extends JFrame implements ActionListener
{
private JLabel texto1, texto2, texto3, texto4, texto5, texto6, texto7, Label1;
private JButton botonIngresar, botonCancelar, botonSalir, botonIngresarFoto, botonConoce, botonNOconoce;
private JPanel panel1, panel2, panel3, panel4, panel5;
private JTextField id, nombre, cantidad;
private JComboBox total;
public Action_listener_matrix()
{
super ("Sistema de Ventas");
Container contenedor = getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
texto1 = new JLabel("Registro de Ventas"); texto1.setVisible(true);
texto2 = new JLabel("ID"); texto2.setVisible(false);
texto3 = new JLabel("Nombre"); texto3.setVisible(false);
texto4 = new JLabel("Cantidad"); texto4.setVisible(false);
texto6 = new JLabel("¿Conoce el Producto a ingresar? "); texto6.setVisible(true);
botonIngresar = new JButton( "Ingresar" ); botonIngresar.addActionListener(this); botonIngresar.setVisible(false);
botonConoce = new JButton( "Si" ); botonConoce.addActionListener(this); botonConoce.setVisible(true);
botonNOconoce = new JButton( "No" ); botonNOconoce.addActionListener(this); botonNOconoce.setVisible(true);
botonCancelar = new JButton( "Cancelar" ); botonCancelar.addActionListener(this); botonCancelar.setVisible(false);
botonSalir = new JButton( "Salir" ); botonSalir.addActionListener(this); botonSalir.setVisible(true);
botonIngresarFoto = new JButton( "Buscar Foto" ); botonIngresarFoto.addActionListener(this); botonIngresarFoto.setVisible(false);
id = new JTextField(3); id.setVisible(false);
nombre = new JTextField(10); nombre.setVisible(false);
cantidad = new JTextField(4); cantidad.setVisible(false);
panel1.add(texto1);
panel3.add(texto6);
panel2.add(texto2); panel2.add(id);
panel3.add(texto3); panel3.add(nombre);
panel4.add(texto4); panel4.add(cantidad);
panel5.add(botonConoce);
panel5.add(botonNOconoce);
panel5.add(botonIngresar);
panel3.add(botonIngresarFoto);
panel5.add(botonCancelar);
panel5.add(botonSalir);
contenedor.add(panel1,BorderLayout.NORTH);
contenedor.add(panel2,BorderLayout.WEST);
contenedor.add(panel3,BorderLayout.CENTER);
contenedor.add(panel4,BorderLayout.EAST);
contenedor.add(panel5,BorderLayout.SOUTH);
setSize( 400, 200 );
setResizable(false);
setLocationRelativeTo(null);
setVisible( true );
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if(source == botonIngresar)
{
}
if(source == botonIngresarFoto)
{
//ACA NECESITO CARGAR UNA FOTO DE MIS IMAGENES PERO NO SE, POR FAVOR, AYUDA
}
if(source == botonConoce)
{
texto1.setVisible(true);
texto2.setVisible(true);
texto3.setVisible(true);
texto4.setVisible(true);
texto6.setVisible(false);
id.setVisible(true);
nombre.setVisible(true);
cantidad.setVisible(true);
botonIngresar.setVisible(true);
botonConoce.setVisible(false);
botonNOconoce.setVisible(false);
botonCancelar.setVisible(true);
botonSalir.setVisible(true);
botonIngresarFoto.setVisible(false);
}
if(source == botonNOconoce)
{
texto1.setVisible(true);
texto2.setVisible(false);
texto3.setVisible(false);
texto4.setVisible(true);
texto6.setVisible(false);
id.setVisible(false);
nombre.setVisible(false);
cantidad.setVisible(true);
botonIngresar.setVisible(true);
botonConoce.setVisible(false);
botonNOconoce.setVisible(false);
botonCancelar.setVisible(true);
botonSalir.setVisible(true);
botonIngresarFoto.setVisible(true);
}
if(source == botonSalir)
{
System.exit(-1);
}
}
public static void main( String args[] )
{
Action_listener_matrix aplicacion = new Action_listener_matrix();
aplicacion.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class Action_listener_matrix extends JFrame implements ActionListener
{
private JLabel texto1, texto2, texto3, texto4, texto5, texto6, texto7, Label1;
private JButton botonIngresar, botonCancelar, botonSalir, botonIngresarFoto, botonConoce, botonNOconoce;
private JPanel panel1, panel2, panel3, panel4, panel5;
private JTextField id, nombre, cantidad;
private JComboBox total;
public Action_listener_matrix()
{
super ("Sistema de Ventas");
Container contenedor = getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
texto1 = new JLabel("Registro de Ventas"); texto1.setVisible(true);
texto2 = new JLabel("ID"); texto2.setVisible(false);
texto3 = new JLabel("Nombre"); texto3.setVisible(false);
texto4 = new JLabel("Cantidad"); texto4.setVisible(false);
texto6 = new JLabel("¿Conoce el Producto a ingresar? "); texto6.setVisible(true);
botonIngresar = new JButton( "Ingresar" ); botonIngresar.addActionListener(this); botonIngresar.setVisible(false);
botonConoce = new JButton( "Si" ); botonConoce.addActionListener(this); botonConoce.setVisible(true);
botonNOconoce = new JButton( "No" ); botonNOconoce.addActionListener(this); botonNOconoce.setVisible(true);
botonCancelar = new JButton( "Cancelar" ); botonCancelar.addActionListener(this); botonCancelar.setVisible(false);
botonSalir = new JButton( "Salir" ); botonSalir.addActionListener(this); botonSalir.setVisible(true);
botonIngresarFoto = new JButton( "Buscar Foto" ); botonIngresarFoto.addActionListener(this); botonIngresarFoto.setVisible(false);
id = new JTextField(3); id.setVisible(false);
nombre = new JTextField(10); nombre.setVisible(false);
cantidad = new JTextField(4); cantidad.setVisible(false);
panel1.add(texto1);
panel3.add(texto6);
panel2.add(texto2); panel2.add(id);
panel3.add(texto3); panel3.add(nombre);
panel4.add(texto4); panel4.add(cantidad);
panel5.add(botonConoce);
panel5.add(botonNOconoce);
panel5.add(botonIngresar);
panel3.add(botonIngresarFoto);
panel5.add(botonCancelar);
panel5.add(botonSalir);
contenedor.add(panel1,BorderLayout.NORTH);
contenedor.add(panel2,BorderLayout.WEST);
contenedor.add(panel3,BorderLayout.CENTER);
contenedor.add(panel4,BorderLayout.EAST);
contenedor.add(panel5,BorderLayout.SOUTH);
setSize( 400, 200 );
setResizable(false);
setLocationRelativeTo(null);
setVisible( true );
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if(source == botonIngresar)
{
}
if(source == botonIngresarFoto)
{
//ACA NECESITO CARGAR UNA FOTO DE MIS IMAGENES PERO NO SE, POR FAVOR, AYUDA
}
if(source == botonConoce)
{
texto1.setVisible(true);
texto2.setVisible(true);
texto3.setVisible(true);
texto4.setVisible(true);
texto6.setVisible(false);
id.setVisible(true);
nombre.setVisible(true);
cantidad.setVisible(true);
botonIngresar.setVisible(true);
botonConoce.setVisible(false);
botonNOconoce.setVisible(false);
botonCancelar.setVisible(true);
botonSalir.setVisible(true);
botonIngresarFoto.setVisible(false);
}
if(source == botonNOconoce)
{
texto1.setVisible(true);
texto2.setVisible(false);
texto3.setVisible(false);
texto4.setVisible(true);
texto6.setVisible(false);
id.setVisible(false);
nombre.setVisible(false);
cantidad.setVisible(true);
botonIngresar.setVisible(true);
botonConoce.setVisible(false);
botonNOconoce.setVisible(false);
botonCancelar.setVisible(true);
botonSalir.setVisible(true);
botonIngresarFoto.setVisible(true);
}
if(source == botonSalir)
{
System.exit(-1);
}
}
public static void main( String args[] )
{
Action_listener_matrix aplicacion = new Action_listener_matrix();
aplicacion.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
if(source == botonIngresarFoto)
{
FileChooser fs = new FileChooser();
fs.showOpenDialog();
//Ahora, en el objeto fs tienes la ruta completa del archivo que ha seleccionado el usuario, ahora deberias hacer comprobaciones de si es una imagen y todo lo demas. No recuerdo bien si esos son los nombres de los metodos porque lo he escrito de cabeza, pero es algo asi.
}
{
FileChooser fs = new FileChooser();
fs.showOpenDialog();
//Ahora, en el objeto fs tienes la ruta completa del archivo que ha seleccionado el usuario, ahora deberias hacer comprobaciones de si es una imagen y todo lo demas. No recuerdo bien si esos son los nombres de los metodos porque lo he escrito de cabeza, pero es algo asi.
}