2013-11-25 35 views
0

嘿,大家我正在嘗試創建一個有點動態的程序,您可以在其中添加圖形或圖像到JPanel中,然後在添加圖形後選擇並移動它們。問題是,當我點擊特定的JComponent時沒有任何反應。事實上,單擊我創建的用於測試項目的任何組件都會爲所有JComponents返回false。但是,如果我點擊左上角的JComponent邊界內的所有JComponents,就會返回true,即單擊(0,0,50,68)範圍內的區域。使用鼠標點擊JComponent時出現問題

這個想法是,如果我點擊其中一個JComponents,它會設置該特定的JComponent是可移動的,但我無法超越實際選擇特定JComponent的部分。

這裏是我建重現問題的一個基本SSCE:

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

public class SSCE1 extends JPanel { 
private ArrayList<Shape> shapeList = new ArrayList<Shape>(); 

SSCE1() { 
    setLayout(null); 
    /* Debug Stuff */ 
    System.out.println("Debug:"); 
    /* Add The First Shape To The List */ 
    shapeList.add(0, new Shape(100, 100)); 
    add(shapeList.get(0)); 
    shapeList.add(1, new Shape(610, 0)); 
    add(shapeList.get(1)); 
    shapeList.add(2, new Shape(500, 900)); 
    add(shapeList.get(2)); 

    addMouseListener(new MouseAdapter() { 
     public void mouseClicked(MouseEvent e) { 
      for (Shape shape : shapeList) { 
       if (shape.contains(e.getPoint())) { 
        System.out.println("Hello"); 
       } else { 
        System.out.println("Goodbye"); 
       } 
      } 

     } 
    }); 
} 
} 

class Shape extends JComponent { 
int xLocation, yLocation, xBounds1, yBounds1; 

Shape(int xLocation, int yLocation) { 
    this.xLocation = xLocation; 
    this.yLocation = yLocation; 
    this.xBounds1 = 50; 
    this.yBounds1 = 68; 
    setBounds(xLocation, yLocation, xBounds1, yBounds1); 
    setLocation(xLocation, yLocation); 
} 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.setColor(Color.blue); 
    g.fillRect(0, 0, 100, 100); 

} 
} 

class Run { 

public static void main(String[] args) { 
    JFrame main = new JFrame(); 
    SSCE1 p1 = new SSCE1(); 
    main.setSize(new Dimension(1000, 1000)); 
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    main.setLocation(new Point(0, 0)); 
    main.setVisible(true); 
    main.add(p1); 
} 

}

+0

什麼形狀,最近怎麼定義之前將鼠標指向Shape的背景下翻譯? – MadProgrammer

+0

對不起,你的意思是? – Sam

+0

什麼是「形狀」? – MadProgrammer

回答

1

使用未來的MouseListener它的工作原理:根據docscontains

addMouseListener(new MouseAdapter() { 
     public void mouseClicked(MouseEvent e) { 
      for (Shape shape : shapeList) { 
       Point convertPoint = SwingUtilities.convertPoint(SSCE1.this, e.getPoint(), shape); 
       if (shape.contains(convertPoint)) { 
        System.out.println("Hello"); 
       } else { 
        System.out.println("Goodbye"); 
       } 
      } 

     } 
    }); 

的原因是下一個方法the point's x and y coordinates are defined to be relative to the coordinate system of this component.,因爲它適用於(0,0,50,68)。所有你需要從JPanel轉換點ShapeSwingUtilities.convertPoint(...)

+0

謝謝,我從來不會想到這個 – Sam

+0

我現在在工作,我會試試這個誰我回家但如果你不介意這個怎麼用?我想我不明白從源座標系到目標座標系的轉換是如何工作的 – Sam

3

幫助基本上,問題是,Shape期待任何鼠標座標傳遞給它的Shape的上下文中定義的內容。也就是說,頂部的Shape左上角總是0x0

要處理的鼠標點在父容器的範圍內,因此,除非Shape在父容器內位於0x0Shape永遠不會包含在鼠標點。

你需要檢查它

例如...

addMouseListener(new MouseAdapter() { 
    public void mouseClicked(MouseEvent e) { 
     for (Shape shape : shapeList) { 
      Point shapePoint = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), shape); 
      if (shape.contains(shapePoint) { 
       System.out.println("Hello"); 
      } else { 
       System.out.println("Goodbye"); 
      } 
     } 

    } 
}); 
+1

已經推薦:P – alex2410

+1

@ alex2410在你的通過之前回答,抱歉... – MadProgrammer