我想将mouselistener添加到卡面板中,但不起作用...卡面板cp1

public class entry extends JFrame implements MouseListener
{
CardLayout clayout1;
Panel p4,p5,p6,cp1;
String s2[]={"fourth","fifth","sixth"};
boolean x;

entry()
{
setLayout(null);
    cp1=new Panel();
    clayout1=new CardLayout();
    cp1.setLayout(clayout1);
    cp1.setBounds(0,0,1920,500);
    cp1.addMouseListener(this);  //here iam adding mouselistener to cardpanel

    p4=new Panel();
    img4=new ImageIcon("a.jpg");
    l4=new JLabel(img4);
    p4.add(l4);

    p5=new Panel();
    img5=new ImageIcon("b.jpg");
    l5=new JLabel(img5);
    p5.add(l5);

    p6=new Panel();
    img6=new ImageIcon("3.jpg");
    l6=new JLabel(img6);
    p6.add(l6);

    add(cp1);
    cp1.add(p4,"fourth");
    cp1.add(p5,"fifth");
    cp1.add(p6,"sixth");
}
public void mouseExited(MouseEvent e)
    {
         x=true;

    }
    public void mouseEntered(MouseEvent e)
    {
        for(int i=0; i<=2;i++){
            clayout1.show(cp1,s2[i]);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException E) {

                E.printStackTrace();
            }
            if(i==2){
                i=-1;}
            System.out.println("entered");  //this "entered" is not being printed in console
            if(x)
            {
            break;
            }
        }
    }

最佳答案

使用Event Dispatcher Thread - EDT而不是使用负责在屏幕上显示Swing工件的Thread.sleep(...),而是使用javax.swing.Timer,如下面的代码所示:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class CardLayoutExample {

    private JFrame frame;
    private JPanel contentPane;

    private Timer timer;

    private String[] cardNames = {"One", "Two", "Three"};
    private Random random;
    private int count;

    private static final int GAP = 5;

    private ActionListener timerAction = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            count %= cardNames.length;
            CardLayout layout = (CardLayout) contentPane.getLayout();
            layout.show(contentPane, cardNames[count++]);
        }
    };

    public CardLayoutExample() {
        random = new Random();
        count = 0;
    }

    private void displayGUI() {
        frame = new JFrame("CardLayoutExample");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        contentPane = getPanel();
        contentPane.setLayout(new CardLayout(GAP, GAP));
        contentPane.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent me) {
                if (!timer.isRunning()) {
                    timer.start();
                }
            }

            @Override
            public void mouseExited(MouseEvent me) {
                timer.stop();
            }
        });

        for (int i = 0; i < cardNames.length; i++) {
            contentPane.add(getPanel(), cardNames[i]);
        }

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        timer = new Timer(1000, timerAction);
    }

    private JPanel getPanel() {
        JPanel panel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 200);
            }
        };
        panel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        panel.setOpaque(true);
        panel.setBackground(getRandomColor());

        return panel;
    }

    private Color getRandomColor() {
        return new Color(random.nextFloat(), random.nextFloat(),
                            random.nextFloat(), random.nextFloat());
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new CardLayoutExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}


输出:

10-04 23:20