I am trying to make a program that opens a panel that contains a JButton. When the user clicks the JButton it is supposed to redraw the Jbutton to a new random position on the panel. There is also a counter on the Jbutton so that i can messure how many times the user has clicked the Jbutton.
My problem is after clicking the JButton it disappears off of the JPanel and can no longer be clicked. Is there anyway i can set the randomization of the Jbutton to fit the panel? Sorry its unorganized and all over the place.
My problem is after clicking the JButton it disappears off of the JPanel and can no longer be clicked. Is there anyway i can set the randomization of the Jbutton to fit the panel? Sorry its unorganized and all over the place.
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ButtonGame extends JFrame implements ActionListener {
final int FRAME_WIDTH = 1600;
final int FRAME_HEIGHT = 200;
final int GAME_PANEL_WIDTH = 1600;
final int GAME_PANEL_HEIGHT = 200;
final int GAME_PANEL_LOCATION_X = 1600;
final int GAME_PANEL_LOCATION_Y = 200;
final int MENU_PANEL_WIDTH = 1600;
final int MENU_PANEL_HEIGHT = 200;
final int MENU_PANEL_LOCATION_X = 0;
final int MENU_PANEL_LOCATION_Y = 120;
final int COUNTER_PANEL_WIDTH = 1600;
final int COUNTER_PANEL_HEIGHT = 200;
final int COUNTER_PANEL_LOCATION_X = 200;
final int COUNTER_PANEL_LOCATION_Y = 0;
public static void main (String[] args)
{
new ButtonGame ();
}
JPanel gamePanel = new JPanel ();
JPanel menuPanel = new JPanel ();
JPanel counterPanel = new JPanel ();
JButton gameButton = new JButton ();
JLabel allTime_Counter = new JLabel ();
JLabel clickTime_Counter = new JLabel ();
JLabel points_Counter = new JLabel ();
Random generator = new Random();
double allTime = 0.0;
double clickTime = 0.0;
public static int points = 0;
public ButtonGame ()
{
this.setTitle ("Button Game");
gameButton.setText ("Press Me!");
allTime_Counter.setText ("Overall Time " + allTime);
clickTime_Counter.setText ("Best Click Time " + clickTime);
points_Counter.setText ("Points " + points);
gameButton.addActionListener (this);
this.setLayout (null);
gamePanel.setLayout (null);
gamePanel.setLocation (GAME_PANEL_LOCATION_X, GAME_PANEL_LOCATION_Y);
menuPanel.setLocation (MENU_PANEL_LOCATION_X, MENU_PANEL_LOCATION_Y);
counterPanel.setLocation (COUNTER_PANEL_LOCATION_X, COUNTER_PANEL_LOCATION_Y);
gamePanel.setSize (GAME_PANEL_LOCATION_X, GAME_PANEL_LOCATION_Y);
menuPanel.setSize (MENU_PANEL_WIDTH, MENU_PANEL_HEIGHT);
counterPanel.setSize (COUNTER_PANEL_WIDTH, COUNTER_PANEL_HEIGHT);
this.setSize (FRAME_WIDTH, FRAME_HEIGHT);
menuPanel.add (gameButton);
//gameButton.setVisible(false);
counterPanel.add (allTime_Counter);
counterPanel.add (clickTime_Counter);
counterPanel.add (points_Counter);
this.add (menuPanel);
this.add (counterPanel);
this.setLocationRelativeTo (null);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.setVisible (true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource () == gameButton) {
points++;
points_Counter.setText ("Points " + points);
gameButton.setLocation(generator.nextInt(this.getHeight()) - gameButton.getHeight(),generator.nextInt(this.getWidth()) - gameButton.getWidth());
add(gameButton);
}
}
}
Comment