-1

Hi I am trying to make java desktop application whee I am having a JLabel I am shuffling image I want to set image size fit on that JLabel

Here is my code

public class ImageShuffle extends JPanel {

    private List<Icon> list = new ArrayList<Icon>();
    private JLabel label = new JLabel();
    private Timer timer = new Timer(1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            update();
        }
    });

    public ImageShuffle() {
        this.setLayout(new GridLayout(1, 0));
          list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\e.jpg"));
    list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\d.jpg"));
    list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png"));
      list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\f.jpg"));
            list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\l.jpg"));
        label.setIcon(UIManager.getIcon("OptionPane.informationIcon"));

        timer.start();
    }

    private void update() {
        Collections.shuffle(list);
        label.setIcon(list.get(0));
    }

    private void display() {
        JFrame f = new JFrame("ImageShuffle");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.add(label);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

How can i achieve this

Thanks in advance

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3456343
  • 252
  • 3
  • 7
  • 21

2 Answers2

1

Check out Darryl's Stretch Icon which will grow/shrink the Icon based on the space available in the label.

camickr
  • 321,443
  • 19
  • 166
  • 288
-1
private void display() {
    JFrame f = new JFrame("ImageShuffle");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(this);
    f.add(label);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setSize(x,y)
    f.setVisible(true);

X is ur width and y ur height!

If you write for example

  f.setSize(800,600);

It should set the size when you insert it like I did!

If you want to set the JLabel size it is like this:

 panelName.setSize(x,y);
 panelName.setVisible(true);