473,419 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Why does Jbutton disappear after click?

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.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.util.Random;
  4.  
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JPanel;
  10.  
  11. public class ButtonGame extends JFrame implements ActionListener {
  12.  
  13.         final int FRAME_WIDTH = 1600;
  14.         final int FRAME_HEIGHT = 200;
  15.  
  16.         final int GAME_PANEL_WIDTH = 1600;
  17.         final int GAME_PANEL_HEIGHT = 200;
  18.         final int GAME_PANEL_LOCATION_X = 1600;
  19.         final int GAME_PANEL_LOCATION_Y = 200;
  20.  
  21.         final int MENU_PANEL_WIDTH = 1600;
  22.         final int MENU_PANEL_HEIGHT = 200;
  23.         final int MENU_PANEL_LOCATION_X = 0;
  24.         final int MENU_PANEL_LOCATION_Y = 120;
  25.  
  26.         final int COUNTER_PANEL_WIDTH = 1600;
  27.         final int COUNTER_PANEL_HEIGHT = 200;
  28.         final int COUNTER_PANEL_LOCATION_X = 200;
  29.         final int COUNTER_PANEL_LOCATION_Y = 0;
  30.  
  31.         public static void main (String[] args)
  32.         {
  33.                 new ButtonGame ();
  34.         }
  35.  
  36.         JPanel gamePanel = new JPanel ();
  37.         JPanel menuPanel = new JPanel ();
  38.         JPanel counterPanel = new JPanel ();
  39.  
  40.         JButton gameButton = new JButton ();
  41.  
  42.         JLabel allTime_Counter = new JLabel ();
  43.         JLabel clickTime_Counter = new JLabel ();
  44.         JLabel points_Counter = new JLabel ();
  45.  
  46.         Random generator = new Random();
  47.  
  48.         double allTime = 0.0;
  49.         double clickTime = 0.0;
  50.         public static int points = 0;
  51.  
  52.         public ButtonGame ()
  53.         {
  54.                 this.setTitle ("Button Game");
  55.  
  56.  
  57.                 gameButton.setText ("Press Me!");
  58.  
  59.  
  60.                 allTime_Counter.setText  ("Overall Time " + allTime);
  61.                 clickTime_Counter.setText   ("Best Click Time " + clickTime);
  62.                 points_Counter.setText    ("Points " + points);
  63.  
  64.  
  65.                 gameButton.addActionListener (this);
  66.  
  67.  
  68.                 this.setLayout (null);
  69.                 gamePanel.setLayout (null);
  70.  
  71.                 gamePanel.setLocation (GAME_PANEL_LOCATION_X, GAME_PANEL_LOCATION_Y);
  72.                 menuPanel.setLocation (MENU_PANEL_LOCATION_X, MENU_PANEL_LOCATION_Y);
  73.                 counterPanel.setLocation (COUNTER_PANEL_LOCATION_X, COUNTER_PANEL_LOCATION_Y);
  74.  
  75.                 gamePanel.setSize (GAME_PANEL_LOCATION_X, GAME_PANEL_LOCATION_Y);
  76.                 menuPanel.setSize (MENU_PANEL_WIDTH, MENU_PANEL_HEIGHT);
  77.                 counterPanel.setSize (COUNTER_PANEL_WIDTH, COUNTER_PANEL_HEIGHT);
  78.                 this.setSize (FRAME_WIDTH, FRAME_HEIGHT);
  79.  
  80.  
  81.                 menuPanel.add (gameButton);
  82.            //gameButton.setVisible(false);
  83.                 counterPanel.add (allTime_Counter);
  84.                 counterPanel.add (clickTime_Counter);
  85.                 counterPanel.add (points_Counter);
  86.                 this.add (menuPanel);
  87.                 this.add (counterPanel);
  88.  
  89.  
  90.                 this.setLocationRelativeTo (null);
  91.                 this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  92.                 this.setVisible (true);
  93.         }
  94.  
  95.         public void actionPerformed (ActionEvent e)
  96.         {
  97.                 if (e.getSource () == gameButton) {
  98.                         points++;
  99.                         points_Counter.setText ("Points " + points);
  100.                         gameButton.setLocation(generator.nextInt(this.getHeight()) - gameButton.getHeight(),generator.nextInt(this.getWidth()) - gameButton.getWidth());
  101.                         add(gameButton);
  102.                 }
  103.  
  104.                 }
  105.         }
May 2 '10 #1
1 5459
jkmyoung
2,057 Expert 2GB
Why do you have add(gameButton); ?
Try removing that.
May 3 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: King W.Wang | last post by:
Hi Java gurus, I've copied the following program from the book "Java by Examples". It compiles well with javac. But it does not run as expected. EXPECTED: It is expected that, when you input...
1
by: brett | last post by:
When I use this code: <select name="status" onChange = "location.href=this.options.value"> <option value="mypage.htm">Select View</option> </select> I don't have a referrer. When I click an...
75
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or...
4
by: Rohan | last post by:
Hi There, If I switch from the Form i have drwan my graphics like Rectangle to some other Window and come back to my graphic window the drawing is disappeared. How should I go abt it? Can if u...
8
by: marcus.kwok | last post by:
I am having a weird problem and I have can't figure out why it is happening. I create an OpenFileDialog and set a filename filter. When the dialog first opens, the filter works correctly, and...
0
by: Bram | last post by:
Hello, I'm designing a PageControl which hosts a number of pages. I added a verb to the designer of the pages in the PageControl to select the parent control, like this: public class...
2
by: Joergen Bech | last post by:
Hope someone has a solution or some suggestions for this. This cannot be right?!? Problem: I have multiple non-modal forms open at the same time. One or more of these forms have a...
5
by: Xu, Qian | last post by:
Hello All, I have some problem by simulating a link click using javascript. The webpage uses a js-library named interface (jQuery like) ------------------------------ <a id="foo" href="#">Try...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.