Hello I am new to java and doing simple programs such as information input forms, and i was wondering what the code would be to setLocation a button to move randomly through out the form.
14 7882
Hello I am new to java and doing simple programs such as information input forms, and i was wondering what the code would be to setLocation a button to move randomly through out the form.
You'll need to be more specific. e.g by "form" do you mean JFrame, JPanel or an HTML form?
welll i am working in Jbuilder currently, and i am trying to set this up under the mouseEntered
Why do you want a button to move randomly? If you are new to Java, I'd recommend staying away from GUI programs until you get some programming chops.
i am currently a student in a JAVA programming class, i don't have a particular reason for wanting a button to move randomly across my program other than to be of an annoyance, is it possible to set up the code for the button to move randomly under the mouseEntered code?
i am currently a student in a JAVA programming class, i don't have a particular reason for wanting a button to move randomly across my program other than to be of an annoyance, is it possible to set up the code for the button to move randomly under the mouseEntered code?
I'm not sure what you mean by the "under the mouseEntered code" part but you can move buttons around a bit depending on where the button is. A more helpful exercise is to draw a button(or image) on a Canvas and do some animation to move it around randomly.
this is the code i was talking about
public void mouseEntered(MouseEvent e)
is there any code that can go in this area that when the mouse enters the area of a button that the button moves to a random location.
this is the code i was talking about
public void mouseEntered(MouseEvent e)
is there any code that can go in this area that when the mouse enters the area of a button that the button moves to a random location.
What part is the problem? The random element or the moving?
the random element, i don't know how to set up the random element, can you tell me how?
the random element, i don't know how to set up the random element, can you tell me how?
Have a read at the java.util.Random class.
Thank you for the link, I looked through a lot of it but since i am new to JAVA i am not sure which parts are important to what i am trying to do, can anyone tell me which parts would be important to wanting a button to move randomly?
Thank you for the link, I looked through a lot of it but since i am new to JAVA i am not sure which parts are important to what i am trying to do, can anyone tell me which parts would be important to wanting a button to move randomly?
Well, after carefully studying the API page for Random, would set the button task aside for a moment and write a very trivial program that exercised java.util.Random to make sure I understood that class. This just seems like common sense to me -- understand separate parts in isolation, first.
BigDaddy has the right idea here: break the problem down. I too was trying to solve this problem and this is sadly the first link in google but it provides no answers. Here's what you need to look up in order to understand it: Events, Event Handler, Random number generation, Button creation, location setting, MouseListener.
For everyone else who googles the random jumping button code I present it here: - import java.awt.*;
-
import java.awt.event.*;
-
import javax.swing.*;
-
import java.util.Random;
-
import java.awt.event.MouseListener;
-
import java.awt.event.MouseEvent;
-
-
public class TestButt extends JFrame implements MouseListener
-
{
-
JPanel pane = new JPanel();
-
JButton pressme = new JButton("Press Me");
-
-
TestButt() // the frame constructor
-
{
-
super("Genius Test"); setBounds(100,100,300,200);
-
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
Container con = this.getContentPane(); // inherit main frame
-
con.add(pane);
-
pane.setSize(500, 500);
-
pressme.setMnemonic('P'); // associate hotkey to button
-
pane.add(pressme);
-
pressme.requestFocus();
-
pressme.addMouseListener(this); //tells listener to listen
-
setVisible(true); // make frame visible
-
}
-
-
public void mouseEntered(MouseEvent e)
-
-
{
-
Random r = new Random();
-
pressme.setLocation(r.nextInt(150), r.nextInt(150));
-
}
-
-
public void mouseExited(MouseEvent e) {}
-
public void mousePressed(MouseEvent e) {}
-
public void mouseReleased(MouseEvent e) {}
-
public void mouseClicked(MouseEvent e) {}
-
-
public static void main(String args[]) {new TestButt();}
-
}
I'm relatively new to programming and there's probably a more streamlined way of doing this but it's a start. If you're learning yourself don't just copy it, you need to understand it if you want to use it in your own programs.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Skip Hollowell |
last post by:
I am working on a menu bar for a site, and am using buttons in the bar
(because the customer wants to use accessKeys for each selection,
apparently it is too much work to click on them with a...
|
by: terti |
last post by:
Hi there, newbie here.
appologies if this is OT. (DOM based)
I have a button defined in the html as follows:
<input name="btnPay" value="Continue with payment"...
|
by: Arif Çimen |
last post by:
Hi to everybody,
I have chnged a button text in design mode. But After compiling and
executing the program
the text of the button do not change to new value.
Any Ideas?
Thaks for helps.
|
by: Colin McGuire |
last post by:
Hi - this was posted last weekend and unfortunately not resolved. The
solutions that were posted almost worked but after another 5 days of
working on the code everynight, I am not further ahead....
|
by: Wonder |
last post by:
How can I create or use the msgobx to show a message without a default
button. The user has explicity to click on the button, so the msgbox closes
it.
Thanks,
|
by: JHawk24821 |
last post by:
I am building a simple card matching game where the user picks two buttons on a form out of a group of buttons in an attempt to find a match. I have already taken care of the randomization needed...
|
by: RobAMacAF |
last post by:
I am making it that when they click the form it adds a button. Sometimes however if they click to close to another button then it makes them overlap. Is there any easy way to tell if the buttons over...
|
by: KalariaNitya |
last post by:
Hello All,
in windows application(vb.net) i have one button control
i want to relocate that button on Second button's click event.
means old location is:184, 111
i want new location :59, 375...
|
by: =?Utf-8?B?UmljaA==?= |
last post by:
I placed a button on a form menustrip for the purpose of causing the
horizontal scrollbar of my form to appear so that I can access controls
outside of the form's current view (the controls are...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |