Results 1 to 6 of 6
- 05-04-2008, 04:53 PM #1
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
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
If i tryJava 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(); } }im told the && operator cannot be applied to a boolean, is there another way of doing this?Java Code:if(e.getSource()==button1&&e.getSource()==button2)
- 05-04-2008, 05:09 PM #2
try to inclose them with parenthesis....
eg. ((e.getSource()==button1)&&(e.getSource()==button2 ))
Update us,freedom exists in the world of ideas
- 05-04-2008, 05:36 PM #3
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
It now compiles but when i click button1 then button2 nothing happens
- 05-04-2008, 06:09 PM #4
That if statement will only allow its subtask to execute if both button1 and button2 are equal....
I think they are not.....
If you click button1, the if statement fails bec,when i click button1 then button2 nothing happens
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
- 05-04-2008, 06:39 PM #5
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
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 06:42 PM.
- 05-05-2008, 04:04 AM #6
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
-
GUI buttons question...
By glenrowan in forum New To JavaReplies: 3Last Post: 01-18-2011, 05:23 PM -
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
change the square to triangle java
By anotsu in forum New To JavaReplies: 3Last Post: 07-09-2009, 11:17 AM -
Incompatible magic value 1008821359
By willemjav in forum Java AppletsReplies: 2Last Post: 03-21-2008, 09:41 AM -
Next, Finish Buttons !!!
By pele in forum SWT / JFaceReplies: 1Last Post: 07-14-2007, 05:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks