Bueno aqui les dejo el primer programa que hice.
El tema del programa es sencillo: Es una pregunta de destreza, al contestarla bien el panel se cambia a color verde, de lo contrario, se pone de color rojo.




Aqui les dejo el codigo:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Pregunta implements ActionListener{
public void actionPerformed(ActionEvent e){
JPanel p= new JPanel();
String cmd= e.getActionCommand();
if(cmd.equals("RA")){
System.out.print("Respuesta incorrecta\n");
this.miPanel.setBackground(Color.RED);
this.miTexto.setText("Incorrecto, Vuelve a Intentarlo");
}
if(cmd.equals("RB")){
System.out.print("Respuesta Correcta felicidades!!\n");
this.miPanel.setBackground(Color.GREEN);
this.miTexto.setText("Correcto, Felicidades!!");
}
if(cmd.equals("RC")){
System.out.print("Respuesta Incorrecta\n");
this.miPanel.setBackground(Color.RED);
this.miTexto.setText("Incorrecto, Vuelve a Intentarlo");
}
if(cmd.equals("RD")){
System.out.print("Respuesta Incorrecta\n");
this.miPanel.setBackground(Color.RED);
this.miTexto.setText("
Incorrecto, Vuelve a intentarlo");
}
if(cmd.equals("Sal")){
System.exit(1);
}
}
private JLabel miTexto;
private JPanel miPanel;
public Pregunta(JPanel p, JLabel l){
this.miPanel=p;
this.miTexto=l;
}
public static void main (String[] args){
JFrame f = new JFrame();
f.setSize(500,350);
f.setLocation(100, 200);
f.setTitle("Pregunta");
JPanel pFlow = new JPanel();
JPanel p=new JPanel();
pFlow.setLayout(new FlowLayout());
JLabel aviso= new JLabel();
Pregunta escucha= new Pregunta(p, aviso);
pFlow.setBackground(Color.WHITE);
pFlow.add(new JLabel("Intrucciones: Responde correctamente la pregunta formulada,
si no esta bien respondida el cuadro se iluminara de color rojo,
de lo contrario de color verde.
Un hombre quería entrar a su trabajo pero olvido su NIP\n
sin embargo se acordaba de ciertas pistas para recordarlo.\n
5 Pistas.\n
*El quinto numero mas el tercero equivalen a 14\n
*El cuarto numero es uno mas que el segundo numero\n
*El primer numero es uno menos que dos veces el segundo numero\n
*El segundo numero mas el tercer numero equivalen a 10\n
*La suma de todos los numeros es 30\n
Cual era su NIP?"));
JButton A= new JButton("76458");
A.addActionListener(escucha);
A.setActionCommand("RA");
JButton B= new JButton("74658");
B.addActionListener(escucha);
B.setActionCommand("RB");
JButton C= new JButton("67458");
C.addActionListener(escucha);
C.setActionCommand("RC");
JButton D= new JButton("65748");
D.addActionListener(escucha);
D.setActionCommand("RD");
JButton salir = new JButton("salir");
salir.addActionListener(escucha);
salir.setActionCommand("Sal");
p.add(new JLabel("Si te gusto el juego, Visita mi blogspot
http://thavoch.blogspot.com :)
"));
p.add(pFlow);
p.add(A);
p.add(B);
p.add(C);
p.add(D);
p.add(salir);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
f.setVisible(true);
return;
}
}
1 comentarios:
Esta muy bien hecho, pero tengo una duda
¿Como se hace la parte de poner texto y pasar a otro renglon sin que te marque error?
Si puedes me la contestas
Publicar un comentario