Mostrar Mensajes

Esta sección te permite ver todos los posts escritos por este usuario. Ten en cuenta que sólo puedes ver los posts escritos en zonas a las que tienes acceso en este momento.


Temas - aprendiendo

Páginas: [1]
1
Hola !!

Alguien que me pueda ayudar con la siguiente duda

He creado una ventana en la que debo ingresar  un password, mediante teclado  o bien presionando los botones que he dispuesto para eso, ademas  debo agregar el nombre de usuario... lo logre pero con un problema! si apretó el botón  respectivo al nº1 por ejemplo queda registrado en el jTextField pero al querer apretar otro botón sobre escribe el valor en la posición que ya se había ocupado... por ende a menos que la clave solo tenga un dígito no me resulta como necesito.
les dejo el código a ver si me pueden ayudar a resolverlo :D

Código: [Seleccionar]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Ventana_inicio extends JFrame implements ActionListener{

private JButton  btnn0;
private JButton  btnn1;
private JButton  btnn2;
private JButton  btnn3;
private JButton  btnn4;
private JButton  btnn5;
private JButton  btnn6;
private JButton  btnn7;
private JButton  btnn8;
private JButton  btnn9;

private JButton btnLimbiar;
private JButton btnIniciar;
private JButton btnCancelar;

private JLabel jlUsuario;
private JLabel jlContraseña;

private JTextField jtUsuario;
private JPasswordField jpfContraseña;



public Ventana_inicio(){
setSize(1000,700);
setTitle("Sistema de ventas");
initcomponents();

}
private void initcomponents() {
getContentPane().setLayout(null);

btnn1 = new JButton("1");
getContentPane().add(btnn1);
btnn1.setBounds(300, 300, 70, 70);
btnn1.addActionListener(this);

btnn2 = new JButton("2");
getContentPane().add(btnn2);
btnn2.setBounds(370, 300, 70, 70);
btnn2.addActionListener(this);

btnn3 = new JButton("3");
getContentPane().add(btnn3);
btnn3.setBounds(440, 300, 70, 70);
btnn3.addActionListener(this);

btnn4 = new JButton("4");
getContentPane().add(btnn4);
btnn4.setBounds(300, 370, 70, 70);
btnn4.addActionListener(this);

btnn5 = new JButton("5");
getContentPane().add(btnn5);
btnn5.setBounds(370, 370, 70, 70);
btnn5.addActionListener(this);

btnn6 = new JButton("6");
getContentPane().add(btnn6);
btnn6.setBounds(440, 370, 70, 70);
btnn6.addActionListener(this);

btnn7 = new JButton("7");
getContentPane().add(btnn7);
btnn7.setBounds(300, 440, 70, 70);
btnn7.addActionListener(this);

btnn8 = new JButton("8");
getContentPane().add(btnn8);
btnn8.setBounds(370, 440, 70, 70);
btnn8.addActionListener(this);

btnn9 = new JButton("9");
getContentPane().add(btnn9);
btnn9.setBounds(440, 440, 70, 70);
btnn9.addActionListener(this);

btnn0 = new JButton("0");
getContentPane().add(btnn0);
btnn0.setBounds(300, 510, 70, 70);
btnn0.addActionListener(this);

btnLimbiar = new JButton("Limpiar");
getContentPane().add(btnLimbiar);
btnLimbiar.setBounds(370, 510, 140, 70);
btnLimbiar.addActionListener(this);

jlUsuario = new JLabel("Usuario: ");
getContentPane().add(jlUsuario);
jlUsuario.setBounds(600, 300, 100, 20);

jtUsuario = new JTextField("");
getContentPane().add(jtUsuario);
jtUsuario.setBounds(700, 300, 140, 20);

jlContraseña = new JLabel("Contraseña: ");
getContentPane().add(jlContraseña);
jlContraseña.setBounds(600, 340, 100, 20);

jpfContraseña= new JPasswordField("");
getContentPane().add(jpfContraseña);
jpfContraseña.setBounds(700, 340, 140, 20);


btnIniciar = new JButton("Iniciar");
getContentPane().add(btnIniciar);
btnIniciar.setBounds(640, 450, 140, 140);
btnIniciar.addActionListener(this);

btnCancelar = new JButton("Cancelar");
getContentPane().add(btnCancelar);
btnCancelar.setBounds(780, 450, 140, 140);
btnCancelar.addActionListener(this);


}
public static void main(String[] args) {
new Ventana_inicio().setVisible(true);

}



public void actionPerformed(ActionEvent e){

if (e.getSource()==btnn0){
jpfContraseña.setText("0");
}
if (e.getSource()==btnn1){
jpfContraseña.setText("1");
}
if (e.getSource()==btnn2){
jpfContraseña.setText("2");
}
if (e.getSource()==btnn3){
jpfContraseña.setText("3");
}
if (e.getSource()==btnn4){
jpfContraseña.setText("4");
}
if (e.getSource()==btnn5){
jpfContraseña.setText("5");
}
if (e.getSource()==btnn6){
jpfContraseña.setText("6");
}
if (e.getSource()==btnn7){
jpfContraseña.setText("7");
}
if (e.getSource()==btnn8){
jpfContraseña.setText("8");
}
if (e.getSource()==btnn9){
jpfContraseña.setText("9");
}


if (e.getSource()==btnLimbiar){

jpfContraseña.setText("");
jtUsuario.setText("");


}


if(e.getSource()==btnIniciar){

if( jtUsuario.getText().equals("juan") && jpfContraseña.getText().equals("1") ){


Ventana_intermedia ventana = new Ventana_intermedia ();
ventana.setVisible(true);


}
else{


if( !jtUsuario.getText().equals("admin") )
JOptionPane.showMessageDialog(null,"USUARIO INCORRECTO");
else           
if( !jpfContraseña.getText().equals("1") )
JOptionPane.showMessageDialog(null,"PASSWORD INCORRECTO");
}



}

if(e.getSource()==btnCancelar){
System.exit(0);

}

}
}





