CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2010
    Posts
    3

    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).
    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

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Move button with mouse pressed

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    May 2010
    Posts
    3

    Re: Move button with mouse pressed

    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

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Move button with mouse pressed

    How can i set an actionListener like mousePressed on my buttons
    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 pressed
    The homework does not ask you to move the button, it says to change the text on the button.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    May 2010
    Posts
    3

    Re: Move button with mouse pressed

    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]

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Move button with mouse pressed

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured