/**Tochangethistemplate,chooseTools|Templates*andopenthetemplateintheeditor.*/packageFrame;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava...
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
/**
*
* @author daniel
*/
public class Demo extends JFrame {
JLabel lal_1, lal_2;
private int count = 0;
Timer timer;
public Demo() {
setLayout(null);
lal_1 = new JLabel("JAVA is fun");
lal_2 = new JLabel("JAVA is powerful");
lal_1.setSize(100, 100);
lal_2.setSize(100, 100);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
timer = new Timer(1000, new TimerListener());
timer.start();
}
public void mouseReleased(MouseEvent e) {
timer.stop();
lal_2.setVisible(false);
lal_1.setVisible(false);
}
});
// 创建窗体
this.setTitle("按下鼠标不动信息交替出现..松开消失");
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ((count % 2) == 0) {
lal_2.setVisible(false);
getContentPane().add(lal_1);
lal_1.setLocation(100, 100);
lal_1.setVisible(true);
} else {
lal_1.setVisible(false);
getContentPane().add(lal_2);
lal_2.setLocation(250, 100);
lal_2.setVisible(true);
}
count += 1;
}
}
}
我想让点击鼠标时候...2个标签轮流出现...放开时候就不见了..
但对标签设置了setvisable 但其实还是在那 ...这样一直运行下去很霸占内存
问下有没方法让显示lal_1的内容时候lal_2不是不可见..而是被销毁了..
等下一秒lal_2出现的时候lal_1就销毁了...
希望有人解答...谢谢...
展开