如何在程序循环中发送事件?

时间:2014-11-30 00:25:43

标签: java swing events event-handling event-dispatch-thread

我在一个项目工作,我需要很好地理解事件驱动的编程是如何工作的。我在过去的几天里看了很多,我明白这是怎么回事,但我无法解决一些问题。

在一个名为Application的类中我得到了这个:

public void Simulate() throws InterruptedException {


    int i = 0;
    while (1 == 1) {

        System.out.println(i);
        Thread.sleep(1000);
        i++;
    }

}

我有这堂课:

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Visual {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Visual window = new Visual();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Visual() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 472, 381);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.NORTH);

        JButton btnStart = new JButton("Start");

        btnStart.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                Application app = new Application();
                try {
                    app.Simulate();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        **JButton print = new JButton("Print");
        print.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("random text");
            }
        });**
        panel.add(print);
        panel.add(btnStart);
    }

}

我想要做的是当我运行这个程序时,我想要推出putton开始在模拟方法中启动循环然后我想按下按钮打印,在计数器静止时打印一些随机文本运行。问题是,在我按下开始按钮后,打印按钮变得不可用,并且不能再按下了。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

对于按钮不使用addMouseListener,使用addActionListener并覆盖actionPerformed方法,以便能够在循环运行时打印您的数字,尝试使用新线程调用您的模拟方法。将Visual类更改为以下示例:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Visual {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Visual window = new Visual();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Visual() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 472, 381);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.NORTH);

        JButton btnStart = new JButton("Start");

        btnStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                Application app = new Application();

                (new Thread(new Runnable() {
                    public void run() {
                        try {
                            app.Simulate();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                })).start();

            }
        });

        JButton print = new JButton("Print");
        print.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("random text");
            }
        });
        panel.add(print);
        panel.add(btnStart);
    }
}