14

我正在为我正在编程的游戏设计图形,我想知道是否有一种简单的方法可以在单击 JLabel 时打开框架?

有没有简单的代码?

在此处输入图像描述

4

5 回答 5

34

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

    }  
}); 
于 2013-02-06T17:27:56.960 回答
3

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

像这样的东西:

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);
        }
    });
于 2013-02-06T17:36:28.130 回答
2
于 2013-02-06T17:50:25.590 回答
1

你可以这样做:

label.addMouseListener(new MouseAdapter()   {   

        public void mouseClicked(MouseEvent e)   
        {   
              JPanel j = new JPanel();
              frame.setContentPane(j);
        }   
});
于 2013-02-06T17:30:13.890 回答
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 .
于 2013-02-06T17:49:30.667 回答