0

我希望面板在鼠标悬停时展开并显示按钮和内容,然后鼠标退出面板必须返回顶部原始大小。到目前为止,我只能打印一条消息:

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

1 回答 1

0

您可以在mouseEnteredandmouseExited方法中执行此操作:

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

如果searchPanel不是唯一使用 this 的组件MouseListener,您可能首先必须检查源是否真的是一个实例JPanel

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

如果您知道源是JPanel.

于 2013-04-16T13:36:24.617 回答