import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet.*;
public class m1 extends JApplet
{
JButton b1=new JButton("1");
int m_x,m_y;
public void init()
{ setLayout(null);
b1.setSize(50,50);
b2.setSize(50,50);
getContentPane().add(b1);
b1.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
m_x=e.getX();
m_y=e.getY();
b1.setLocation(m_x,m_y);
repaint();
}
});
}
public void paint(Graphics g)
{
}
}
找了参考书和API还是作不出.能帮我改正一下吗?我好新手!谢!
[CODE]import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet.*;
public class m1 extends JApplet {
JButton b1=new JButton("1");
int m_x,m_y;
public void init()
{
setLayout(null);
b1.setSize(50,50);
getContentPane().add(b1);
b1.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
int x=e.getX();
int y=e.getY();
Point p=SwingUtilities.convertPoint(b1, x,y, m1.this);
b1.setLocation(b1.getX()+(p.x-m_x), b1.getY()+(p.y-m_y));
repaint();
m_x=p.x;
m_y=p.y;
}
});
b1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
int x=me.getX();
int y=me.getY();
Point p=SwingUtilities.convertPoint(b1, x,y, m1.this);
m_x=p.x;
m_y=p.y;
}
});
}
}[/CODE]
你自己看看吧
还有,你的代码风格不太好,成员变量一般来说应该声明成private的,编码风格你自己试着改改吧
类的命名也一样,你命一个m1 是什么意义,类名应该首字母大写,并且尽量用有意义的名字