尝试做使用线程的简单程序。但是有些东西我看不懂。这是我的代码:
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MainClass {
public static void main(String[] args) {
new BIGBUTTON();
}}
class GUILCLASS extends Thread {
int i;
public GUILCLASS(){
start();
i=0;
}
@Override
public void run() {
super.run();
while (true){
System.out.println("I did this cycle " +i +" times");
i++;
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class BIGBUTTON extends JFrame {
private JButton buttonForTestingButton;
private JPanel panel1;
GUILCLASS guilclass=new GUILCLASS();
boolean mark1;
public BIGBUTTON() {
panel1=new JPanel();
buttonForTestingButton=new JButton("Button for testing");
mark1=true;
setVisible(true);
setBounds(100,100,100,100);
add(panel1);
add(buttonForTestingButton);
buttonForTestingButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if(mark1){
synchronized (guilclass){
try {
guilclass.wait();
mark1=false;
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
} else {
synchronized (guilclass){
guilclass.notify();
mark1=true;
}
}
}
});
}
}
我想要做的是通过单击按钮使一个线程等待(),直到我再次单击此按钮,但我的按钮只是冻结并且没有其他任何事情发生。寻求帮助。