62,614
社区成员
发帖
与我相关
我的任务
分享
package event;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Try extends JPanel
{
private int x=-1,y=-1;
public Try()
{
this.setLayout(new BorderLayout(5,5));
JPanel p1 = new JPanel(new FlowLayout());
JPanel p2 = new JPanel(new FlowLayout());
p1.addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent event)
{
x=event.getX();
y=event.getY();
Graphics g=getGraphics();
paint(g);
}
}
);
this.add(p1,"Center");
JMenu m1 = new JMenu("编辑");
JMenu m2 = new JMenu("查看");
JMenu m3 = new JMenu("帮助");
JMenuBar bar = new JMenuBar();
bar.add(m1);
bar.add(m2);
bar.add(m3);
p2.add(bar);
this.add(p2,"North");
}
public static void main(String args[])
{
Try a = new Try();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(a);
application.setSize(400,300);
application.setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);//增加此句
g.fillOval(x,y+40,5,5);
}
}