작은 문제인 것처럼 보입니다. MouseListener를 Line2D 객체에 추가하려고 시도했지만 작동하지 않습니다. 시도한 방법이 잘못되었거나 다른 방법으로 할 수 있습니다. 내가 여기서 잘못하고있는 것을 알아낼 수있게 도와주세요.그래픽 객체에 MouseListener 추가
public class DrawingLines {
public static void main(String[] args){
LineFrame lf = new LineFrame();
lf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lf.setVisible(true);
}
}
class LineFrame extends JFrame{
public LineFrame(){
setTitle("Line test");
setSize(500, 500);
LinesPanel lp = new LinesPanel();
Container contentpane = getContentPane();
contentpane.add(lp);
}
}
class LinesPanel extends JPanel{
public LinesPanel(){
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Line2D line = new Line2D.Double(105.5, 306.6, 350.8, 4.9);
g2.draw(line);
line.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Line Clicked !");
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
}
}