Как вызвать внутренний класс - Java

Узнай цену своей работы

Формулировка задачи:

Доброго времени суток, как по таймеру вызвать внутренний класс MyActionListener()
Листинг программы
  1. import java.awt.Font;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JPanel;
  8. import javax.swing.Timer;
  9.  
  10. public class Test8
  11. {
  12. public JFrame window;
  13.  
  14. public class myPanel extends JPanel
  15. {
  16. public myPanel()
  17. {
  18. //...
  19. myJlabel();
  20. }
  21.  
  22. public void myJlabel()
  23. {
  24. setBounds(100, 100, 50, 50);
  25. JLabel jlabel = new JLabel(":)");
  26. jlabel.setFont(new Font("Verdana",1,20));
  27. add(jlabel);
  28. System.out.println("Panel");
  29. }
  30. }
  31.  
  32. public class myButton extends JButton
  33. {
  34. public myButton()
  35. {
  36. //...
  37. myButton1();
  38. }
  39.  
  40. public void myButton1()
  41. {
  42. setText("B");
  43. setBounds(100, 200, 50, 50);
  44. setFocusPainted(false);
  45. setVisible(true);
  46. System.out.println("Button");
  47. }
  48. }
  49.  
  50. public class myFrame extends JFrame
  51. {
  52. public myFrame()
  53. {
  54. window = new JFrame();
  55. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  56. window.setBounds(0, 0, 450, 360);
  57. window.setLayout(null);
  58. window.setVisible(true);
  59. window.add(new myPanel());
  60. window.add(new myButton());
  61. }
  62.  
  63. public class MyActionListener implements ActionListener
  64. {
  65. public void actionPerformed(ActionEvent e)
  66. {
  67. System.out.println("Helloy world");
  68. }
  69. }
  70. }
  71.  
  72. public Test8()
  73. {
  74. new myFrame();
  75. myFrame.MyActionListener nestedObject = new myFrame.MyActionListener();
  76. Timer timer = new Timer(5000, nestedObject);
  77. timer.start();
  78. }
  79.  
  80. public static void main(String[] args)
  81. {
  82. new Test8();
  83. }
  84. }

Решение задачи: «Как вызвать внутренний класс»

textual
Листинг программы
  1. import java.awt.Font;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JPanel;
  8. import javax.swing.Timer;
  9.  
  10.  
  11.  
  12. public class Test8
  13. {
  14.     public JFrame window;
  15.     public int _speed = 5;
  16.     public int _point = 100;
  17.    
  18.     public class myPanel extends JPanel
  19.     {
  20.         public myPanel()
  21.         {
  22.             //...
  23.             myJlabel();
  24.         }
  25.        
  26.        
  27.         public void myJlabel()
  28.         {
  29.             setBounds(100, 100, 50, 50);
  30.             JLabel jlabel = new JLabel(":)");
  31.             jlabel.setFont(new Font("Verdana",1,20));
  32.            
  33.             add(jlabel);
  34.            
  35.             System.out.println("Panel");
  36.         }
  37.     }
  38.    
  39.    
  40.     public class myButton extends JButton
  41.     {
  42.         public myButton()
  43.         {
  44.             //...
  45.             myButton1();
  46.         }
  47.        
  48.        
  49.         public void myButton1()
  50.         {
  51.             setText("B");
  52.             setBounds(_point, 200, 50, 50);
  53.             setFocusPainted(false);
  54.             setVisible(true);
  55.            
  56.             //ActionListener actionListener = new ButtonActionListener();
  57.             //addActionListener(actionListener);
  58.            
  59.             Timer timer = new Timer(30, new ButtonActionListener());
  60.             timer.start();
  61.            
  62.             System.out.println("Button");
  63.         }
  64.        
  65.        
  66.         public class ButtonActionListener implements ActionListener
  67.         {
  68.             public void actionPerformed(ActionEvent e)
  69.             {
  70.                 if (_point > 400 || _point < 0)
  71.                 {
  72.                     _speed = _speed * -1;
  73.                 }
  74.                
  75.                 setBounds(_point, 200, 50, 50);
  76.                 _point += _speed;
  77.             }
  78.        }
  79.     }
  80.    
  81.    
  82.     public class myFrame extends JFrame
  83.     {
  84.         public myFrame()
  85.         {
  86.             window = new JFrame();
  87.             window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  88.             window.setBounds(0, 0, 450, 360);
  89.             window.setLayout(null);
  90.             window.setVisible(true);
  91.            
  92.             window.add(new myPanel());
  93.             window.add(new myButton());
  94.         }
  95.     }  
  96.    
  97.        
  98.     public Test8()
  99.     {
  100.         new myFrame();
  101.     }
  102.  
  103.  
  104.     public static void main(String[] args)
  105.     {
  106.         new Test8();
  107.     }
  108. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4.167 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут