hello, i'm still sort of new to java... anyway, i was bored over this winter break so i decided to try to write a program called "MineSeeker" which... is more or less going to be like Minesweeper. i'm designing the program to simply show three fields where the user can specify how many mines, rows, and cols in the game and a button to get started.
i haven't even gotten to the real functionality of the program yet.. but am getting kicked in the rear end by the gui part of it. i'm not sure if it is my inferior coding or clumsiness on the part of java gui's (my guess is the former). like... i've just been fiddling with the numbers for specifying how many rows and columns and clicking on the button to make sure that the window resizes accordingly. sometimes it works... and sometimes it does not. what perplexes me is that it will work for an unspecified amount of tries, and then it will suddenly not work. however, if i manually maximize the window and then click on the button, it will pack the JFrame the way i imagined i'd coded it to in the first place. if i maximize and then restore... sometimes it will pack the JFrame correctly, and sometimes it won't. even more puzzling, is that if i use different parts of the BorderLayout manager for my textfields, button and mineField (see program code below)... the volatility of the window's inclination to resize changes. i'm not really asking for help on the program as a whole... but i'd really appreciate if someone could tell me if it is indeed something that I'm doing wrong, which is causing my window to not resize correctly after a certain number of times.
thank you very much.
/*********************************************** *MineSeeker v1.0 *by Calvin Chen (cvchen83@yahoo.com) * *MineSeeker is basically a watered-down version *of the Minesweeper game found on MS Windows95+ *platforms. I got bored over the '02-'03 Winter *break while not in class at UCSD, so I dediced *to humor myself. ***********************************************/
public class MineSeeker extends JFrame { private boolean[][] mineContent; private int numRows; private int numCols; private int numMines; private int height; private int width; private JPanel mineField; private JButton start; private JLabel mLabel; private JLabel hLabel; private JLabel wLabel; private JTextField mTF; private JTextField hTF; private JTextField wTF;
/*Constructor*/ public MineSeeker() { int randomR, randomC; Container window = getContentPane(); JPanel top = new JPanel(); JPanel middle = new JPanel(); JPanel bottom = new JPanel();
mLabel = new JLabel("Mines:"); hLabel = new JLabel(" Height:"); wLabel = new JLabel(" Width:"); mTF = new JTextField("10", 2); hTF = new JTextField("10", 2); wTF = new JTextField("10", 2); start = new JButton("!");
mineField = new JPanel() { public void paintComponent(Graphics graphix) { super.paintComponent(graphix); for(int r = 0; r < numRows; r++) { for(int c = 0; c < numCols; c++) { graphix.drawRect(c * 15, r * 15, 15, 15); } } } };