点击面板变换面板上显示的字符
/**
*
*/
package Lyt2;
import java.awt.BorderLayout;
import javax.swing.JFrame;
/**
* @author Administrator
*
*/
public class lyt extends JFrame{
public lyt()
{
pressShow l=new pressShow();
setLayout(new BorderLayout());
add(l);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
lyt frame=new lyt();
frame.setTitle("change");
frame.setSize(700,800);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
*
*/
package Lyt2;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
/**
* @author Administrator
*
*/
public class pressShow extends JPanel{
/**
* @param args
*/
int i=0;
private String message0=new String("Java is fun");
private String message1=new String("Java is powerful");
private String message2=new String("");
public pressShow()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{ i++;
message2=message0;
repaint();
}
});
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if(i%2==0)
g.drawString(message0, 40, 40);
else
g.drawString(message1, 40, 40);
}
}