簡體   English   中英

java中的mouseevent未被觸發

[英]mouseevent in java is not being triggered

我正在使用Java7設計虛擬鋼琴。 我遇到了鼠標點擊事件調用的問題。 JFrame(鋼琴)包含JPanel(PianoKeyboard),后者又包含幾個鋼琴鍵作為JComponent(PianoKey)。 我希望鋼琴類(jframe)知道按下相應圖標時按下了哪個鍵。

因此我做了以下PianoKey有一個MouseListener,其方法是在PianoKeyboard中實現的。 PianoKeyboard有一個KeyListener,其方法是在Piano中實現的。 當從鋼琴鍵接收到事件時,將觸發Key偵聽器事件。

但問題是,當單擊圖標時,鋼琴鍵盤內的鼠標事件監聽器方法都沒有被觸發。

鋼琴班:

public class Piano extends JFrame implements KeyListener {

    public Piano() {
        super();
        this.setTitle("Piano");
        this.setName("Piano");      
        this.setSize(850,200);  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);       
        PianoKeyboard s = new PianoKeyboard(0);
        s.addListener(this);
        this.getContentPane().add(s);
        this.setVisible(true);
    }
}

鋼琴鍵盤類:

public class PianoKeyboard extends JPanel implements MouseListener{
    ArrayList<KeyListener> listeners = new ArrayList<KeyListener>();
    public PianoKeyboard(int tempo){
        super();
        this.tempo = tempo;     
        this.setBackground(Color.WHITE);
        this.setEnabled(true);
        this.setSize(Piano.FRAME_SIZE);
        this.setVisible(true);
        this.setLayout(new OverlayLayout(this));
        createKeyboard();
    }
    public void addListener(KeyListener listener){
        listeners.add(listener);
    }

    private void createKeyboard() {
    ........ code ......
    for (int i = 0; i < 25; i++) {
            PianoKey k = new PianoKey(i+1, tempo);
            k.addMouseListener(this);           
            ..... code .....
            keys.add(k);
        }
    }

   /** the followign methods are also available:
         mouseClicked, mouseExited, mousePressed, mouseReleased
         which contain the following code when neeed:
         for(KeyListener k : listeners){
        k.keyPressed();
    }
   */

   public void paint(Graphics g){
        for(int i = 0; i < keys.size(); i++){
            g.drawImage(keys.get(i).getImage(),positions.get(i),0,54,150,null);
        }
    }
}

鋼琴鍵類:

public class PianoKey extends JComponent {
    public PianoKey(int pitch,int tempo) {
        super();
    ....code....
    this.setImage(ImageIO.read(cl.getResource(IMAGE_DIRECTORY + "/key/white.png")));
}

關鍵聽眾:

public interface KeyListener {
    public void keyPressed();
}

在你的鋼琴課上實現這個:

public void keyTyped(KeyEvent e) {
    System.out.println("am i working?");
}

現在發生了什么?

我認為發生的事情是你沒有實際添加任何GUI的鍵來注冊鼠標點擊。 我看到你將它們添加到看似某種列表的內容中,但這就是全部。 它們“出現”是因為面板正在從它們中繪制圖像,但這與實際上是界面的可見部分不同。

class PianoKey
extends JComponent {

    PianoKey(/* args */) {

        BufferedImage key = ImageIO.read(
            cl.getResource(IMAGE_DIRECTORY + "/key/white.png")
        );

        final Dimension sz = new Dimension(key.getWidth(), key.getHeight());
        setPreferredSize(sz);
        setMinimumSize(sz);
        setMaximumSize(sz);

        setImage(key);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        BufferedImage key = getImage();

        g.drawImage(key, 0, 0, key.getWidth(), key.getHeight(), null);
    }
}

class PianoKeyBoard
extends JPanel
implements MouseListener {

    PianoKeyBoard(/* args */) {

        setLayout(new GridLayout(25, 0));

        for( int i = 1; i <= 25; i++ ) {
            PianoKey key = new PianoKey(/* args */);

            key.addMouseListener(this);

            add(key);
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {
        System.out.println("pressed");
    }
    @Override
    public void mouseClicked(MouseEvent me) {
        System.out.println("clicked");
    }
    @Override
    public void mouseReleased(MouseEvent me) {
        System.out.println("released");
    }
    @Override
    public void mouseEntered(MouseEvent me) {
        System.out.println("entered");
    }
    @Override
    public void mouseExited(MouseEvent me) {
        System.out.println("exited");
    }
}

class Piano
extends JFrame {

    Piano() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setContentPane(new PianoKeyBoard(/* args */));

        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Piano().setVisible(true);
            }
        });
    }
}

這樣的東西會起作用。 在這里,PianoKey繪制圖像,PianoKeyBoard只是將它們添加到自身。 另外,作為旁注,請確保在Swing覆蓋paintComponent ,而不是繪制。

暫無
暫無

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

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