Guest User

Untitled

a guest
Apr 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. public class NewClass extends JPanel implements MouseListener {
  2.  
  3. public NewClass() {
  4. JFrame frame=new JFrame();
  5. addMouseListener(this);
  6. Timer t=new Timer();
  7. TimerTask task=new TimerTask() {
  8.  
  9. @Override
  10. public void run() {
  11. System.out.println("heloo");
  12. }
  13. };
  14. t.schedule(task, 1000);
  15.  
  16. }
  17.  
  18.  
  19. @Override
  20. public void mouseEntered(MouseEvent me) {
  21.  
  22. }
  23.  
  24.  
  25. public static void main(String[]args){
  26.  
  27. JFrame frame=new JFrame();
  28. frame.add(new NewClass());
  29. frame.setSize(300,300);
  30. frame.setVisible(true);
  31. }
  32.  
  33. }
  34.  
  35. import java.awt.event.*;
  36. import javax.swing.*;
  37. import java.awt.*;
  38.  
  39. public class TimerDemo extends JFrame {
  40.  
  41. private static final long serialVersionUID = 1L;
  42. private javax.swing.Timer timer;
  43.  
  44. public TimerDemo() {
  45. super("Panel Timer Demo");
  46. //initialing swing timer
  47. timer = new javax.swing.Timer(100, getButtonAction());
  48. JPanel p = new JPanel ();
  49. p.addMouseListener(new MouseAdapter(){
  50. @Override
  51. public void mouseEntered(MouseEvent ev){
  52. timer.start();
  53. }
  54. @Override
  55. public void mouseExited(MouseEvent ev){
  56. timer.stop();
  57. }
  58. });
  59.  
  60. p.setBackground(Color.blue);
  61.  
  62. getContentPane().add(p,BorderLayout.CENTER);
  63. setDefaultCloseOperation(3);
  64. setLocationRelativeTo(null);
  65. pack();
  66. }
  67.  
  68. private ActionListener getButtonAction() {
  69. ActionListener action = new ActionListener() {
  70. @Override
  71. public void actionPerformed(ActionEvent e) {
  72. System.out.println("Heloo");
  73. }
  74. };
  75. return action;
  76. }
  77.  
  78. public static void main(String... args) {
  79. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  80. @Override
  81. public void run() {
  82. new TimerDemo().setVisible(true);
  83. }
  84. });
  85. }
  86. }
Add Comment
Please, Sign In to add comment