473,651 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does Jbutton disappear after click?

1 New Member
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 5500
jkmyoung
2,057 Recognized Expert Top Contributor
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
2195
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 something in the text field on the dialog window and click the OK button, the dialog window should disappear and the text should be printed on the second window. REAL BEHAVIOR:
1
1729
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 anchor tag link, the resulting page does have a referrer. How can I get a referrer using the above JS?
75
9851
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 is there some deeper underlying reason?
4
9578
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 can explain me why it happens also it'll be gr8. :)
8
6079
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 only the files I want to see appear in the file list box. When I change the filter to one of the other filters, all the files in the list disappear. If I then manually type a filter into the "Filename" textbox, then the files appear and the filter...
0
1112
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 PagePanelDesigner : ParentControlDesigner { public override DesignerVerbCollection Verbs { get { DesignerVerbCollection verbs = new DesignerVerbCollection();
2
7187
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 ToolStrip, each of which has one or more ToolStripButtons.
5
3800
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 try try</a> $('#foo').click(function(){ $('ul',this.parentNode).BlindToggleVertically(500); this.blur(); return false;
0
8802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5612
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.