2
No logro que al elegir un Item dentro de un JComboBox me arroje en este caso una palabra en un JTextField... les dejo el código  que hice aunque en la parte lógica por lo visto esta mal . Gracias y quedo atento a cualquier mensaje de ayuda.

Código: [Seleccionar]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Productos_ extends JFrame implements ActionListener {

private JLabel jlDatos;
private JLabel jlID;
private JLabel jlNombre;
private JLabel jlCategoria;
private JLabel jlStock;
private JLabel jlPrecio_venta;
private JComboBox jcbID_prod;
private JTextField jtfNombre;
private JTextField jtfcategoria;
private JLabel jlnum_Stock;
private JTextField jtfprecio_venta;
private JButton btninicio;

public Productos_(){


setSize(500,500);
setTitle("Productos");
initcomponents();

}



private void initcomponents() {
getContentPane().setLayout(null);


jlDatos = new JLabel("Datos del Producto ");
getContentPane().add(jlDatos);
jlDatos.setBounds(20, 20, 160, 20);

jlID = new JLabel("ID Producto: ");
getContentPane().add( jlID);
jlID.setBounds(20, 50, 100, 20);

String ID[] = { "111-229A","111-002B","111-992C","111-882D","111-772E","111-662F","111-552G","111-222H","111-442J","111-332K", };
jcbID_prod = new JComboBox();
getContentPane().add(jcbID_prod);
jcbID_prod.setBounds(130, 50, 100, 20);
jcbID_prod.addActionListener(this);


for(int i=0;i<=9; i++){
jcbID_prod.addItem (ID[i]);

}

jlNombre = new JLabel("Nombre");
getContentPane().add(jlNombre);
jlNombre.setBounds(20, 80, 100, 20);

jtfNombre= new JTextField();
getContentPane().add(jtfNombre);
jtfNombre.setBounds(130,80, 100, 20);


jlCategoria = new JLabel("Categoria: ");
getContentPane().add(jlCategoria);
jlCategoria.setBounds(20, 110, 100, 20);

jtfcategoria= new JTextField();
getContentPane().add(jtfcategoria);
jtfcategoria.setBounds(130,110, 100, 20);

jlStock = new JLabel("Stock: ");
getContentPane().add(jlStock);
jlStock.setBounds(20, 140, 100, 20);

jlnum_Stock = new JLabel(" ");
getContentPane().add(jlnum_Stock);
jlnum_Stock.setBounds(130, 140, 100, 20);

jlPrecio_venta = new JLabel("Precio Venta: ");
getContentPane().add(jlPrecio_venta);
jlPrecio_venta.setBounds(20, 170, 100, 20);

jtfprecio_venta= new JTextField();
getContentPane().add(jtfprecio_venta);
jtfprecio_venta.setBounds(130, 170, 100, 20);

btninicio = new JButton("INICIO");
getContentPane().add(btninicio );
btninicio .setBounds(20, 220, 150, 70);
btninicio .addActionListener(this);
}

public static void main(String[] args) {
new Productos_().setVisible(true);

}
public void actioPerformed(ActionEvent e ){

String ID = (jcbID_prod.getSelectedItem().toString());

if (e.getSource()== "111-229A"){
jtfNombre.setText("Manzana");


}
if( e.getSource()== "111-002B"){
jtfNombre.setText("Pera");


}
if( e.getSource()=="111-992C"){
jtfNombre.setText("Naranja");


}
if( e.getSource()== "111-882D"){

jtfNombre.setText("Mandarina");


}
if( e.getSource()== "111-772E"){
jtfNombre.setText("Platano");


}
if( e.getSource()== "111-662F"){
jtfNombre.setText("Sandia");


}
if( e.getSource()== "111-552G"){
jtfNombre.setText("Melon");


}
if( e.getSource()== "111-222H"){
jtfNombre.setText("Granada");


}
if( e.getSource()== "111-442J"){
jtfNombre.setText("Pomelo");


}
if(e.getSource()== "111-332K"){
jtfNombre.setText("Papaya");


}

}
}

Páginas: [1]

Sobre la educación, sólo puedo decir que es el tema más importante en el que nosotros, como pueblo, debemos involucrarnos.

Abraham Lincoln (1808-1865) Presidente estadounidense.

aprenderaprogramar.com: Desde 2006 comprometidos con la didáctica y divulgación de la programación

Preguntas y respuestas

¿Cómo establecer o cambiar la imagen asociada (avatar) de usuario?
  1. Inicia sesión con tu nombre de usuario y contraseña.
  2. Pulsa en perfil --> perfil del foro
  3. Elige la imagen personalizada que quieras usar. Puedes escogerla de una galería de imágenes o subirla desde tu ordenador.
  4. En la parte final de la página pulsa el botón "cambiar perfil".