0

I'm trying to add an Image button to my GUI but all I get is a blank screen. Can anyone show me whats wrong with my code? This is what I have so far.

package fallingStars;

import java.awt.*;
import java.awt.event.*;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI extends JFrame 
{
    private JLabel label;
    private JButton button;
    private JPanel buttonPanel;

    public static void startButton() 
    {
        ImageIcon start = new ImageIcon("Start.png");
        JButton play = new JButton(start);
        play.setBounds(150,100,100,50);
    }

    public static void main(String args[]) 
    {
        GUI gui = new GUI();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(300, 500);
        gui.setVisible(true);
        gui.setResizable(false);
        gui.setTitle("Falling Stars");

        JPanel panel = new JPanel();

        startButton();

    }
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 1
    Where do you add your JButton to your JPanel ? – Mindeater May 15 '14 at 03:12
  • 2
    Your `startButton` method creates an ImageIcon then creates a JButton. Which part do you think is supposed to make it show up? You also create a JPanel `panel` in your main method which you then do nothing with. You also create 3 components as instance variables but then never use them either. – takendarkk May 15 '14 at 03:15
  • `play.setBounds(150,100,100,50);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson May 15 '14 at 03:49
  • 1) `ImageIcon start = new ImageIcon("Start.png");` By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. 2) See also this [chess GUI Q&A](http://stackoverflow.com/q/21142686/418556) for a working example of using images in buttons. – Andrew Thompson May 15 '14 at 03:57

2 Answers2

2

You have to add the JButton to the container using container.add(JButton);

You're going to get a blank screen if you don't actually add it :)

loafy
  • 105
  • 1
  • 11
1

First of all just insert an ImageIcon in JLabel. If you want an image act as button, you have to add MouseListener & implement mouseClicked() as I did.

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;


public class MyFrame extends JFrame{
    private JLabel label;
    private static MyFrame frame;

    public MyFrame(){
        initComponent();
        label.addMouseListener(new MouseListener(){

            @Override
            public void mouseClicked(MouseEvent arg0) {
                JOptionPane.showMessageDialog(frame, "You just clicked the image");         
            }

            @Override
            public void mouseEntered(MouseEvent arg0) {}
            @Override
            public void mouseExited(MouseEvent arg0) {}
            @Override
            public void mousePressed(MouseEvent arg0) {}
            @Override
            public void mouseReleased(MouseEvent arg0) {}

        });
    }

    private void initComponent() {
        this.setSize(400, 400);
        this.setLocationRelativeTo(null);
        label = new JLabel();
        label.setIcon(new ImageIcon(getClass().getResource("res/image.png")));
        add(label);     
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible (true);
    }

    public static void main(String[] args) {
        frame = new MyFrame();
    }
}
Adarsh Singhal
  • 362
  • 1
  • 3
  • 12