Как вызвать внутренний класс - Java
Формулировка задачи:
Доброго времени суток, как по таймеру вызвать внутренний класс MyActionListener()
Листинг программы
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- public class Test8
- {
- public JFrame window;
- public class myPanel extends JPanel
- {
- public myPanel()
- {
- //...
- myJlabel();
- }
- public void myJlabel()
- {
- setBounds(100, 100, 50, 50);
- JLabel jlabel = new JLabel(":)");
- jlabel.setFont(new Font("Verdana",1,20));
- add(jlabel);
- System.out.println("Panel");
- }
- }
- public class myButton extends JButton
- {
- public myButton()
- {
- //...
- myButton1();
- }
- public void myButton1()
- {
- setText("B");
- setBounds(100, 200, 50, 50);
- setFocusPainted(false);
- setVisible(true);
- System.out.println("Button");
- }
- }
- public class myFrame extends JFrame
- {
- public myFrame()
- {
- window = new JFrame();
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setBounds(0, 0, 450, 360);
- window.setLayout(null);
- window.setVisible(true);
- window.add(new myPanel());
- window.add(new myButton());
- }
- public class MyActionListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- System.out.println("Helloy world");
- }
- }
- }
- public Test8()
- {
- new myFrame();
- myFrame.MyActionListener nestedObject = new myFrame.MyActionListener();
- Timer timer = new Timer(5000, nestedObject);
- timer.start();
- }
- public static void main(String[] args)
- {
- new Test8();
- }
- }
Решение задачи: «Как вызвать внутренний класс»
textual
Листинг программы
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- public class Test8
- {
- public JFrame window;
- public int _speed = 5;
- public int _point = 100;
- public class myPanel extends JPanel
- {
- public myPanel()
- {
- //...
- myJlabel();
- }
- public void myJlabel()
- {
- setBounds(100, 100, 50, 50);
- JLabel jlabel = new JLabel(":)");
- jlabel.setFont(new Font("Verdana",1,20));
- add(jlabel);
- System.out.println("Panel");
- }
- }
- public class myButton extends JButton
- {
- public myButton()
- {
- //...
- myButton1();
- }
- public void myButton1()
- {
- setText("B");
- setBounds(_point, 200, 50, 50);
- setFocusPainted(false);
- setVisible(true);
- //ActionListener actionListener = new ButtonActionListener();
- //addActionListener(actionListener);
- Timer timer = new Timer(30, new ButtonActionListener());
- timer.start();
- System.out.println("Button");
- }
- public class ButtonActionListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- if (_point > 400 || _point < 0)
- {
- _speed = _speed * -1;
- }
- setBounds(_point, 200, 50, 50);
- _point += _speed;
- }
- }
- }
- public class myFrame extends JFrame
- {
- public myFrame()
- {
- window = new JFrame();
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setBounds(0, 0, 450, 360);
- window.setLayout(null);
- window.setVisible(true);
- window.add(new myPanel());
- window.add(new myButton());
- }
- }
- public Test8()
- {
- new myFrame();
- }
- public static void main(String[] args)
- {
- new Test8();
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д