How do you make an Image a Button?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: How do you make an Image a Button?

  1. #1
    Join Date
    May 2005
    Posts
    14

    How do you make an Image a Button?

    hi, i finally figured out how to make an image appear on the screen and now i was wondering how i can make the image a button. I want the image to activate one of my methods in my program when it is clicked on. How do i do this?

  2. #2
    Join Date
    Oct 2004
    Posts
    311
    link it to a mouseEvent and a mouseEventListener

  3. #3
    Join Date
    May 2005
    Posts
    14

    ???

    ok....But how do i do that? Im kind of a beginner to java graphics. Some code would be great if u have the time to show me. Cause i dont know how to link it to a mouseEvent and a mouseEventListener

  4. #4
    Join Date
    Mar 2004
    Posts
    635
    How's this?

    Code:
        JButton myButton = makeImageButton(new ImageIcon("normal.gif"), new ImageIcon("hover.gif"));
        
        private JButton makeImageButton(ImageIcon normal, ImageIcon pressed)
        {
            JButton b = new JButton(normal);
            b.setPressedIcon(pressed);        
            b.setMargin(new Insets(0,0,0,0));
            b.setContentAreaFilled(false);
            b.setBorderPainted(false);
            b.setFocusPainted(false);
                
            return b;
        }

  5. #5
    Join Date
    May 2005
    Posts
    14
    im kinda lookin for something more basic if possible.

    however, when i typed in the code, it couldnt find class JButton. what class do i need to import to fix this problem?

  6. #6
    Join Date
    May 2005
    Posts
    5
    If you are trying to use the JButton class type

    import javax.swing.JButton

    at the top of the java document


    Go to this website for all of the java api and all the java classes
    http://java.sun.com/j2se/1.5.0/docs/api/index.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center