0

I have tried this code by setting setComponentZOrder() but it also did not worked please give me some suggestion how can i achieve this goal to make an image slideshow and put a button on it in jframe

import java.awt.Image;
import java.awt.*;
import java.awt.event.ActionListener;
import javafx.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.*;

public class slidemain extends JFrame {

    JLabel jl;
    JButton b;
    Timer tm;
    int x = 0;
    int w;
    int h;
    String[] list = {
        "C:\\Users\\HARITI\\Desktop\\sat.jpg",
        "C:\\Users\\HARITI\\Desktop\\mtab.jpg",
        "C:\\Users\\HARITI\\Desktop\\abc.jpg"
    };

    public slidemain()
    {
        super("java slide show");
        // w = super.getWidth();
        // h = super.getHeight();

        jl = new JLabel();
        b = new JButton();
        //b.setVisible(true);
        super.setComponentZOrder(jl, 0);
        super.setComponentZOrder(b, 1);
        jl.setBounds(0, 100, 1350, 650);
        setImageSize(2);

        tm = new Timer(1500, new ActionListener(){
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
            setImageSize(x);
            x += 1;
            if (x >= list.length)
            {
                x = 0;
            }
        }
        });
        add(jl);
        tm.start();
        setLayout(null);

        getContentPane().setBackground(Color.decode("#bdb67b"));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void setImageSize(int i) {

        ImageIcon icon = new ImageIcon(list[i]);
        Image img = icon.getImage();
        Image newimg = img.getScaledInstance(jl.getWidth(), jl.getHeight(), Image.SCALE_SMOOTH);
        ImageIcon newimc = new ImageIcon(newimg);
        jl.setIcon(newimc);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        // TODO code application logic here
        new slidemain();
    }
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99

1 Answers1

1

This...

super.setComponentZOrder(jl, 0);
super.setComponentZOrder(b, 1);

is going to have no affect if neither of the components have been added to the container yet.

Which brings us to your next problem, you never actually add the button to anything

And event if you did, it wouldn't be displayed, because you're using a null layout

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify.

Maybe have a look at Why is it frowned upon to use a null layout in SWING? for some more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366