簡體   English   中英

鼠標偵聽器在卡面板中不起作用

[英]mouse listener not working in card panel

我想將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;  
            }
        }
    }

通過使用Thread.sleep(...) ,而不是在負責將Swing工件呈現到屏幕上的Event Dispatcher Thread - EDT上休眠,應該使用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);
    }
}

輸出:

CardLayoutAnimation

當然,您的“輸入”不會被打印-您在事件處理程序中立即創建了一個無限循環。 刪除它並調用Thread.sleep ,將其打印出來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM