java - 带有 MouseListener 的 CardLayout 不起作用

标签 java swing jframe mouselistener cardlayout

我正在处理CardLayout。我作为内容 Pane 添加到 JFrameJPanel 有一个 CardLayout,我想在不同的 Pane 之间交换。我有一个带有按钮的工作 Pane 和用于教程的其他五个图像 Pane ,仅当某个 boolean 值为 true 时才会显示这些 Pane 。我的意思是,每次这个 boolean 值设置为 true 时,都应该使用 next() 方法完成五次交换。我的问题是,第一次交换后,屏幕变成空白。为什么会发生这种情况?

第二个问题。我正在使用 MouseListener 进行交换,但我希望程序在一段时间后自动执行此操作。我尝试使用 Thread.sleep(5000),但出现黑屏。

这是我的代码,其中card是一个类变量,以便在Mouselistener中使用它,parent是已经创建的工作面板, ImagePanel 是一个用于创建tutorialPanels 的类,它向它们添加了下面的MouseListener。另外,rootPane 是一个类 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
    }

这是鼠标监听器:

//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) {
    }
}

我知道监听器已执行,因为我检查了它。 如有任何帮助,我们将不胜感激,提前谢谢您。

最佳答案

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

不要使用Thread.sleep。而是使用 javax.swing.Timer。您可以了解更多How to Use Swing Timers

这是一个简单的示例,使用了您的一些应用格式。

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();
            }
        });
    }
}

关于java - 带有 MouseListener 的 CardLayout 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23000835/

相关文章:

java - 为什么我得到的是 null 而不是 @Inject-ed 字段?

java - 用java创建一个视频游戏Mario(Nes版本)需要多少知识?

java - JTable 添加行自下而上 - 逐行添加的行应出现在最后一行的底部,但所有行都应底部对齐

java - 如何在 netbeans 中使用 choice.showOpenDialog(parent) ?

java - 如果是,如何让我的程序循环程序,如果否,则退出程序并打印

java - JFrame 在打开时陷入了一些奇怪的内部死锁

用于桌面应用程序的 JavaFX-8 GUI : Are there Look & Feels like in Swing?

java - 如何修复我的文本字段,使其不与旁边的面板重叠?

java - 透明度和 JPanel

java - 在传递变量时分配变量