Java: error when calling JPanel from JFrame

2

I have a class that inherits from JFrame and the view is shown, in the view I have a button where I call another class that inherits from JPanel but the new view does not open, I hope and you can help me.

  

JFrame

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//HEREDA DE JFRAME
public class Index extends JFrame {

private static final long serialVersionUID = 1L;

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

//Atributos
private JTextField txtJugador1, txtJugador2;
private JButton btnIngresar;
//private JLabel labelImagen;
private JLabel lBienvenido, lblJugador1, lblJugador2;
//private JTextField txtJugador2;

//Constructor
public Index () {
    getContentPane().setForeground(new Color(255, 255, 204));
    this.setSize(400, 310);
    getContentPane().setLayout(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setResizable(false);

    Font estilo = new Font("Andale Mono", Font.PLAIN, 14);

    lblJugador1 = new JLabel("Nombre (jugador 1)");
    lblJugador1.setBounds(99, 46, 174, 30);
    lblJugador1.setFont(estilo);
    this.getContentPane().add(lblJugador1);

    txtJugador1 = new JTextField();
    txtJugador1.setBounds(99,  88, 174, 30);
    txtJugador1.setFont(estilo);
    this.getContentPane().add(txtJugador1);

    lblJugador2 = new JLabel("Nombre (jugador 2)");
    lblJugador2.setBounds(99, 130, 174, 30);
    lblJugador2.setFont(estilo);
    this.getContentPane().add(lblJugador2);

    btnIngresar = new JButton("Ingresar");
    //btnIngresar.addActionListener(this);
    btnIngresar.setBounds(99, 241, 174, 30);
    btnIngresar.setFont(estilo);
    getContentPane().add(btnIngresar);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setResizable(false);

    lBienvenido = new JLabel("Bienvenido");
    lBienvenido.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
    lBienvenido.setBounds(149, 7, 138, 30);
    getContentPane().add(lBienvenido);

    txtJugador2 = new JTextField();
    txtJugador2.setFont(new Font("Andale Mono", Font.PLAIN, 14));
    txtJugador2.setBounds(99, 172, 174, 30);
    getContentPane().add(txtJugador2);

    btnIngresar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Jugador jugador = new Jugador();
            jugador.setJugador1(txtJugador1.getText());
            jugador.setJugador2(txtJugador2.getText());
            //mandamos a la ventana del juego
            Juego juego = new Juego();
            juego.setVisible(true);
            System.out.println("si entra");
        }
    });

}
} // termina la clase
  

JPanel

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Juego extends JPanel {

private static final long serialVersionUID = 1L;

Pelota pelota = new Pelota(this);
    ArrayList <Pelota> pelotas = new ArrayList<>();
    Raqueta raqueta = new Raqueta(this);
    Raqueta raqueta2 = new Raqueta(this);//se crea la segunda raqueta
    int aceleracion = 10;

    public Juego() {
        establecerEscuchadorDeTeclado();
        setFocusable(true);
        pelotas.add(pelota);
        raqueta.Y = 330;
        raqueta2.Y = 30;
    }

private void move(){
        for (int i = 0; i < pelotas.size(); i++) {
           pelotas.get(i).mover();
        }
        raqueta.mover();
        raqueta2.mover();//se mueva la segunda raqueta
}

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            for (int i = 0; i < pelotas.size(); i++) {
                pelotas.get(i).visualizar(g2d);
            }
            raqueta.visualizar(g2d);
            raqueta2.visualizar(g2d);//visualizo la segunda raqueta
}

public static void main(String[] args) throws InterruptedException {
    JFrame frame = new JFrame("Caso de estudio");
    Juego game = new Juego();
    frame.add(game);
            frame.setResizable(false);
    frame.setSize(300, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    while(true){
        game.move();
        game.repaint();
        Thread.sleep(game.aceleracion);

    }

}

    private void establecerEscuchadorDeTeclado() {
        addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                raqueta.keyPressed(e);
                raqueta2.keyPressed(e);//teclas con las que se va a mover
            }

            @Override
            public void keyReleased(KeyEvent e) {
                raqueta.keyReleased(e);
                raqueta2.keyReleased(e);
            }
        });
    }

    public void gameOver() {
        JOptionPane.showMessageDialog(this, "GAME OVER", "\nEl juego ha terminado", JOptionPane.YES_NO_OPTION);
        System.exit(ABORT);
    }
}
    
asked by Javier fr 06.02.2017 в 21:42
source

3 answers

1

You still need to add your jpanel to jframe , you need to add it add(juego);

public void actionPerformed(ActionEvent e) {
            Jugador jugador = new Jugador();
            jugador.setJugador1(txtJugador1.getText());
            jugador.setJugador2(txtJugador2.getText());
            //mandamos a la ventana del juego
            Juego juego = new Juego();

juego.setVisible(true);
getContentPane()..add(juego);
            System.out.println("si entra");
        }
    
answered by 06.02.2017 в 22:08
0

You create the JPanel in a local variable, but you never add it to the JFrame.

btnIngresar.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Jugador jugador = new Jugador();
        jugador.setJugador1(txtJugador1.getText());
        jugador.setJugador2(txtJugador2.getText());
        //mandamos a la ventana del juego
        Juego juego = new Juego();
        // eso no te sirve para nada:
        juego.setVisible(true);
        System.out.println("si entra");
    }
});

You have to join the panel to the frame, so you can declare a field in the frame and assign the juego to this field, or simply add it to the frame as:

btnIngresar.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Jugador jugador = new Jugador();
        jugador.setJugador1(txtJugador1.getText());
        jugador.setJugador2(txtJugador2.getText());
        //mandamos a la ventana del juego
        Juego juego = new Juego();
        getContentPane().add(juego);
        juego.setVisible(true);
        System.out.println("si entra");
    }
});

If you want more control like or where it is shown to you, add a JPanel in Your frame where it suits you, name it for example JPanel juegoHolder and add the game to this panel.

btnIngresar.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Jugador jugador = new Jugador();
        jugador.setJugador1(txtJugador1.getText());
        jugador.setJugador2(txtJugador2.getText());
        //mandamos a la ventana del juego
        Juego juego = new Juego();
        ((Index)getContentPane()).juegoHolder.add(juego);
        juego.setVisible(true);
        System.out.println("si entra");
    }
});

A bit more elaborate would be, add a CardLayout to your gui and then place games named in this JPanel with the option to change between games added with the api of CardLayout . Maybe it would be for a re-invoicing with eventually a new question.

    
answered by 06.02.2017 в 22:11
0

Well it would have been easier than everything you see at the beginning in the JFrame you do it in a JPanel you add all the components to the JPanel and then you place it at the JFrame as you call the panel of the game, Hide the JPanel of the Form and place the game panel if you do not show them is by the controls of the JFrame .

public void actionPerformed(ActionEvent e) {
            Jugador jugador = new Jugador();
            jugador.setJugador1(txtJugador1.getText());
            jugador.setJugador2(txtJugador2.getText());
            //mandamos a la ventana del juego
            Juego juego = new Juego();
add(juego);
            juego.setVisible(true);
            System.out.println("si entra");
        }

So you call the game JPanel.

    
answered by 01.09.2017 в 15:49