单击JLabel以打开新帧

时间:2013-02-06 17:24:00

标签: java swing frame jlabel mouselistener

我正在为我正在编程的游戏设计图形,我想知道当JLabel被克服时是否有一种简单的方法来打开一个框架?

这是否有简单的代码?

enter image description here

5 个答案:

答案 0 :(得分:21)

实现MouseListener接口并使用它mouseClicked方法来处理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);

    }  
}); 

答案 1 :(得分:2)

创建标签并在其中添加点击事件。

这样的事情:

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

答案 2 :(得分:1)

你可以这样做:

label.addMouseListener(new MouseAdapter()   {   

        public void mouseClicked(MouseEvent e)   
        {   
              JPanel j = new JPanel();
              frame.setContentPane(j);
        }   
});

答案 3 :(得分:1)

答案 4 :(得分:0)

1:- Implement your class containing the JLabel with MouseListener interface
2:- add MouseListener to your JLabel 
3:-Override mouseClicked Event in your class
4:- In mouseClicked Even't body add your code to open a new JFrame/Frame .