Results 1 to 6 of 6
  1. #1
    goldman is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default Problem using buttons to creat a magic square game

    Hi, i am pretty new to java and have been given an assignment to create a magic square board game. I have used a gridLayout of buttons to create the boxes. The first thing i need to do is click two of the buttons to join together to create a free cell. My problem is i cannot seem to get the code for the actionPerformed correct. Is it actually possible to do this using buttons and gridLayout ? My code is below

    Java Code:
       import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;
    
    
    public class JoalGrid extends Applet implements ActionListener{
       
      private Button button1, button2, button3, button4, button5, button6,button7, button8, button9;
      
    int firstButton;
      
      
       public void init() {
       
      button1 = new Button ("1"); 
      button1.addActionListener(this);
      button2 = new Button ("2");
      button2.addActionListener(this);
      button3 = new Button ("3");
      button4 = new Button ("4");
      button5 = new Button ("5");
      button6 = new Button ("6");
      button7 = new Button ("7");
      button8 = new Button ("8");
      button9 = new Button ("9");
    
       
       
          setLayout(new GridLayout(3,3,6,6));
            add(button1);
            add(button2);
            add(button3);
            add(button4);
            add(button5);
            add(button6);
            add(button7);
            add(button8);
            add(button9);
            
            }
        
        public void actionPerformed(ActionEvent e){
        
        if(e.getSource()==button1)
        if(e.getSource()==button2)
          
          button1.setLabel("free cell");
          repaint();
          
          
          
          }
          
          
        
        
    
        }
    If i try
    Java Code:
     if(e.getSource()==button1&&e.getSource()==button2)
    im told the && operator cannot be applied to a boolean, is there another way of doing this?

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    try to inclose them with parenthesis....

    eg. ((e.getSource()==button1)&&(e.getSource()==button2 ))

    Update us,
    freedom exists in the world of ideas

  3. #3
    goldman is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    It now compiles but when i click button1 then button2 nothing happens

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    That if statement will only allow its subtask to execute if both button1 and button2 are equal....

    I think they are not.....

    when i click button1 then button2 nothing happens
    If you click button1, the if statement fails bec,
    e.getSource()==button1 is true,
    but e.getSource()==button2 is false, and you are using && operator

    Try to change && to ||

    What are you trying to do? either button1 or 2 be clicked, something must be happened?
    freedom exists in the world of ideas

  5. #5
    goldman is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    I am trying to get it that so that if i click button1 followed by button2 then button ones label will change to free and button twos label will add the value of button ones label to the value of button2 and display the total. E.g. I click one, the two. One should become2free" and two should become"3"
    Last edited by goldman; 05-04-2008 at 07:42 PM.

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    After the said example, if again one was clicked then two,

    Is the label for button1 will now become 3free?
    And is the label for button2 will now become 4?

    If it is, you can set a flag(boolean) for sure, try to have some experiment on it.

    algo,

    button1 clicked, flag1 set to true
    button2 clicked, check if flag1 is true,
    if it is,
    button1.setText(button2.getText().concat("free"));
    button2.setText(new value);
    flag1 = false;
    freedom exists in the world of ideas

Similar Threads

  1. GUI buttons question...
    By glenrowan in forum New To Java
    Replies: 3
    Last Post: 01-18-2011, 06:23 PM
  2. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  3. change the square to triangle java
    By anotsu in forum New To Java
    Replies: 3
    Last Post: 07-09-2009, 12:17 PM
  4. Incompatible magic value 1008821359
    By willemjav in forum Java Applets
    Replies: 2
    Last Post: 03-21-2008, 10:41 AM
  5. Next, Finish Buttons !!!
    By pele in forum SWT / JFace
    Replies: 1
    Last Post: 07-14-2007, 06:22 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
  •