Clicking a JLabel to open a new frame

user1992697 picture user1992697 · Feb 6, 2013 · Viewed 44.5k times · Source

I am designing the graphics for a game i am programming, i wanted to know if there is an easy way of opening a frame when a JLabel is cliked?

Is there easy code for this?

enter image description here

Answer

Robin Chander picture Robin Chander · Feb 6, 2013

Implement MouseListener interface and use it mouseClicked method to handle the clicks on the JLabel.

label.addMouseListener(new MouseAdapter()  
{  
    public void mouseClicked(MouseEvent e)  
    {  
       // you can open a new frame here as
       // i have assumed you have declared "frame" as instance variable
       frame = new JFrame("new frame");
       frame.setVisible(true);

    }  
});