Results 1 to 4 of 4
  1. #1
    Interframe is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default How do I make my buttons work in the GUI of this code?

    Hi, so i have a background image and added buttons (with icons on them) on top of the background. Now, I'm trying to figure out how I can make these buttons do something when they are clicked on. Here's the code:

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.JFrame.*; 
    import java.awt.event.*;
    import java.awt.image.*;
    import java.awt.geom.*;
    
    public class ImagePanel extends JPanel {
        private JButton button1; 
        BufferedImage image;
        public ImagePanel(BufferedImage image) 
        {
            this.image = image;
    
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Draw image centered.
            int x = (getWidth() - image.getWidth())/2;
            int y = (getHeight() - image.getHeight())/2;
            g.drawImage(image, x, y, this);
        }
    
        public static void main(String[] args) throws IOException {
            String path = "background.gif";
            BufferedImage image = ImageIO.read(new File(path));
            ImagePanel container = new ImagePanel(image);
            // You'll want to be sure this component is opaque
            // since it is required for contentPanes. Some
            // LAFs may use non-opaque components.
            container.setOpaque(true);
            container.setLayout(new GridBagLayout());
            GridBagConstraints constraints = new GridBagConstraints();
    
            // Add components
    
            JButton button1 = new JButton(new ImageIcon("facebook.gif")); //create an image folder in the same .java package
            //constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.ipady = 0; //reset to default
            constraints.weighty = 0; //request any extra vertical space
            constraints.anchor = GridBagConstraints.PAGE_END; //bottom of space
            constraints.insets = new Insets(10,0,0,0); //top padding
            constraints.gridwidth = 0; //2 columns wide
            constraints.gridy = 1; //third row
            constraints.gridx = 1; //third row
            container.add(button1,constraints);
    
            JButton button2 = new JButton(new ImageIcon("gmail.png")); //create an image folder in the same .java package
            //constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.ipady = 0; //reset to default
            constraints.weighty = 0; //request any extra vertical space
            constraints.anchor = GridBagConstraints.PAGE_END; //bottom of space
            constraints.insets = new Insets(10,0,0,0); //top padding
            constraints.gridwidth = 0; //2 columns wide
            constraints.gridy = 2; //third row
            constraints.gridx = 2; //third row
            container.add(button2,constraints);
    
            JButton button3 = new JButton(new ImageIcon("quiz.gif")); //create an image folder in the same .java package
            //constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.ipady = 0; //reset to default
            constraints.weighty = 0; //request any extra vertical space
            constraints.anchor = GridBagConstraints.PAGE_END; //bottom of space
            constraints.insets = new Insets(10,0,0,0); //top padding
            constraints.gridwidth = 0; //2 columns wide 
            constraints.gridy = 3; //third row
            constraints.gridx = 3; //third row
            container.add(button3,constraints);
    
            JButton button4 = new JButton(new ImageIcon("bigrss.gif")); //create an image folder in the same .java package
            //constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.ipady = 0; //reset to default
            constraints.weighty = 0; //request any extra vertical space
            constraints.anchor = GridBagConstraints.PAGE_END; //bottom of space
            constraints.insets = new Insets(10,0,0,0); //top padding
            constraints.gridwidth = 0; //2 columns wide
            constraints.gridy = 4; //third row
            constraints.gridx = 4; //third row
            container.add(button4,constraints);
    
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(container);
            f.setSize(270,500);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
    }
    Any ideas?
    Last edited by Eranga; 06-14-2011 at 06:12 AM. Reason: code tags added

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You may want to bookmark this link: The Really Big Index and check it anytime you are unsure. Many times you can find the answers there.

Similar Threads

  1. Make all buttons the same size
    By Matthew1246 in forum AWT / Swing
    Replies: 3
    Last Post: 03-22-2011, 01:22 PM
  2. can't make ui:repeat work
    By kaghamdi in forum JavaServer Faces (JSF)
    Replies: 6
    Last Post: 12-31-2010, 11:43 AM
  3. Make it work !
    By PhQ in forum New To Java
    Replies: 6
    Last Post: 09-20-2010, 08:22 AM
  4. How to make buttons vertical
    By ninjalord918 in forum AWT / Swing
    Replies: 2
    Last Post: 07-29-2010, 09:10 AM
  5. JApplet buttons wont work
    By Mark101 in forum New To Java
    Replies: 3
    Last Post: 03-16-2010, 06:53 PM

Posting Permissions

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