Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 HOW TO include button below sudoku puzzle? please help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rated




PostPosted: Tue May 19, 2009 6:22 pm   Post subject: HOW TO include button below sudoku puzzle? please help

I have my basic sudoku program here (it hasnt been able to solve yet). After the user presses level 1, they will be taken to the puzzle itself.

I want to include a button below the puzzle. But everytime I try doing so, it messes up the order of the rows and columns. Can someone please help me create a JButton below the puzzle?

thanks

code:
import java.awt.Button;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class practice extends JApplet implements Runnable, ActionListener{

        JButton num0 = new JButton("Start Level 1");
        JButton num1 = new JButton ("start level 2");
        JPanel p = new JPanel();
       

        Font firstFont = new Font("Times New Romans", Font.PLAIN, 20);


        public static final Component dialog = null;


        /** declares the array for the three different levels */
        public int level1[][] ;
        public int level2[][];
        public int level3[][];
        public int x[][];


        public void init(){
                p.setLayout(new FlowLayout());
                add(num0);
                num0.addActionListener(this);
               
        }


        public void actionPerformed(ActionEvent e) {
                JButton button = (JButton) e.getSource();
                if (button == num0) {
                        initlevel1();
                        start();
                        remove (num0);

                }

               
        }   

        /** The view */
        public Button view[][] ;


        /** Creates the sudoku puzzle for the three different levels */
        public void createModel()
        {
                level1 = new int[9][9] ;
                // Clear all cells
                for( int row = 0; row < 9; row++ )
                        for( int col = 0; col < 9; col++ )
                                level1[row][col] = 0 ;
                // Create the initial situation
                level1[0][0] = 6 ;
                level1[0][3] = 1 ;
                level1[0][5] = 8 ;
                level1[0][6] = 2 ;
                level1[0][8] = 3 ;
                level1[1][1] = 2 ;
                level1[1][4] = 4 ;                                               
                level1[1][7] = 9 ;
                level1[2][0] = 8 ;
                level1[2][2] = 3 ;
                level1[2][5] = 5 ;
                level1[2][6] = 4 ;
                level1[3][0] = 5 ;
                level1[3][2] = 4 ;
                level1[3][3] = 6 ;
                level1[3][5] = 7 ;
                level1[3][8] = 9 ;
                level1[4][1] = 3 ;
                level1[4][7] = 5 ;
                level1[5][0] = 7 ;
                level1[5][3] = 8 ;
                level1[5][5] = 3 ;
                level1[5][6] = 1 ;
                level1[5][8] = 2 ;
                level1[6][2] = 1 ;
                level1[6][3] = 7 ;
                level1[6][6] = 9 ;
                level1[6][8] = 6 ;
                level1[7][1] = 8 ;
                level1[7][4] = 3 ;
                level1[7][7] = 2 ;
                level1[8][0] = 3 ;
                level1[8][2] = 2 ;
                level1[8][3] = 9 ;
                level1[8][5] = 4 ;
                level1[8][8] = 5 ;

               
        }

       


        /** Creates an empty view */
        public void createView()
        {
                setLayout( new GridLayout(9,9) ) ;
                //sets size of Applet
                setSize (300,300);
                view = new Button[9][9] ;
                // Create an empty view
                for( int row = 0; row < 9; row++ )
                        for( int col = 0; col < 9; col++ )
                        {
                                view[row][col]  = new Button() ;
                                add( view[row][col] ) ;
                        }
        }

        /** Updates the view from the level1 */
        public void updateView()
        {
                for( int row = 0; row < 9; row++ )
                        for( int col = 0; col < 9; col++ )
                                if( x[row][col] != 0 )
                                        view[row][col].setLabel( String.valueOf(x[row][col]) ) ;
                                else
                                        view[row][col].setLabel( "" ) ;
        }
        public void initlevel1()
        {
                createModel() ;
                x=level1;
                createView() ;
                updateView() ;
        }
       
        public void start()
        {
                // This statement will start the method 'run' to in a new thread
                (new Thread(this)).start() ;
        }
        public void run()
        {
                try
                {
                        // Let the observers see the initial position
                        Thread.sleep( 1000 ) ;

                        // Start to solve the puzzle in the left upper corner
                       
                }
                catch( Exception e )
                {
                }
        }
        }

Sponsor
Sponsor
Sponsor
sponsor
Dark




PostPosted: Wed May 20, 2009 2:43 pm   Post subject: RE:HOW TO include button below sudoku puzzle? please help

Didn't read most of your code, but if you want a button at the bottom, try this:

Java:

public void init()
{
  p.setLayout(new BorderLayout());
  p.add(num0, BorderLayout.SOUTH);
  num0.addActionListener(this);             
}


You didn't put p.add(num0); Try adding the button to the panel p, you just put add, but no p.add...

You could also try p = (JPanel) frame.getContentPane();

^ For your frame, whatever it may be.



You could also try setting the frame visible to true At the end of the constructor, if nothing else works.
rated




PostPosted: Fri Jun 05, 2009 3:17 pm   Post subject: Re: HOW TO include button below sudoku puzzle? please help

I know a few people in my class who copied my code after I pasted it, so in case my teacher checks this website, R.W. (intiails) wrote this...
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: