2016-07-04 43 views
1

在我的課中,我想添加到主要JPanel其他JPanel對象在子類中創建,並捕獲鼠標事件,知道obj如何生成它。 我嘗試通過創建一個子類DoSquare(JPanel擴展),添加監聽器,創建objs並繪製該子類的圖形,但是當我運行程序並單擊其中一個DoSquare obj創建時,e.getSource()總是返回SubObj對象代替點擊的元素。 我如何區分聽衆事件中選定的組件?來自子類的Java陷阱事件obj

這裏我的代碼:

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

public class SubObj extends JPanel { 

Listener listener = new Listener(); 
DoSquare firstQ = new DoSquare(100, 50); 
DoSquare secondQ = new DoSquare(200, 70); 

public static void main(String[] args) { 

    JFrame panel = new JFrame(); 
    SubObj content = new SubObj(); 
    panel.setContentPane(content); 
    panel.setFocusable(true); 
    panel.setSize(400, 400); 
    panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    panel.setVisible(true); 

} 

SubObj() { 
    addMouseListener(listener); 
    addFocusListener(listener); 

} 

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


    firstQ.draw(g, Color.BLACK); 
    secondQ.draw(g, Color.CYAN); 
} 

private class DoSquare extends JPanel { 

    private int coordX; 
    private int coordY; 
    Listener squareLis = new Listener(); 

    DoSquare(int X, int Y) { 
     addMouseListener(squareLis); 
     addFocusListener(squareLis); 
     coordX = X; 
     coordY = Y; 
    } 

    void draw(Graphics g, Color c) { // Draw the square 

     g.setColor(c); 
     g.fillRect(coordX, coordY, 50, 50); 
    } 


} 

public class Listener implements MouseListener, FocusListener { 
    public void mouseClicked(MouseEvent e) {} 
    public void mouseEntered(MouseEvent e) {} 
    public void mouseExited(MouseEvent e) {} 
    public void mousePressed(MouseEvent e) { 
     System.out.println(e.getSource()); 

    } 
    public void mouseReleased(MouseEvent e) {} 

    public void focusGained(FocusEvent e) {} 
    public void focusLost(FocusEvent e) {} 
} 

} 

在此先感謝

AM

回答

2

在你SubObj類的構造函數,你應該做的:

SubObj() { 
    firstQ.addMouseListener(listener); 
    secondQ.addFocusListener(listener); 
} 

並刪除squareLis變量你的DoSquare c姑娘。

UPDATE

public class SubObj extends JPanel { 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       SubObj content = new SubObj(); 
       frame.setContentPane(content); 
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    SubObj() { 
     MouseAdapter mouseAdapter = new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent e) { 
       System.out.println(e.getSource()); 
       System.out.println("click from " + ((TestPanel) e.getSource()).getId()); 
      } 
     }; 

     JPanel testPanel1 = new TestPanel("1"); 
     testPanel1.setPreferredSize(new Dimension(200, 200)); 
     testPanel1.setBackground(Color.BLACK); 
     testPanel1.addMouseListener(mouseAdapter); 
     add(testPanel1); 

     JPanel testPanel2 = new TestPanel("2"); 
     testPanel2.setPreferredSize(new Dimension(100, 100)); 
     testPanel2.setBackground(Color.RED); 
     testPanel2.addMouseListener(mouseAdapter); 
     add(testPanel2); 
    } 

    private class TestPanel extends JPanel { 
     private String id; 

     TestPanel(String id) { 
      this.id = id;  
     } 

     public String getId() { 
      return id; 
     } 
    } 
} 

我上面寫的代碼就相當於你想達到什麼,和它的工作您想要的方式。我不使用你的代碼,因爲我不確定你到底想要什麼。我的代碼用於演示兩個子面板如何在點擊時響應鼠標事件,這是我們關注此問題的焦點。

+0

嗨,謝謝你的回覆,我儘量改變聽衆的建議,但結果不會改變。 – AleMal

+0

@almal它的工作原理,嘗試我更新的代碼。 –

+0

太棒了,它的工作!再次感謝 – AleMal