Jump to content

JApplet Game


Sniffy

Recommended Posts

Hello, I've been working on an applet that is supposed to be a "Catch the Clown" style game. When the user clicks on the button, a new random location is set and the score is incremented. Anyways, I can't get the setLocation() method to work for my JButton, any help?It might have something to do with the FlowLayout object because FlowLayout sorts Components in rows, but I don't know what other alternatives I can use. I just need to get to know JComponents and JApplets (methods) more so help would be appreciated.

/** * @(#)appletGame.java * * * @author  * @version 1.00 2007/3/3 */import java.awt.*;import java.awt.event.*;import javax.swing.*;public class appletGame extends JApplet implements ActionListener {	Container con = getContentPane();	JButton press = new JButton("Press");	int score = 0;	JLabel dispScore = new JLabel("Score:" + score);		public void init()	{		con.add(dispScore);		con.add(press);		press.setLocation(10, 10);		press.setLocation(100, 100);		press.addActionListener(this);		con.setLayout(new FlowLayout());	}	public void actionPerformed(ActionEvent e)	{		int locationX = (int) Math.round(Math.random()*300);		int locationY = (int) Math.round(Math.random()*300);		score++;		dispScore.setText("Score: " + score);		press.setLocation(locationX, locationY);			}		}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...