鼠标驱动程序
package v;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class change extends JFrame{
public change(){
MessagePanel p=new MessagePanel();
setLayout(new BorderLayout());
add(p);
}
public static void main(String[] args){
change frame=new change();
frame.setTitle("change message");
frame.setSize(200,100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class MessagePanel extends JPanel{
private String message1="Java is fun";
private String message2="java is powerful";
private int x=20;
private int y=20;
int count;
public MessagePanel(){
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
count=e.getClickCount();
repaint();
}
});
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(count%2==1)
g.drawString(message1, 20,20);
else
g.drawString(message2, 20,20);
}
}
}