level 2
public class demo9 extends JFrame{
public demo9(){
JPanel panel = new newpanel("ni hao");
setLayout(new BorderLayout());
add(panel);
}
public static void main(String[] args){
demo9 frame = new demo9();
frame.setTitle("demo9");
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class newpanel extends JPanel{
private String message = "ni hao";
private int x = 20;
private int y = 20;
public newpanel(String s){
addMouseMotionListener(new MouseMotionAdapter(){
public void MouseDragged(MouseEvent e){
x = e.getX();
y = e.getY();
repaint();
}
});
}
public void paint(Graphics g){
super.paint(g);
g.drawString(message,x ,y);
}
}
}
2014年09月12日 10点09分