我正在为我正在编程的游戏设计图形,我想知道当JLabel被克服时是否有一种简单的方法来打开一个框架?
这是否有简单的代码?
答案 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)
不要创建新的JFrame,永远不要使用OutOfMemoryException
进行计算,因为Object
永远不会被GC',
要使用多个观看次数CardLayout
请@Andrew Thompson
答案 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 .