我编了个小程序,在窗口上点击鼠标就画一个圈,结果点击没用啊,高手帮一下吧,
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassTest_paint{pub...
import java .awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test_paint
{
public static void main(String[] args)
{
new Test_pain("主窗口");
}
}
class Test_pain extends JFrame
{
int x,y;
public Test_pain(String s)
{
super(s);
setVisible(true);
setBounds(200,200,400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(new Monito());
}
public void paint(Graphics g)
{
Color c=g.getColor();
g.setColor(Color.yellow);
g.fillOval(x,y,20,10);
}
class Monito extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
Test_paint t=(Test_paint)e.getSource();
int x= e.getX();
int y=e.getY();
System.out.println(x);
repaint();
}
}
} 展开
import java.awt.event.*;
import javax.swing.*;
public class Test_paint
{
public static void main(String[] args)
{
new Test_pain("主窗口");
}
}
class Test_pain extends JFrame
{
int x,y;
public Test_pain(String s)
{
super(s);
setVisible(true);
setBounds(200,200,400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(new Monito());
}
public void paint(Graphics g)
{
Color c=g.getColor();
g.setColor(Color.yellow);
g.fillOval(x,y,20,10);
}
class Monito extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
Test_paint t=(Test_paint)e.getSource();
int x= e.getX();
int y=e.getY();
System.out.println(x);
repaint();
}
}
} 展开
展开全部
我看了你的程序,给你改了三处:(见注释)
完整的程序如下:
import java .awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test_paint
{
public static void main(String[] args)
{
new Test_pain("主窗口");
}
}
class Test_pain extends JFrame
{
int x,y;
public Test_pain(String s)
{
super(s);
setVisible(true);
setBounds(200,200,400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(new Monito());
}
public void paint(Graphics g)
{
Color c=g.getColor();
g.setColor(Color.yellow);
g.fillOval(x,y,20,10);
}
class Monito extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
//Test_paint t=(Test_paint)e.getSource(); //这里注释掉一句
x= e.getX(); //这里int x=e.getX();改成 x=e.getX();
y=e.getY(); //这里int y=e.getY();改成 y=e.getY();
System.out.println(x);
repaint();
}
}
}
完整的程序如下:
import java .awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test_paint
{
public static void main(String[] args)
{
new Test_pain("主窗口");
}
}
class Test_pain extends JFrame
{
int x,y;
public Test_pain(String s)
{
super(s);
setVisible(true);
setBounds(200,200,400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(new Monito());
}
public void paint(Graphics g)
{
Color c=g.getColor();
g.setColor(Color.yellow);
g.fillOval(x,y,20,10);
}
class Monito extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
//Test_paint t=(Test_paint)e.getSource(); //这里注释掉一句
x= e.getX(); //这里int x=e.getX();改成 x=e.getX();
y=e.getY(); //这里int y=e.getY();改成 y=e.getY();
System.out.println(x);
repaint();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询