Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Tolls

Thread: Help with JLabel, new to Java

  1. #1
    tonynsx is offline Member
    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Default Help with JLabel, new to Java

    Hi everybody,

    I'm new to Java. I'm learning GUI now. My problem is that, the label for my tic tac toe game doesn't work. Please help.
    Java Code:
    package game;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class gamePanel extends JPanel{
        
        private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
        private String mark;
        private int count=0;
        private JLabel label;
        
        public gamePanel()
        {
            ButtonListener listener = new ButtonListener();
            
            setPreferredSize(new Dimension(450,450));
            setLayout(new GridLayout(4,3));
                          
            b1=new JButton(" ");
            b1.addActionListener(listener);
            add(b1);
            
            b2=new JButton(" ");
            b2.addActionListener(listener);
            add(b2);
            
            b3=new JButton(" ");
            b3.addActionListener(listener);
            add(b3);
            
            b4=new JButton(" ");
            b4.addActionListener(listener);
            add(b4);
            
            b5=new JButton(" ");
            b5.addActionListener(listener);
            add(b5);
            
            b6=new JButton(" ");
            b6.addActionListener(listener);
            add(b6);
            
            b7=new JButton(" ");
            b7.addActionListener(listener);
            add(b7);
            
            b8=new JButton(" ");
            b8.addActionListener(listener);
            add(b8);
            
            b9=new JButton(" ");
            b9.addActionListener(listener);
            add(b9);
            
            label = new JLabel("Tic Tac Toe");
            label.setFont(new Font("Helvetica", Font.BOLD, 24));
            label.setForeground(Color.red);
            add(label);
        }
        
        private class ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                count++;
                
                if(count %2 !=0)
                {
                    mark="X";
                }  
                else
                {
                    mark="O";
                } 
                
                if(e.getSource()==b1)
                {
                    b1.setText(mark);
                    b1.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b1.setEnabled(false);
                }
                else if(e.getSource()==b2)
                {
                    b2.setText(mark);
                    b2.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b2.setEnabled(false);
                }
                else if(e.getSource()==b3)
                {
                    b3.setText(mark);
                    b3.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b3.setEnabled(false);
                }
                else if(e.getSource()==b4)
                {
                    b4.setText(mark);
                    b4.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b4.setEnabled(false);
                }
                else if(e.getSource()==b5)
                {
                    b5.setText(mark);
                    b5.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b5.setEnabled(false);
                }
                else if(e.getSource()==b6)
                {
                    b6.setText(mark);
                    b6.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b6.setEnabled(false);
                }
                else if(e.getSource()==b7)
                {
                    b7.setText(mark);
                    b7.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b7.setEnabled(false);
                }
                else if(e.getSource()==b8)
                {
                    b8.setText(mark);
                    b8.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b8.setEnabled(false);
                }
                else if (e.getSource()==b9)
                {
                    b9.setText(mark);
                    b9.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b9.setEnabled(false);
                }
            }
        }
        
        public void result()
        {
            if(b1.getText() == b2.getText() && b2.getText() == b3.getText())  
                label.setText("Player wins.");
            else if(b4.getText() == b5.getText() && b5.getText() == b6.getText())  
                label.setText("Player wins.");
            else if(b7.getText() == b8.getText() && b8.getText() == b9.getText())  
                label.setText("Player wins.");
            else if(b1.getText() == b4.getText() && b4.getText() == b7.getText())  
                label.setText("Player wins.");
            else if(b2.getText() == b5.getText() && b5.getText() == b8.getText())  
                label.setText("Player wins.");
            else if(b3.getText() == b6.getText() && b6.getText() == b9.getText())  
                label.setText("Player wins.");
            else if(b1.getText() == b5.getText() && b5.getText() == b9.getText())  
                label.setText("Player wins.");
            else if(b3.getText() == b5.getText() && b5.getText() == b7.getText())  
                label.setText("Player wins.");
            else
            	label.setText("Computer wins");
    
        }
        
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Help with JLabel, new to Java

    Well, for one thing, you need to use equals to compare strings and not ==. Also, even though I am not trying to following your algorithm for playing the game you can simplify the actionPerformed method. Since you do the same thing for every button, you can just get the button reference from e.getSource() and use it to set the values. The appropriate button will then be updated.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: Help with JLabel, new to Java

    "doesn't work" is something you should expand on, as it could mean anything.
    Jim has spotted a few things, but you really ought to get in the habit of explaining what "doesn't work" actually means.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Java Swing - JLabel setText() doesn't work
    By Zyril in forum New To Java
    Replies: 6
    Last Post: 07-29-2012, 09:51 PM
  2. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  3. jLabel
    By Matty in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2008, 10:22 PM
  4. GUI - JLabel
    By Azndaddy in forum New To Java
    Replies: 8
    Last Post: 05-02-2008, 07:03 AM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 02:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •