java在Frame中画一条直线
importjava.awt.event.*;importjava.awt.*;publicclassMainextendsFrame{publicstaticvoidm...
import java.awt.event.*;
import java.awt.*;
public class Main extends Frame {
public static void main(String[] args) {
Frame f = new Frame("Frame");
f.setSize(300, 300);
f.addMouseListener(new Mouse());
f.setVisible(true);
}
}
创建一个主类,然后创建MouseAdapter事件监听类
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
class A extends Frame{
public void Draw(int x,int y,int w,int q){
getGraphics().drawLine(x, y, w, q);
}
}
public class Mouse extends MouseAdapter {
int X;
int Y;
@Override
public void mousePressed(MouseEvent e){
X=e.getX();
Y=e.getY();
}
@Override
public void mouseReleased(MouseEvent e){
A a=new A();
a.Draw(this.X,this.Y,e.getX(),e.getY());
}
}
代码有什么不对的么 为什么窗口出现后单击鼠标没有反映 展开
import java.awt.*;
public class Main extends Frame {
public static void main(String[] args) {
Frame f = new Frame("Frame");
f.setSize(300, 300);
f.addMouseListener(new Mouse());
f.setVisible(true);
}
}
创建一个主类,然后创建MouseAdapter事件监听类
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
class A extends Frame{
public void Draw(int x,int y,int w,int q){
getGraphics().drawLine(x, y, w, q);
}
}
public class Mouse extends MouseAdapter {
int X;
int Y;
@Override
public void mousePressed(MouseEvent e){
X=e.getX();
Y=e.getY();
}
@Override
public void mouseReleased(MouseEvent e){
A a=new A();
a.Draw(this.X,this.Y,e.getX(),e.getY());
}
}
代码有什么不对的么 为什么窗口出现后单击鼠标没有反映 展开
1个回答
展开全部
package test;
import java.awt.event.*;
import java.awt.*;
public class Mains extends Frame {
public Mains(String title){
super(title);
}
public static void main(String[] args) {
Mains f = new Mains("Frame");
f.setSize(300, 300);
f.addMouseListener(new Mousess(f));
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void Draw(int x,int y,int w,int q){
getGraphics().drawLine(x, y, w, q);
}
}
class Mousess extends MouseAdapter {
int X;
int Y;
Mains f;
public Mousess(Mains f){
this.f = f;
}
@Override
public void mousePressed(MouseEvent e) {
X = e.getX();
Y = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
f.Draw(this.X, this.Y, e.getX(), e.getY());
}
}
更多追问追答
追问
new WindowAdapter() {
public void windowClosing(WindowEvent e)
我如果不用内置类的话不能实现么 我的想法是把那个类单独提出来,不用内置类
追答
可以的哈。我只是懒得弄,所以写在里边了。那你就写个类实现WindowListener接口。或者继承WindowAdapter类 实现windowClosing方法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询