Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking a JLabel to open a new frame

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

like image 504
user1992697 Avatar asked Feb 06 '13 17:02

user1992697


People also ask

How do you make a new line in a JLabel?

Surround the string with <html></html> and break the lines with <br/> . just a little correction: use <br /> instead of just <br> ... this is recommended way of doing it (to not miss any closing tags)...


2 Answers

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

    }  
}); 
like image 118
Robin Chander Avatar answered Sep 20 '22 18:09

Robin Chander


create a label and add click event in it .

Something like this :

JLabel click=new JLabel("Click me");

 click.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
           JFrame jf=new JFrame("new one");
        jf.setBackground(Color.BLACK);
        jf.setSize(new Dimension(200,70));
        jf.setVisible(true);
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    });
like image 26
Arpit Avatar answered Sep 22 '22 18:09

Arpit