简体   繁体   中英

clicking a JPanel inside a JFrame

So I have a JFrame , in which it has a bunch of JPanel which is called venPanel . When I click on a venPanel I want the JFrame to add a new JPanel to the east of the layout (because the JFrame uses the border layout). How can I achieve this in my venPanel class? Currently the mouseClick action listener for the venPanel is implemented as:

@Override
public void mouseClicked(MouseEvent arg0) {
    try {
        GUIVenDetails vendetail = new GUIVenDetails(ven);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Where GUIVenDetails is the JPanel I wanted to add to the east of the JFrame .. I hope the question is clear..

Something like:

JPanel source = (JPanel)event.getSource();
JPanel parent = (JPanel)source.getParent();
parent.add(anotherPanel, BorderLayout.EAST);
parent.revalidate();

或者您可以向使用CardLayout的EAST添加JPanel ,然后通过调用CardLayout方法在该位置交换JPanels

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM