JAVA----我在使用MouseListener接口是写mouseClicked方法时,直接异常
新手求指点!!!!!publicclassMyFrameextendsJFrameimplementsMouseListener{/**CreatesnewformMyF...
新手求指点!!!!!
public class MyFrame extends JFrame implements MouseListener {
/** Creates new form MyFrame */
public MyFrame() {
this.setVisible(true);
this.setTitle("五子");
this.setSize(200, 100);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
System.out.println("宽度为;"+width);
System.out.println("高度为;"+height);
this.setLocation((width-200)/2,(height-200)/2);
this.addMouseListener(this);
public void mouseClicked(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
System.out.println("鼠标点击");
}
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
throw new UnsupportedOperationException("Not supported yet.");
的意思是不是直接异常
所以我写 System.out.println("鼠标点击");
时一定要把每个方法离的throw new UnsupportedOperationException("Not supported yet.");
注掉才不会异常 展开
public class MyFrame extends JFrame implements MouseListener {
/** Creates new form MyFrame */
public MyFrame() {
this.setVisible(true);
this.setTitle("五子");
this.setSize(200, 100);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
System.out.println("宽度为;"+width);
System.out.println("高度为;"+height);
this.setLocation((width-200)/2,(height-200)/2);
this.addMouseListener(this);
public void mouseClicked(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
System.out.println("鼠标点击");
}
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
throw new UnsupportedOperationException("Not supported yet.");
的意思是不是直接异常
所以我写 System.out.println("鼠标点击");
时一定要把每个方法离的throw new UnsupportedOperationException("Not supported yet.");
注掉才不会异常 展开
2个回答
展开全部
如果你只是想在点击窗体的时候弹出提示,那么只需要实现mouseClicked方法就可以了,其他的mouse*方法体为空就可以了 不需要throw异常的。
下面我只是把抛出异常改为打印方法名,你可以看情况修改~~
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class MyFrame extends JFrame implements MouseListener {
/** Creates new form MyFrame */
public MyFrame() {
this.setVisible(true);
this.setTitle("五子");
this.setSize(200, 100);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
System.out.println("宽度为;"+width);
System.out.println("高度为;"+height);
this.setLocation((width-200)/2,(height-200)/2);
this.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
System.out.println("鼠标点击");
}
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent e) {
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e) {
System.out.println("mouseExited");
}
}
下面我只是把抛出异常改为打印方法名,你可以看情况修改~~
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class MyFrame extends JFrame implements MouseListener {
/** Creates new form MyFrame */
public MyFrame() {
this.setVisible(true);
this.setTitle("五子");
this.setSize(200, 100);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
System.out.println("宽度为;"+width);
System.out.println("高度为;"+height);
this.setLocation((width-200)/2,(height-200)/2);
this.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
System.out.println("鼠标点击");
}
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent e) {
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e) {
System.out.println("mouseExited");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询