2013-04-16 4 views
0

마우스를 움직이면 패널을 확장하고 단추를 표시하고 싶은데 패널을 벗어난 애 폰 마우스가 원래의 크기로 돌아와야합니다. searchPanelMouseListener를 사용하는 유일한 요소가 아닌 경우가 있고,mouselistener로 패널을 변경하려면 어떻게해야합니까?

JPanel source = (JPanel)e.getSource(); 
//Edit the searchPanel here 

:

public JPanel GUI() 
{ 
    final JPanel totalGUI = new JPanel(); 
    totalGUI.setBackground(Color.blue); 
    totalGUI.setLayout(null); 


    //+++++++++++++++ 
    // - - - - - - PANEL 1! 
    //+++++++++++++++  

    JPanel SearchPanel = new JPanel(); //Create new grid bag layout 
    SearchPanel.setLocation(5, 5); 
    SearchPanel.setSize(420, 120); 
    totalGUI.add(SearchPanel); 

    SearchPanel.addMouseListener(this); 


    return totalGUI; 
} 

public void mouseEntered(MouseEvent e) { 
     System.out.print("Mouse entered"); 
    } 
public void mouseExited(MouseEvent e) { 
     System.out.print("Mouse exited"); 
    } 

private static void createAndShowGUI() 
{ 
    JFrame.setDefaultLookAndFeelDecorated(true); 
    JFrame frame = new JFrame("RWB"); 

    gay3 demo = new gay3(); 
    frame.setContentPane(demo.GUI()); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(false); 
    frame.setSize(650, 500);  
    frame.setVisible(true); 

} 

public static void main(String[] args) 
{ 
    createAndShowGUI(); 
} 
+1

더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. –

+0

'gay3 demo = new gay3();'행복한 코드를 (트리플) 쓰는 것을 보는 것이 좋다. –

+1

'mouseEntered' 및'mouseExited' 메소드에서 e.getSource()를 호출 해 보았습니까? –

답변

0

당신은 mouseEnteredmouseExited 방법 안에이 작업을 수행 할 수 있습니다 : 지금까지 나는 단지 메시지를 인쇄 할 수 있습니다 먼저 소스가 JPanel의 인스턴스인지 확인해야합니다.

Object o = e.getSource(); 
if(o instanceof JPanel) { 
    JPanel source = (JPanel)o; 
    //Edit the searchPanel here 
} 

소스가 JPanel 인 경우에는 필요하지 않습니다.

+0

JPanel에 배치 된 JComponent에 의해 mouse_events가 소비 될 가능성이 있습니다. – mKorbel

관련 문제