import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class A extends JFrame implements MouseListener {
private int index = 0;
private String[] msgs = { "java is fun", "java is powerful" };
private JLabel lbl = new JLabel(msgs[index]);
public A() {
this.setTitle("test");
this.setSize(300, 200);
this.add(lbl);
lbl.setFocusable(false);
addMouseListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new A();
}
public void mouseClicked(MouseEvent e) {
index = index == 0 ? 1 : 0;
lbl.setText(msgs[index]);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
只要添加面板的MouseClick事件即可