簡體   English   中英

如何使用mouselistener更改面板?

[英]How do I change a panel with mouselistener?

我希望面板在鼠標懸停時展開並顯示按鈕和其他內容,然后退出面板的鼠標必須返回最大的原始大小。 到目前為止,我只能打印一條消息:

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();
}

您可以在mouseEnteredmouseExited方法內執行此操作:

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

如果searchPanel不是使用此MouseListener的唯一組件,則可能首先必須檢查源是否確實是JPanel的實例:

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

如果您知道源是JPanel則沒有必要。

暫無
暫無

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

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