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?