我在一个项目中工作,我需要很好地理解事件驱动编程的工作原理。在过去的几天里,我读了很多书,我明白这是如何工作的,但我无法弄清楚一些事情。
在一个名为应用程序的类中,我得到了这个:
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);
}
}
我想做的是当我运行这个程序时,我想在模拟方法中启动循环,然后我想按下按钮打印,在计数器仍在运行时打印一些随机文本。问题是在我预处理开始按钮后,打印按钮变得不可用,无法再按下。我该如何解决这个问题?
对于不使用 addMouseListener 的按钮,请使用 addActionListener 并重写 actionPerforming 方法,为了能够在循环运行时打印您的数字,请尝试使用新线程调用您的模拟方法。将视觉类更改为以下示例:
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);
}
}