Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

NEed Help!!!

Hi,

i am tying to make a game. here is how it should work when i press the green button , the textField in front of it should show how many times i clicked the button.

THANKS IN ADVANCE !!!!!!!!

here is the code.
FRAME:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class HorseGameFrame extends JFrame{
private JButton Start;
private JButton Green;
private JButton Red;
private JButton Blue;
private JTextField mark1, mark2, mark3;

public HorseGameFrame(String title){
super(title);
setLayout(null);

add(theButton());
add(blueHorsebox());
add(greenHorsebox());
add(redHorsebox());
add(theTextFileds());

setDefaultCloseOperation(EXIT_ON_CLOSE);

setSize(448,204);

}

public JPanel theButton() {

JPanel layoutControl = new JPanel();
layoutControl.setLayout(new GridLayout(4,1,7,5));

Start = new JButton("Start");
Start.setFont(new Font("Times", Font.BOLD, 16));
layoutControl.add(Start);

Green = new JButton("Green");
Green.setFont(new Font("Times", Font.BOLD, 16));
layoutControl.add(Green);

Red = new JButton("Red");
Red.setFont(new Font("Times", Font.BOLD, 16));
layoutControl.add(Red);

Blue = new JButton("Blue");
Blue.setFont(new Font("Times", Font.BOLD, 16));
layoutControl.add(Blue);

layoutControl.setSize(80,145);
layoutControl.setLocation(10,7);
return layoutControl;
}

public JPanel greenHorsebox(){
JPanel greenHorse = new JPanel();
greenHorse.setLayout(new GridLayout(1,10,2,2));
JLabel [] greenLabel;
greenLabel = new JLabel[10];

for(int i=0; i<greenLabel.length;++i){
JLabel aButton = new JLabel(new ImageIcon("box.gif"));
greenLabel[i] = aButton;
greenHorse.add(aButton);
aButton.setOpaque(true);
aButton.setBackground(Color.white);
aButton.setBorder(BorderFactory.createLineBorder(Color.gray));

}

greenHorse.setSize(289,32);
greenHorse.setLocation(140,44);

return greenHorse;
}

public JPanel blueHorsebox(){
JPanel blueHorse = new JPanel();
blueHorse.setLayout(new GridLayout(1,10,2,2));
JLabel [] blueLabel;
blueLabel = new JLabel[10];

for(int i=0; i<blueLabel.length;++i){
JLabel aButton = new JLabel(new ImageIcon("box.gif"));
blueLabel[i] = aButton;
blueHorse.add(aButton);
aButton.setOpaque(true);
aButton.setBackground(Color.white);
aButton.setBorder(BorderFactory.createLineBorder(Color.gray));

}

blueHorse.setSize(289,32);
blueHorse.setLocation(139,81);

return blueHorse;
}

public JPanel redHorsebox(){
JPanel redHorse = new JPanel();
redHorse.setLayout(new GridLayout(1,10,2,2));
JLabel [] redLabel;
redLabel = new JLabel[10];

for(int i=0; i<redLabel.length;++i){
JLabel aButton = new JLabel(new ImageIcon("box.gif"));
redLabel[i] = aButton;
redHorse.add(aButton);
aButton.setOpaque(true);
aButton.setBackground(Color.white);
aButton.setBorder(BorderFactory.createLineBorder(Color.gray));

}

redHorse.setSize(289,32);
redHorse.setLocation(140,119);

return redHorse;
}

public JPanel theTextFileds(){

JPanel layoutControlText = new JPanel();
layoutControlText.setLayout(new GridLayout(4,1,7,5));

JTextField mark1 = new JTextField("0");
layoutControlText.add(mark1);

JTextField mark2 = new JTextField("0");
layoutControlText.add(mark2);

JTextField mark3 = new JTextField("0");
layoutControlText.add(mark3);

layoutControlText.setSize(29,145);
layoutControlText.setLocation(100,45);
return layoutControlText;
}

/*private void updateTextField() {
mark1.setText("");
}
private void addButtonEventHandler() {

int theNumber = 0;
mark1.setText("1");
updateTextField();
}
*/
}

AND THE MAINimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class HorseGameUI extends JFrame{

public static void main(String[] args) {
// Set up the items to be put into the list
//JFrame.setDefaultLookAndFeelDecorated(true);
HorseGameFrame frame = new HorseGameFrame("GAME");
frame.setVisible(true);
}

}
[4721 byte] By [kimstanely] at [2007-11-11 7:53:12]
# 1 Re: NEed Help!!!
i may be wrong, but wouldnt it be something simple like:

if(green button is clicked)
{
counter++;
greenButton.setText("Clicked: " + counter + " times");
}

just make sure you initialize counter as a global variable. sorry if this is not what your looking for lol.
anubis at 2007-11-11 22:37:00 >
# 2 Re: NEed Help!!!
if(green button is clicked)
{
counter++;
greenButton.setText("Clicked: " + counter + " times");
}
Right, but you'll also need to add an action listener. I would probably do this with an anonymous inner class.

greenButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent) {
greenButton.setText("Clicked: " + (++counter) + " times");
}
});
Good luck! :)
destin at 2007-11-11 22:38:11 >
# 3 Re: NEed Help!!!
thanks both !!!!!!!!!!!!!!!!11
kimstanely at 2007-11-11 22:39:04 >
# 4 Re: NEed Help!!!
thanks again for you help, now if i have three pic of horses and i would like to do is when i press the green button there will be a horse in the labels in the front that will move forward the times the button is clicked.

i think it should be some thing like this:

if(mark1.getText() == 1){
for(int i=0;i<greenLabel.length;++i){
System.out.println("sdjkfsdkl");
greenLabel[i].setIcon(new ImageIcon("green.gif"));
}
}

but it doesn't work
thannks
again
kimstanely at 2007-11-11 22:40:03 >