发布网友 发布时间:6小时前
共3个回答
热心网友 时间:4分钟前
建议你把Mouse类写在Jback类里面,就可以实现你要的功能
package Test;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class Bai {
public static void main(String args[]) {
new Jback().back();
}
}
class Jback extends JFrame {
static int x, y;
int w = Toolkit.getDefaultToolkit().getScreenSize().width;
int l = Toolkit.getDefaultToolkit().getScreenSize().height;
public void back() {
this.setBounds((w - 500) / 2, (l - 500) / 2, 500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.addMouseListener(new Mouse());
}
public void paint(Graphics g) {
g.fillOval(x, y, 10, 10);
}
public void addqizi(int a, int b) {
x = a;
y = b;
}
class Mouse extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
int X1 = e.getX();
int Y1 = e.getY();
addqizi(X1, Y1);
repaint();
}
}
}
热心网友 时间:5分钟前
你先new一下 再去重写 不符合逻辑 把 重写写到类里 main时再new
热心网友 时间:7分钟前
应该是放的地方不太对, 我帮你重新整理了一下,直接复制运行即可,源文件名改成Test.java
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.event.MouseInputAdapter;
public class Test extends JFrame{
int x,y;
int w=Toolkit.getDefaultToolkit().getScreenSize().width;
int l=Toolkit.getDefaultToolkit().getScreenSize().height;
public Test() {
this.setBounds((w-500)/2,(l-500)/2,500,500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.addMouseListener(new MouseInputAdapter(){
public void mouseClicked(MouseEvent e){
int X1=e.getX();
int Y1=e.getY();
addqizi(X1,Y1);
repaint();
}
});
}
public void paint(Graphics g){
g.fillOval(x, y, 10, 10);
}
public void addqizi(int a,int b){
x=a;
y=b;
}
public static void main(String args[]){
new Test();
}
}
需要鼠标双击,才能看到圆点.