Click to See Complete Forum and Search --> : Move button with mouse pressed


Stavros3
May 10th, 2010, 09:58 AM
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).
Tips:
- 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:

Java Code:

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()
{
super("Work6Part1");
btn1 = new JButton("One");
btn2 = new JButton("Two");
btn3 = new JButton("Three");

setLayout(new FlowLayout());

add(btn1);
add(btn2);
add(btn3);

ButtonHandler handler = new ButtonHandler();

btn1.addActionListener(handler);
btn2.addActionListener(handler);
btn3.addActionListener(handler);
}
public static void main(String[] args)
{
W6P1 window = new W6P1();
window.setSize(300, 100);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

//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,

Stavros

keang
May 10th, 2010, 11:07 AM
When posting code please use code tags.

Your main class doesn't need to implement ActionListener as you are providing an inner class that implements ActionListener to handle the events.

You say you are stuck but you have identified that button 1 was pressed so what does the question say you should do next?

Remember, when dealing with a fixed number of items, such as the buttons and labels, it can be helpful to put them in arrays - you can then iterate over the array rather than repeating your code n times.

Stavros3
May 10th, 2010, 12:50 PM
Sorry for the code tags.
I am new to java programming so execuse me if i don't understand something you are saying.

I have posted the question on top. (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.)

Maybe the button 1 i set it's wrong.

How can i set an actionListener like mousePressed on my buttons and how the button is gonna move each time i pressed. That;s my main question

keang
May 10th, 2010, 01:10 PM
How can i set an actionListener like mousePressed on my buttons :confused: You already have set an ActionListener on your buttons. The ActionListener's actionPerformed() method will be called whenever the button is clicked.
how the button is gonna move each time i pressedThe homework does not ask you to move the button, it says to change the text on the button.

Stavros3
May 10th, 2010, 01:28 PM
Oh yes you have right.

So how am i gonna do that?

Something like that?

[code]
public void actionPerformed(ActionEvent ae){
if (ae.getSource()==cmdAdd)
{
??HERE??
}
[code]

keang
May 10th, 2010, 03:59 PM
Your code tags aren't quite correct you've missed the forward slash off the closing tag (see the bottom of this post for an example).

Something like that?I'm not being difficult but wouldn't it have been quicker to try it and see if it works than posting here and waiting for a reply?

Just put a System.out.println("Some Message") type statement where you have HERE and see if/when the message outputs to the console.