Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: gui not linking with main project/ class

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default gui not linking with main project/ class

    hello guys im working on a tic tac toe game wherei am Creating a game in which the user is presented with a three by-
    three gui containing the digits 1 through 9. When the user chooses a position by
    typing a number, place an X in the appropriate spot. Generate a random number for
    the position where the computer will place an O .....

    import javax.swing.*;
    import tict.TicTGui;
     
    //import tict.TicTGui;
     
      public class TicT //implements ActionListener
    {
    	/*	Variables*/
        private javax.swing.JButton button1;
        private javax.swing.JButton button2;
        private javax.swing.JButton button3;
        private javax.swing.JButton button4;
        private javax.swing.JButton button5;
        private javax.swing.JButton button6;
        private javax.swing.JButton button7;
        private javax.swing.JButton button8;
        private javax.swing.JButton button9;
        private javax.swing.JFrame jFrame1;
     
        private String letter = "";
        public static int count = 0;
     
        /*Make The Window Visible*/
         public void run()
                {
                    new TicTGui().setVisible(true);
                    setCount(1);
     
                }
     
     
     
        private void setCount(int i)
        {
            throw new UnsupportedOperationException("Not yet implemented");
        }
     
    	/*Calculate Who's Turn It Is*/
        public int  WhoToPlay()
        {
     
            if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9|| count == 11)
            {
                letter = "X";
     
            } else
            		if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
            {
                letter = "O";
            }
    		return count;
        }
     
    	/*checking who plays*/
       	public void MakeYourMove(int count)
    	{
    		/*int me=0;
    		int you=0;
    		if(count%2==2)
    		{
    			count=me;
    		}
    		else
    		{
    		 	count=you;
    		}*/
     
     
        	for(int i=0;i<9;i++)
        	{
     
        		if(count==1)
        		{
    			 String input = JOptionPane.showInputDialog("welcome  you will take X and computer wiil take O \n Please ENTER number you want to play ");
            		 int pawn = Integer.parseInt(input);
                    //set letter  to choosen buttons
                    switch (input)
                    {
                        case "1":
                            button1.setText("X");
                            button1.setEnabled(false);
                            break;
                        case "2":
                            button2.setText("X");
                            button2.setEnabled(false);
                            break;
                        case "3":
                            button3.setText("X");
                            button3.setEnabled(false);
                            break;
                        case "4":
                            button4.setText("X");
                            button4.setEnabled(false);
                            break;
                        case "5":
                            button2.setText(letter);
                            button5.setEnabled(false);
                            break;
                        case "6":
                            button6.setText("X");
                            button6.setEnabled(false);
                            break;
                        case "7":
                            button7.setText("X");
                            button7.setEnabled(false);
                            break;
                        case "8":
                            button8.setText("X");
                            button8.setEnabled(false);
                            break;
                        case "9":
                            button9.setText(letter);
                            button9.setEnabled(false);
                            break;
                        default:
                            JOptionPane.showInputDialog("Sorry invalid number entered Please enter a number from 1 to 9 \n Press enter to procced ");
                            break;
                    }
    		    	}
     
    		    	//generating a number to be played by computer
              		  int ran = 1 + (int)(Math.random() * 9);
    		    	//else
    		    		if(count==2)
    		    		{
    		    			 if (ran==1)
    		        		{
                                                button1.setText("O");
    				            button1.setEnabled(false);
     
    		       			 }
    				         else
    				         	if(ran==2)
    				       	{
    				             button2.setText("O");
    				             button2.setEnabled(false);
    				        }
    				         else
    				         	if(ran==3)
    				       	{
    				             button3.setText("0");
    				             button3.setEnabled(false);
    				        }
    				         else
    				         	if(ran==4)
    				       	{
    				             button4.setText("0");
    				             button4.setEnabled(false);
    				        }
    				         else
    				         	if(ran==5)
    				       	{
    				            button5.setText("0");
    				            button5.setEnabled(false);
    				        }
    				         else
    				         	if(ran==6)
    				       	{
    				             button6.setText("O");
    				             button6.setEnabled(false);
    				        }
    				         else
    				         	if(ran==7)
    				       	{
    				             button7.setText("X");
    				             button7.setEnabled(false);
    				        }
    				         else
    				         	if(ran==8)
    				       	{
    				             button8.setText("X");
    				             button8.setEnabled(false);
    				        }
    				         else
    				         	if(ran==9)
    				       	{
    				             button9.setText(letter);
    				             button9.setEnabled(false);
    				    	}
     
        				}
    	}
    	}
    	 public static void main(String[] args)
     
     
    }


    problem is that when i run this code the gui gets blank and does not do instuctions from main project


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: gui not linking with main project/ class

    What's up with your main() method? Did your copy/paste miss something?

    And where's the class TicTGui? I don't know that you need to post all that's missing, but we need to understand what you're trying to do, the parts involved, and how you've wired them together. That is completely unclear from what you've posted so far.

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: gui not linking with main project/ class

    here
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package tict;
     
    /**
     *
     * @author njabulo
     */
    public class TicTGui extends javax.swing.JFrame {
     
        /**
         * Creates new form TicTGui
         */
        public TicTGui() {
            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() {
     
            jFrame1 = new javax.swing.JFrame();
            button1 = new javax.swing.JButton();
            button4 = new javax.swing.JButton();
            button7 = new javax.swing.JButton();
            button2 = new javax.swing.JButton();
            button5 = new javax.swing.JButton();
            button8 = new javax.swing.JButton();
            button3 = new javax.swing.JButton();
            button6 = new javax.swing.JButton();
            button9 = new javax.swing.JButton();
     
            javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
            jFrame1.getContentPane().setLayout(jFrame1Layout);
            jFrame1Layout.setHorizontalGroup(
                jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            jFrame1Layout.setVerticalGroup(
                jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(new java.awt.Color(255, 0, 51));
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(button7, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                        .addComponent(button4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(button1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(14, 14, 14)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(button3, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(button5, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                                .addComponent(button8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(button6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(button9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGap(22, 22, 22))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(button1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(button2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(button3, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(button4, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                                .addComponent(button6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(button7, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(button8, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(button9, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap(22, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        /**
         * @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
             * [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
             */
            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(TicTGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(TicTGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(TicTGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(TicTGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() 
                {
                    new TicTGui().setVisible(true);
     
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton button1;
        private javax.swing.JButton button2;
        private javax.swing.JButton button3;
        private javax.swing.JButton button4;
        private javax.swing.JButton button5;
        private javax.swing.JButton button6;
        private javax.swing.JButton button7;
        private javax.swing.JButton button8;
        private javax.swing.JButton button9;
        private javax.swing.JFrame jFrame1;
        // End of variables declaration                   
    }


    --- Update ---

    note that the above code is automaticaly generated as you design gui

    --- Update ---

    note that the above code is automaticaly generated as you design gui

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: gui not linking with main project/ class

    Yes, I see you're using the GUI Builder. I'm glad I'm not modifying that code.

    The class TicT doesn't seem to be involved in what you've posted, yet it contains the logic for playing the game. Is your question how to add that logic or include it in the GUI?

  5. #5
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: gui not linking with main project/ class

    yes i want to know "how to add that logic or include it in the GUI?"

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: gui not linking with main project/ class

    Then I suggest you do something like:

    Move the main() method to a separate class and reduce it to something like:
    public class TestClass
    {
        public static void main(String args[])
        {
            /*
             * Create and display the form on the EDT
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() 
                {
                    new TicTGui();
                }
            });
     
        } // end method main()
     
    } // end class TestClass
    Modify the TicTGui class' constructor to create an instance of TicT that is aware of the GUI, and then calls the TicT run() method after the GUI is built, like this:
        // default constructor
        public TicTGui()
        {
            initComponents();
            TicT ticT = new TicT( this );
            setVisible( true );
            ticT.run();
     
        } // end default constructor
    Change the constructor in TicT to accept a TicTGui object, like this:
        // constructor that accepts a TicTGui object
        public TicT( TicTGui ticTGui )
        {
            this.ticTGui = ticTGui;
     
        } // end constructor
    Change the game play logic in TicT to modify the GUI when moves are chosen, like this but for each player and each button:
                        case "5":
                            ticTGui.setButton5Text( "X" );
                            ticTGui.setButton5Enabled( false );
                            break;
    And add the necessary methods to the GUI code, like this (but for all buttons):
        public void setButton5Text( String text )
        {
            button5.setText( text );
        }
     
        public void setButton5Enabled( boolean enabled )
        {
            button5.setEnabled( enabled );
        }
    And that wires all of the parts together. The main() method creates a TicTGui instance, then TicTGui creates a TicT instance, passing to the TicT instance an instance of itself, and then the TicT.run() method is called to control the game play, updating the TicTGui instance using TicTGui setter/mutator methods to reflect the game's state.

    Hope this helps. Ask any other questions you have as needed.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    njabulo ngcobo (September 15th, 2013)

  8. #7
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: gui not linking with main project/ class

    got it

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: gui not linking with main project/ class

    Oh, I was staring at the code later, and I realized (I think) there's no checking to see if either the user or the computer chooses a square that has already been chosen. Disabling the button will work in a game where the user moves with a mouse (and you're not there yet), but it won't prevent the computer selecting a disabled square.

    Also, the moves should be made with a single method rather than having nearly duplicate logic for the user and the computer. Flip/flop the player between user/computer and then call a method to decide the move and update the GUI based on the player's choice and whether that player is 'X' or 'O'.

  10. #9
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: gui not linking with main project/ class

    oh yes with regards to the moves bieng made with a single method rather than having nearly duplicate logic for the user and the computer,thats what the assignment want but i thought it would confuse me even more,any advice on how i can do that

    --- Update ---

    case "5":
                            ticTGui.setButton5Text( "X" );
                            ticTGui.setButton5Enabled( false );
                            break;

    setButton5Text now has errors will this work [code=java] public void setbutton5Text(String x) {
    throw new UnsupportedOperationException("Not yet implemented");
    }[/java]

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: gui not linking with main project/ class

    Post the error if this doesn't help: Remember that Java is case sensitive. setbutton5Text() is not the same as setButton5Text(). Did you add the method setButton5Text() to class TicTGui?

    Your suggested change won't work because of Java's case sensitivity, and even if you fix the case problem, it is temporary code that you'll have to change to functional code someday.

  12. #11
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: gui not linking with main project/ class

    case "5":
                            ticTGui.setbutton5Text( "X" );
                            ticTGui.setbutton5Enabled( false );

    right there "cannot fing symbol"

  13. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: gui not linking with main project/ class

    C'mon. When we ask you to post the error, post the danged error, copied and pasted from exactly as it appears at your end, all of it. If it's long, put it in code tags. There are several bits of info your poor interpretation doesn't include.

    Also, show the methods you wrote in TicTGui, setbutton5Text() and setbutton5Enabled().

  14. #13
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: gui not linking with main project/ class

    #now i am confused was i suppose to modify the code in the gui builder source code

  15. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: gui not linking with main project/ class

    My last instruction in post #6 specifically answers this. Don't be afraid of that code and all if its warnings to not touch it. It's YOUR source code (poorly written, though it is), and you should learn how to own it and use it IF you're going to use the GUI Builder.

    Otherwise, learn to write the application by hand and trash that GUI Builder stuff.

    As for writing common methods for both the computer and the user, you can construct your code something like:

    // defines whose turn it is, the player or the computer. simply
    // flip/flops between the two.
    selectPlayer();

    // gets the user's move or selects the computer's move
    // based on whose turn it is
    getPlayersMove();

    // modifies the GUI to reflect the player's move
    setPlayersMove();

    The key is to "generalize" the methods to the greatest degree possible so that they can be used no matter whose turn it is. The methods either adjust to which player is taking a turn, or the result simply doesn't matter whose turn it is.

Similar Threads

  1. Noobie Object/Class Linking help
    By Bonerize in forum Object Oriented Programming
    Replies: 4
    Last Post: January 31st, 2013, 08:40 AM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  4. Linking class's
    By Tate in forum Object Oriented Programming
    Replies: 3
    Last Post: February 23rd, 2012, 04:12 AM
  5. [SOLVED] FREE HELP WANTED: Setting up Class structure for Gui(JFrame) project.
    By JonLane in forum Object Oriented Programming
    Replies: 2
    Last Post: February 21st, 2012, 02:19 AM

Tags for this Thread