java mouselistener 无法实现退出
为什么无法点击exit按扭退出,代码如下:packagecom.FireHole;importjava.awt.Container;importjava.awt.even...
为什么无法点击exit按扭退出,
代码如下:
package com.FireHole;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Start extends JFrame{
public Start() {
this.setTitle("Fire Hole");
this.setLayout(null);
this.setBounds(400, 300, 500, 500);
Container main =getContentPane();
this.setVisible(true);
JButton jb1 = new JButton("Start");
main.add(jb1);
jb1.setBounds(175, 150, 150, 30);
JButton jb2 = new JButton("About");
main.add(jb2);
jb2.setBounds(175, 200, 150, 30);
JButton jb3 = new JButton("Exit");
main.add(jb3);
jb3.setBounds(175, 250, 150, 30);
jb3.addMouseListener(new MouseAdapter() {
public void MouseEvent(MouseAdapter e){
System.exit(0);
}
});
}
public static void main(String[] args) {
new Start();
}
} 展开
代码如下:
package com.FireHole;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Start extends JFrame{
public Start() {
this.setTitle("Fire Hole");
this.setLayout(null);
this.setBounds(400, 300, 500, 500);
Container main =getContentPane();
this.setVisible(true);
JButton jb1 = new JButton("Start");
main.add(jb1);
jb1.setBounds(175, 150, 150, 30);
JButton jb2 = new JButton("About");
main.add(jb2);
jb2.setBounds(175, 200, 150, 30);
JButton jb3 = new JButton("Exit");
main.add(jb3);
jb3.setBounds(175, 250, 150, 30);
jb3.addMouseListener(new MouseAdapter() {
public void MouseEvent(MouseAdapter e){
System.exit(0);
}
});
}
public static void main(String[] args) {
new Start();
}
} 展开
展开全部
应该添加的不是MouseListener,而是ActionListener。jb3.addActionListener();
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
追问
那mouseListener是干嘛用的?
追答
mouseListener是添加对鼠标的动作的监听的。但是你这里是需要对按钮进行监听。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询