I am making a simple minesweeper game(no menus at top and other stuff) All i need to have is a 9x9 board and when each tile is clicked it either is blank if no near bombs, or says how many bombs are near it or says B for bomb.
I have all above correctly done and it works perfectly, my problem is that I need to put in a timer that starts from 0 and continually counts up from 0 while I play the game until i click on a bomb where it stops. Then I will have below my board a place where the user can put in their name and beneath it there is a button called submit which will std out the person's name and time. (Note the player can do this at anytime)
My problem is that whenever I try to add a JPanel or JLabel or anything really to my frame it won't be put on it and that my 9x9 grid keeps scaling to size of my window. How should I add a timer display and a submitting name part so i can see it on the window!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MS extends JFrame implements ActionListener{
static JFrame bframe;
static JPanel p;
static double[] bomb;
public MS() {
p = new JPanel(new GridLayout(9,10));
JButton btn;
bomb = new double[81];
for(int i=0; i<81; i++)
bomb[i] = Math.random();
for (int i=0; i< 81; i++) {
btn = new JButton();
btn.addActionListener(this);
btn.setActionCommand(Integer.toString(i));
p.add(btn);
}
}
public static void main(String[] args) {
bframe=new MS(); //CREATE me and
bframe.add(timelabel);
bframe.add(p); //add the JPanel
bframe.setLocation(32,32); //where my upper left hand corner goes
//bframe.setSize(64*width, 64*height);
bframe.setSize(500,500);
bframe.setVisible(true); //I start out invisible
bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //need this for the window manager
}
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
int temp = Integer.parseInt(e.getActionCommand());
System.out.printf("%s\n", e.getActionCommand());
int count = 0;
String total = "";
if(bomb[temp] > 0.15) {
//for top line
if((temp!=0) && (temp!=1) && (temp!=2) && (temp!=3) && (temp!=4) &&(temp!=5) && (temp!=6) && (temp!=7) && (temp!=8)) {
if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72))
if(bomb[temp-10] <= 0.15)
count++;
if(bomb[temp-9] <= 0.15)
count++;
if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80))
if(bomb[temp-8] <= 0.15)
count++;
}
if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72))
if(bomb[temp-1] <= 0.15)
count++;
if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80))
if(bomb[temp+1] <= 0.15)
count++;
if((temp!=72) && (temp!=73) && (temp!=74) && (temp!=75) && (temp!=76) && (temp!=77) && (temp!=78) && (temp!=79) && (temp!=80)) {
if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72))
if(bomb[temp+8] <= 0.15)
count++;
if(bomb[temp+9] <= 0.15)
count++;
if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80))
if(bomb[temp+10] <= 0.15)
count++;
}
if(count==0)
jb.setText("");
else {
total=Integer.toString(count);
jb.setText(total);
if(count==1)
jb.setForeground(Color.blue);
if(count==2)
jb.setForeground(Color.green);
if(count==3)
jb.setForeground(Color.red);
}
}
else
jb.setText("B");
}
}
For clarity: What i wish to know is why is my 9x9 grid box's scaling with the window, how I can fix it, and how can i add it another 1 or 2 jpanels so I can complete the other parts of my project.