0

I'm trying to make a game of minesweeper in java, but my components are not showing up when the new game button is pressed. Here is the whole code:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package campominado;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Felipe
 */
public class MainUI extends javax.swing.JFrame {

    /**
     * Creates new form MainUI
     */
    public MainUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        file = new javax.swing.JMenu();
        novoJogo = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        file.setText("File");

        novoJogo.setText("Novo Jogo");
        novoJogo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novoJogoActionPerformed(evt);
            }
        });
        file.add(novoJogo);

        jMenuBar1.add(file);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 277, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void novoJogoActionPerformed(java.awt.event.ActionEvent evt) {                                         
        JPanel area = new JPanel();
        JPanel areaJogo = new JPanel();
        JPanel areaMenu = new JPanel();

        this.add(area);

        area.setLayout(new BorderLayout());
        area.add(areaMenu, BorderLayout.NORTH);
        area.add(areaJogo, BorderLayout.SOUTH);

        JLabel linhas, colunas, minas;
        linhas = new JLabel("Linhas:");
        colunas = new JLabel("Colunas:");
        minas = new JLabel("Minas:");
        JTextField lin, col, min;
        lin = new JTextField();
        col = new JTextField();
        min = new JTextField();

        areaMenu.setLayout(new FlowLayout());
        areaMenu.add(linhas);
        areaMenu.add(lin);
        areaMenu.add(colunas);
        areaMenu.add(col);
        areaMenu.add(minas);
        areaMenu.add(min);
        areaMenu.add(new JButton("Iniciar"));

        areaMenu.validate();
        areaMenu.repaint();
        System.out.println("teste");
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new MainUI().setVisible(true);
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JMenu file;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem novoJogo;
    // End of variables declaration                   
}

On a related question: after this method starts working I plan to make a listener for the OK button to fill the second panel with the cells of the game using lots of buttons on a gridlayout. Is there a better way of doing this or my idea is fine?

Leet-Falcon
  • 2,107
  • 2
  • 15
  • 23
user1912475
  • 39
  • 1
  • 5
  • Are you certain that this method gets called ? Could you post the code linking the "New Game" button to it ? – Arnaud Dec 04 '15 at 14:01
  • Is your program printing "teste" ? System.out.println("teste"); – reos Dec 04 '15 at 15:34
  • Assuming ``this`` relates to your game window, what ``LayoutManager`` is installed there? - I am asking because``this.add(area);`` comes with no layouting argument. With the default - ``null`` layout - the window arranges controls by their absoulte coordinates, ,meaning the panel ``area`` will be put to 0, 0 (the top left corner). This is a wild guess; please post your full code or better, create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)!... – Binkan Salaryman Dec 04 '15 at 16:53
  • Yes, the program do print "test". The rest of the code was all auto-generated by netbeans, post edited with the whole thing. – user1912475 Dec 04 '15 at 21:32

1 Answers1

0

When you are adding a JPanel to this in run-time, you must indicate that the layout for this has changed so swing can redo the new layout and repaint.
Your changes are not limited to areaMenu only.
So instead of:

areaMenu.validate();
areaMenu.repaint();

Call:

this.revalidate();  // redo the layout and notify the parent 
this.repaint();     // redraw the updated state

See here for further detail.

Community
  • 1
  • 1
Leet-Falcon
  • 2,107
  • 2
  • 15
  • 23