Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Move button with mouse pressed

  1. #1
    Junior Member
    Join Date
    May 2010
    Thanked 0 Times in 0 Posts

    Default Move button with mouse pressed

    Hello guys.
    I'm new here.
    I have a problem with my java programming and i'm wondering if you can help.

    Write a class (called W6P1.java) that will create the following interface:
    3 buttons (one, two, three).
    - Set size to (300,100);
    - Use FlowLayout as the layout manager

    Add action listeners to the left button <one> and the right button <three> and add code to rotate the labels of the buttons every time a button is pressed. If the left button is pressed the labels rotate to the left (from <one>, <two>, <three> they will become <two>, <three>, <one>, press it again and they become <three>, <one>, <two>; press it for a third time and they become as before <one> <two> <three>.
    Similar operation is required when the right button (<three>) is clicked but this time the rotation will take place right-wise.

    I have done this:
    import javax.swing.*; 		//for everything that is Jxxx
    import java.awt.*; 			//for FlowLayout
    import java.awt.event.*;	//for the actionlistener
    public class W6P1 extends JFrame implements ActionListener
    	private JButton btn1;
    	private JButton btn2;
    	private JButton btn3;
    	public W6P1()
    		btn1 = new JButton("One");
    		btn2 = new JButton("Two");
    		btn3 = new JButton("Three");
    		btn1 = new FlowLayout(FlowLayout.RIGHT);
    		setLayout(new FlowLayout());
    		ButtonHandler handler = new ButtonHandler();
    	public static void main(String[] args)
    		W6P1 window = new W6P1();
    		window.setSize(300, 100);
    	//create the inner class
    	private class ButtonHandler implements ActionListener
    		public void actionPerformed(ActionEvent e)
    			if (e.getSource() == btn1)

    I'm really confused what to do next.
    Anyone can help me?

    Thanks in advance,

    Last edited by Json; May 10th, 2010 at 09:15 AM. Reason: Please use code tags.

Similar Threads

  1. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  2. mouse Released no work!!
    By frenkelor in forum AWT / Java Swing
    Replies: 1
    Last Post: January 23rd, 2010, 06:41 AM
  3. How to move an image (or how to delete one)
    By User in forum AWT / Java Swing
    Replies: 3
    Last Post: December 17th, 2009, 11:25 AM
  4. Want to move my button away from center.
    By Fendaril in forum AWT / Java Swing
    Replies: 2
    Last Post: November 6th, 2009, 10:05 PM
  5. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM