Hi guys,
I'm having trouble with my code and I'm kind of new in Java so I don't know what to do. I have to do a program with Buttons and each button has to take me to another class (I don't know if I'm being clear). For example if I click the button "Bingo" my class named Bingo (which I already have) should run but I don't know how to do that.
Here's my code:

import java.awt.*; 
import javax.swing.*; 
import java.io.*;
import java.awt.event.*;

public class GameFrame extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    JFrame tablero=new JFrame();
    JButton Bingo=new JButton("Bingo");
    JButton Adivina=new JButton("Adivina Palabras");
    JButton Black=new JButton("Blackjack");
    JLabel t=new JLabel(" Bienvenido al Casino. Elige lo que quieras jugar:");

    public GameFrame() {
        setTitle("Casino");
        setLayout(new GridLayout(5, 2, 20, 3));
        add(t); 
        add(new JLabel ("")); 
        add(new JLabel (""));
        add(new JLabel ("")); 

        add (Bingo);
        Bingo.addActionListener(this);

        add (Adivina);
        Adivina.addActionListener(this);

        add (Black);
        Black.addActionListener(this);

        pack();
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {   

     if(e.getSource() == Bingo){
          new Bingo();
     }else{
         if(e.getSource() == Adivina){
             new Adivinar();
            }else{
                new Blackjack();
            }
     }



    }
}

Recommended Answers

All 4 Replies

If you look at your actionPerformed method it simply instantiates a new Bingo, Adivina, or Blackjack by calling their no-args constructors when the appropriate button is pressed. All you need is a no-args constructor in each of those classes in which you create the window and make it visible.

Thank you, but what do I do if Blackjack and Bingo do not have a window? They open in the JVM.

what do you mean "do not have a window"?
what is it exactly you are trying to do, and what do those actions do? without knowing the code of those classes, it's pretty difficult to check what's actually going on.

I've alredy solved the Bingo one, but here's my code fot Blackjack

import java.io.*;
public class Blackjack{
    private static BufferedReader stdIn=new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter stdOut=new PrintWriter(System.out,true);

    public void Blackjack() throws IOException{
      double j[][],s[],s2[];
      int x=0,p=0,b=0,m=0,n=0,y=0,ga=0,er=0;
      int ap[],dg=0,pa=0;
      double g=0;
      String r=new String("");

      System.out.print("¿Cuántos jugadores van a ser? ");
      n=Integer.parseInt(stdIn.readLine());

      j=new double[n][3];
      s=new double[n];
      s2=new double[n];
      ap= new int[n];

      do{

        for(y=0;y<n;y++){

            System.out.print("Jugador "+(y+1)+"\n¿Cuánto vas a apostar? ");
            ap[y]=Integer.parseInt(stdIn.readLine());


            for(x=0;x<2;x++){
                j[y][x]=Math.round(Math.random()*13);

                    if((j[y][x]>=2)&&(j[y][x]<=10)){
                        j[y][x]=j[y][x];
                    }else{

                        if(j[y][x]==1){
                            j[y][x]=11;
                        }else{
                            j[y][x]=10; 
                        }

                    }

            }

            s[y]=j[y][0]+j[y][1];

            System.out.println("Tus cartas son: "+j[y][0]+" y "+j[y][1]+".");

            if(s[y]<21){

                 System.out.print("¿Quieres sacar otra carta? ");
                 r=stdIn.readLine();

                 if(!r.equals("no")){

                    j[y][2]=Math.round(Math.random()*13);

                        if((j[y][2]>=2)&&(j[y][2]<=10)){
                               j[y][2]=j[y][2];
                        }else{

                            if(j[y][2]==1){
                                j[y][2]=11;
                            }else{
                                j[y][2]=10; 
                            }

                        }

                    System.out.println("Tu tercer carta fue: "+j[y][2]);    
                    s[y]=s[y]+j[y][2];

                         if(s[y]==21){
                            p=p+1;
                            s2[y]=21-s[y];
                            System.out.println(s2[y]);
                         }else{

                            if((s[y]>=17)&&(s[y]<=20)){
                                b=b+1;
                                s2[y]=21-s[y];
                            }else{

                                if(s[y]>21){
                                    s2[y]=100+s[y];
                                    pa=pa+1;
                                }else{
                                    s2[y]=21-s[y];
                                }

                                m=m+1;
                            }

                         }

                 }else{

                     if((s[y]>=17)&&(s[y]<=20)){
                        b=b+1;
                        s2[y]=21-s[y];
                     }else{

                         if(s[y]>21){
                            s2[y]=100+s[y];
                         }else{
                            s2[y]=21-s[y];
                         }

                        m=m+1;
                     }

                    }


            }else{



              if(s[x]==21){
                p=p+1;
                s2[y]=21-s[y];
              }else{
                s2[y]=100+s[y]; 
                m=m+1;
              }

            }

        }

        g=s2[0];

         for(x=1;x<n;x++){

            if(g<s2[x]){
               g=g;
            }else{
               g=s2[x];
            }

            if(g==s2[x]){
               ga=x;
            }else{
               er=er+1;
            }

            }

         if(er==n){
            ga=0;
         }

         for(x=0;x<n;x++){
             dg=dg+ap[x];
         }

         dg=dg-ap[ga];

         if(s[ga]>100){
             s[ga]=s[ga]-100;
         }else{
             s[ga]=s[ga];
         }

        System.out.println();   
        System.out.println("El ganador fue el jugador "+(ga+1)+" con una suma de "+s[ga]+". \nGanaste $"+dg);



        System.out.print("¿Quieren jugar otra vez? ");
        r=stdIn.readLine();

      }while(!r.equals("no"));

      System.out.println(); 
      System.out.println("Gracias por jugar. \nHubo "+p+" juegos perfectos, "+b+" juegos buenos y "+m+" juegos malos."); 

    }

}

and here's the one of Adivina:

import java.io.*;
import javax.swing.JOptionPane;
public class Adivina{
    private static BufferedReader stdIn=new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter stdOut=new PrintWriter(System.out,true);

    public void Adivina() throws IOException{ 
        int l=0,e=0,ad=0,np=0,x=0;
        double n=0;
        String r=new String(""), p[]=new String [10], car[];
        String c[],pi[][]=new String[10][3],lp[]=new String[20];
        String a=new String("*");

        p[0]="arbol";
        p[1]="mesa";
        p[2]="perro";
        p[3]="gato";
        p[4]="loteria";
        p[5]="serpiente";
        p[6]="computadora";
        p[7]="sillon";
        p[8]="celular";
        p[9]="cortina";

        pi[0][0]="Ser vivo";
        pi[0][1]="Planta";
        pi[0][2]="Alto";
        pi[1][0]="Mueble";
        pi[1][1]="Patas";
        pi[1][2]="Comida";
        pi[2][0]="Ser vivo";
        pi[2][1]="Mamífero";
        pi[2][2]="Mejor amigo del hombre";
        pi[3][0]="Ser vivo";
        pi[3][1]="Felino";
        pi[3][2]="Mascota";
        pi[4][0]="Juego de mesa";
        pi[4][1]="Cartas";
        pi[4][2]="Gritar";
        pi[5][0]="Ser vivo";
        pi[5][1]="Reptil";
        pi[5][2]="Verde";
        pi[6][0]="Máquina";
        pi[6][1]="Pantalla";
        pi[6][2]="Teclado";
        pi[7][0]="Mueble";
        pi[7][1]="Descansar";
        pi[7][2]="Sala";
        pi[8][0]="Máquina";
        pi[8][1]="Comunicarse";
        pi[8][2]="Portátil";
        pi[9][0]="Tela";
        pi[9][1]="Sombra";
        pi[9][2]="Ventana";

        n=Math.round(Math.random()*9);
        e=(int)Math.floor(n);

        l=p[e].length();
        car=new String[l];
        c=new String[l];

        for(x=0;x<l;x++){
            char pe=p[e].charAt(x);
            car[x]=Character.toString(pe);
            c[x]="*";
        }

        for(x=0;x<l;x++){
           a=a+c[x];
        }

        int i,s=0,mal=0,bi=0,u=0;

        x=0;

        r=JOptionPane.showInputDialog("Tienes 5 intentos para adivinar la palabra. ¡Suerte!\nSi quieres una pista escribe 'pista'\n"+a);
        lp[x]=r;

        for(i=5;i>0;i--){

            if(x>0){

              if(r.equals("pista")){
                bi=bi+1;
              }else{

                for(int w=0;w<x;w++){

                    if(r.equals(lp[w])){
                        s=s+1;
                    }

                }

              }

            }

            x++;

            if(s>0){

                JOptionPane.showMessageDialog(null,"Ya habías ingresado esa letra, intenta de nuevo.");
                mal=l;
                u=1;
                s=0;

            }else{

              for(int y=0;y<l;y++){

                 if(r.equals("pista")){

                    bi=bi+1;   

                    if(np>2){
                        JOptionPane.showMessageDialog(null,"Lo siento, sólo tienes 3 pistas.");
                    }else{
                        JOptionPane.showMessageDialog(null,"Pista: "+pi[e][np]);
                    }

                    np=np+1;
                    y=l;

                 }else{

                   if(r.equals(car[y])){
                        c[y]=r;
                        ad=ad+1;
                        bi=bi+1;
                   }else{
                        c[y]=c[y];
                        mal=mal+1;
                   }

                }

                }  

            }

           a="";  

           for(int z=0;z<l;z++){
             a=a+c[z];
           }


            if(ad==l){
              i=0;
            }else{

              if(mal==l){

                if(u==0){
                    JOptionPane.showMessageDialog(null,"La letra que ingresaste no está en la palabra.");
                }

                r=JOptionPane.showInputDialog("Te quedan "+(i-1)+" intentos.\n"+a);
                lp[x]=r;
                a="";

                if(r.equals(null)){
                    i=0;
                }

              }else{
                r=JOptionPane.showInputDialog("Te quedan "+(i)+" intentos.\n"+a);
                lp[x]=r;
                a="";
                bi=bi+1;

                if(r.equals(null)){
                    i=0;
                }

              }

            } 

           if(bi>0){
               i=i+1;
            }

           mal=0;
           bi=0;

        }

        if(ad==l){
            JOptionPane.showMessageDialog(null,"¡Felicidades! Adivinaste la palabra.");
        }else{
            JOptionPane.showMessageDialog(null,"Lo siento, perdiste. La palabra era "+p[e]);
        }

    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.