Question

I am dealing with CardLayout. The JPanel I added as a contentpane to my JFrame has a CardLayout, and I want to swap between different panes. I have a working pane with buttons and five other image panes for the tutorial that are to be displayed only if a certain boolean value is true. I mean, every time this boolean is set true, five swaps should be done using next() method. My problem is that, after the first swap, the screen becomes blank. why does this happen?

Second question. I am using a MouseListener to swap, but I would like the program to do it automatically after some time. I tried to use Thread.sleep(5000), but I get a black screen.

This is my code, where card is a class variable in order to use it in the Mouselistener, parent is the working panel, already created, and ImagePanel is a class to create tutorialPanels, which adds to them the MouseListener below. Also, rootPane is a class pane.

card = new CardLayout();
    rootPane = new JPanel(card);
    this.getContentPane().add(rootPane);
    //create panels to add
    ImagePanel inputTutorial = new ImagePanel("backgroundIn.png");
    ImagePanel numericTutorial = new ImagePanel("backgroundNum");
    ImagePanel outputTutorial = new ImagePanel("backgroundOut");
    ImagePanel commandTutorial = new ImagePanel("backgroundCom");
    ImagePanel errorTutorial = new ImagePanel("backgroundErr");
    ImagePanel finalTutorial = new ImagePanel("backgroundFinal");
    //add  the panels
    rootPane.add(parent);
    rootPane.add(inputTutorial);
    rootPane.add(numericTutorial);
    rootPane.add(outputTutorial);
    rootPane.add(commandTutorial);
    rootPane.add(errorTutorial);
    rootPane.add(finalTutorial);
    //set rootPane content panel
    this.getContentPane().add(rootPane);
    //if the boolean is true
    if (firstTime == true) {
        card.next(rootPane);
        //other swaps done by mouselisteners
    }

This is the mouselistener:

//mouse click listener
private class MouseActionListener implements MouseListener {
    public void mousePressed (MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
            card.next(rootPane);
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    }
}

I know that the listener is executed because I checked it. Any help would be appreciated, thank you in advance.

Was it helpful?

Solution

"but I would like the program to do it automatically after some time. I tried to use Thread.sleep(5000)"

Don't use Thread.sleep. Instead use a javax.swing.Timer. You can learn more at How to Use Swing Timers

Here's a simple example, using some of your app format.

enter image description here

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SlideShow {

    public SlideShow() {
        final CardLayout layout = new CardLayout();
        final JPanel mainPanel = createMainPanel(layout);

        Timer timer = new Timer(1000, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                layout.next(mainPanel);
            }
        });
        timer.start();

        JFrame frame = new JFrame();
        frame.add(mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel createMainPanel(CardLayout layout) {
        JPanel panel = new JPanel(layout);
        panel.add(new ImagePanel("mario.png"));
        panel.add(new ImagePanel("bowser.png"));
        panel.add(new ImagePanel("luigi.png"));
        panel.add(new ImagePanel("koopa.png"));
        panel.add(new ImagePanel("princess.png"));
        return panel;
    }

    private class ImagePanel extends JPanel {

        BufferedImage image;

        public ImagePanel(String fileName) {
            try {
                image = ImageIO.read(getClass().getResource("/marioblobs/" + fileName));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, this);
        }

        @Override
        public Dimension getPreferredSize() {
            return image == null ? new Dimension(200, 200) 
                    : new Dimension(image.getWidth(), image.getHeight());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SlideShow();
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top