Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NewClass extends JPanel implements MouseListener {
- public NewClass() {
- JFrame frame=new JFrame();
- addMouseListener(this);
- Timer t=new Timer();
- TimerTask task=new TimerTask() {
- @Override
- public void run() {
- System.out.println("heloo");
- }
- };
- t.schedule(task, 1000);
- }
- @Override
- public void mouseEntered(MouseEvent me) {
- }
- public static void main(String[]args){
- JFrame frame=new JFrame();
- frame.add(new NewClass());
- frame.setSize(300,300);
- frame.setVisible(true);
- }
- }
- import java.awt.event.*;
- import javax.swing.*;
- import java.awt.*;
- public class TimerDemo extends JFrame {
- private static final long serialVersionUID = 1L;
- private javax.swing.Timer timer;
- public TimerDemo() {
- super("Panel Timer Demo");
- //initialing swing timer
- timer = new javax.swing.Timer(100, getButtonAction());
- JPanel p = new JPanel ();
- p.addMouseListener(new MouseAdapter(){
- @Override
- public void mouseEntered(MouseEvent ev){
- timer.start();
- }
- @Override
- public void mouseExited(MouseEvent ev){
- timer.stop();
- }
- });
- p.setBackground(Color.blue);
- getContentPane().add(p,BorderLayout.CENTER);
- setDefaultCloseOperation(3);
- setLocationRelativeTo(null);
- pack();
- }
- private ActionListener getButtonAction() {
- ActionListener action = new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- System.out.println("Heloo");
- }
- };
- return action;
- }
- public static void main(String... args) {
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new TimerDemo().setVisible(true);
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment