Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i put a JButton on an image?

I am trying to fix a JFrame where there will be a background image and on the image JButtons which will do some commands. I try to do it without layout because i want to put small buttons in some specific locations on the JFrame but every time i do it, the background image comes to the front or the JFrame has size equal to the JFrame size. With the following code, the JButton has the same size to JFrame. I have tried to change the size and location of the JButton but nothing. Can you help me please?

here is the code


public final class Test extends JComponent
{
 private Image background;
 private JFrame frame;
 private Dimension dimension;

public Test()
{  
    dimension = new Dimension(15, 15);
    frame = new JFrame("Iphone");
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(this);
    frame.setBounds(641, 0, 344, 655);
    frame.setVisible(true);

    test = displayButton("tigka");
    frame.getContentPane().add(test);
}

public void update(Graphics g)
{
    paint(g);
}


public void paintComponent(Graphics g)
{
    super.paintComponents(g);
    g.drawImage(background, 0, 25, null); // draw background

// label();

test = displayButton("test"); } public JButton displayButton(String name) { JButton button = new JButton(name); button.setSize(100, 100); button.setPreferredSize(dimension); return button; }

like image 748
Eristikos Avatar asked May 06 '11 14:05

Eristikos


People also ask

How to add image icon to JButton in Java Swing?

I n this tutorial, we are going to see how to add image Icon to JButton in Java Swing. To add an icon to a button, use the class Icon, which will allow you to add an image to a button. In the example below, we create a button in which we add an icon with the class Icon.

How to-button on image?

How TO - Button on Image 1 Step 1) Add HTML:#N#Example#N#<div class="container">#N#<img src="img_snow.jpg" alt="Snow">#N#<button class="btn"> Button... More ...

How do I add an icon to a button in jQuery?

To add an icon to a button, use the class Icon, which will allow you to add an image to a button. In the example below, we create a button in which we add an icon with the class Icon. Icon icon = new ImageIcon("C:\image.jpg"); JButton btn = new JButton(icon);

What kind of image can be used as a button image?

The image can be // a gif, jpeg, png and some other type supported. And we also set the // mnemonic character of the button for short-cut key.


2 Answers

You need to change the content pane to get a background for your Frame.

public static void main(String[] args) throws IOException {

    JFrame frame = new JFrame("Test");

    frame.setContentPane(new JPanel() {
        BufferedImage image = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, 300, 300, this);
        }
    });

    frame.add(new JButton("Test Button"));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

Output:

screenshot

like image 106
dacwe Avatar answered Sep 20 '22 18:09

dacwe


Have you tried using a JLabel with HTML in the label? Something like this:

import javax.swing.*;

public class SwingImage1
{
  public static void main( String args[] )
  {
    JFrame  frm = new JFrame( "Swing Image 1" );
    JLabel  lbl = new JLabel( "<html><body><img src=\"http://liv.liviutudor.com/images/liv.gif\"></body></html>" );
    frm.getContentPane().add( lbl );
    frm.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frm.pack();
    frm.setVisible( true );
  }
}

then on top of your label you can add your button?

like image 45
Liv Avatar answered Sep 19 '22 18:09

Liv