hola gente

me dejaron un proyecto de hacer un juego llamado batalla naval un ejemplo lo pueden ver en
esta direccion... [Enlace externo eliminado para invitados]

el problema es que quiero hacerlo igual igual como ese ejemplo....

mi duda es la siguiente, hice una matriz de botones que me llena un jframe padre con botones de igual tama;o , el problema es que yo quiero hacer un jframe padre que contenga dos jframe hijos y que cada jframe hijo contenga la matriz de botones.... programo desde NetBeans IDE 7.1.2 y los frame los agrego desde la interfaz grafica de NetBeans.

el codigo que me llena el jframe padre es este...

Código: Seleccionar todo

public class Menu2 extends javax.swing.JFrame {

    /**
     * Creates new form Menu2
     */
    public Menu2() {
        initComponents();
    }
    private JButton[][] boton;
    private int n;

    /**
     * Creates a new instance of Main
     */
    @SuppressWarnings("OverridableMethodCallInConstructor")
    public Menu2(int n) {
        this.n = n;
        boton = new JButton[n][n];

        this.setLayout(new GridLayout(n, n));
        this.setBounds(6, 6, 4, 4);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                boton[i][j] = new JButton();
                String nombre = new Integer(i).toString();
                nombre += new Integer(j).toString();
                boton[i][j].setActionCommand(nombre);
                this.add(boton[i][j]);


            }
        }




        this.accionBotones();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(new Dimension(800, 600));
    }

    public void accionBotones() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                boton[i][j].addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {

                        JButton accion = (JButton) evt.getSource();
                        System.out.println("apretado el boton " + accion.getActionCommand());

                       /* if (Integer.parseInt(accion.getActionCommand()) == 2) {
                            boton[0][2].setIcon(new ImageIcon(getClass().getResource("Explosion.gif")));

                            Timer timer = new Timer(2000, new ActionListener() {

                                public void actionPerformed(ActionEvent e) {
                                    boton[0][2].setIcon(new ImageIcon(getClass().getResource("fuego.gif")));
                                     boton[6][8].setIcon(new ImageIcon(getClass().getResource("fuego.gif")));
                                }
                            });


                            timer.start();


                        }
                        * 
                        */

                    }
                });
            }

        }

    }
Un JFrame Padre y luego le colocas 2 JPanel. Y a cada panel le agregas los botones.

Y de introducir 2 JFrame no se puede.
adwind escribió:Un JFrame Padre y luego le colocas 2 JPanel. Y a cada panel le agregas los botones.

Y de introducir 2 JFrame no se puede.

Adwind gracias por tu respuesta. el problema cuando quiero agregar los botones al jpanel me aparece un mensaje que dice> Exception in thread "main" java.lang.NullPointerException
at appprobatallanaval.Main.<init>(Main.java:38)
at appprobatallanaval.AppProBatallaNaval.main(AppProBatallaNaval.java:23)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

no se si asi estara bien este codigo, para agregarselo al jpanel.

Código: Seleccionar todo

 jpanel.setLayout(new GridLayout(n, n));
       for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                boton[i][j] = new JButton();
                String nombre = new Integer(i).toString();
                nombre += new Integer(j).toString();
                boton[i][j].setActionCommand(nombre);
                jpanel.add(boton[i][j]);


            }
        }
hola otra ves acaba de arreglar el error de nullpointerException

cree un objeto para el jpanel

Código: Seleccionar todo

JPanel oJpanel = new JPanel();

    oJpanel.setLayout(new GridLayout(n, n));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                boton[i][j] = new JButton();
                String nombre = new Integer(i).toString();
                nombre += new Integer(j).toString();
                boton[i][j].setActionCommand(nombre);
                oJpanel.add(boton[i][j]);

            }
            }
el problema es que no me muestra el jpanel con los botones< solo se logra ver el frame padre....
Responder

Volver a “Java”