在运行界面里加一个暂停按钮,可以在执行的时候随时暂停,求大神

{
private JFrame frm = new JFrame("Demo");
private JButton btnPause = new JButton("Pause");
private JLabel lblTest = new JLabel("0");
private boolean isPaused = false;
private int seconds = 0;

 public void run () {
         while (true) {
                 while (isPaused);
                 seconds ++;
                 lblTest.setText(seconds + "");
                 try {
                         sleep(200);
                 } catch (InterruptedException ie) {}
         }
 }

 public PCmx () {
         frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frm.setResizable(false);
         frm.setLayout(null);
         frm.setSize(150, 200);
         frm.setLocationRelativeTo(null);

         lblTest.setBounds(20, 50, 100, 25);

         btnPause.setBounds(20, 120, 100, 30);
         btnPause.addActionListener(new ActionListener() {
                 public void actionPerformed (ActionEvent ae) {
                         if (isPaused) {
                                 btnPause.setText("Pause");
                                 isPaused = false;
                         } else {
                                 btnPause.setText("Resume");
                                 isPaused = true;
                         }
                 }
         });

         frm.add(lblTest);
         frm.add(btnPause);
         frm.setVisible(true);
         start();
 }

 public static void main (String args[]) {
         new PCmx();
 }

}

3个回答

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//窗口类
class window extends JFrame{

  public JFrame jf;
  public JPanel jp3;
  public ScrollPane sp1,sp2,sp3;
  public Container c;
window(){
  jf=new JFrame();
    jp3=new JPanel();
    sp1=new ScrollPane();
    sp2=new ScrollPane();
    sp3=new ScrollPane();
    c=getContentPane();
    c.setLayout(new GridLayout(2,2,10,10));
    jf.add(c);
    c.add(sp1);
    c.add(sp2);
    c.add(jp3);
    c.add(sp3);

    jf.setSize(400,300);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

//Share 类
class Share{
private int u,jishu=0;
private boolean available=false;
public JTextArea jt=new JTextArea("==========生产消费状态==========\n\n");
//同步方法
public synchronized int get(){
jishu++;
while(available==false){
try{wait();}
catch(InterruptedException e){}
jt.append(jishu+" Share中没有资源,消费者等待……\n");
}
available=false;
notifyAll();
jt.append(jishu+" 正在唤醒生产者生产……\n");
return u;
}
public synchronized void put(int value){
jishu++;
while(available==true){
try{wait();}
catch(InterruptedException e){}
jt.append(jishu+" Share中已有资源,生产者等待……\n");
}
u=value;
available=true;
notifyAll();
jt.append(jishu+" 正在唤醒消费者消费……\n");
}
//互斥方法
public int hget(){
jishu++;
jt.append(jishu+" 消费者 正在消费资源……\n");
return u;
}
public void hput(int value){
jishu++;
jt.append(jishu+" 生产者 正在生产资源……\n");
u=value;
}

}

//生产者类
class Producer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========生产者进程==========\n\n");

public Producer(Share s){
    shared=s;
    }
    public void run(){
        for(int i=1;i<11;i++){
            shared.put(i);
            jieguo[i]=i+" 生产者第"+i+"次生产"+"  生产者的生产数据:"+i+"\n";
            jt.append(jieguo[i]);
            try{
                //sleep((int)(Math.random()*100));
                sleep(1000);
            }catch(InterruptedException e){}

            }
        }

}

class hProducer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========生产者线程==========\n\n");

public hProducer(Share s){
    shared=s;
    }
    public void run(){
        for(int i=1;i<11;i++){
            shared.hput(i);
            jieguo[i]=i+" 生产者第"+i+"次生产"+"  生产者的生产数据:"+i+"\n";
            jt.append(jieguo[i]);
            try{
                //sleep((int)(Math.random()*100));
                sleep(1000);
            }catch(InterruptedException e){}

            }
        }

}

//消费者类
class Consumer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========消费者线程==========\n\n");

public Consumer(Share s){
    shared=s;
    }
    public void run(){
        int value=0;
        for(int i=1;i<11;i++){
            value=shared.get();
            jieguo[i]=i+" 消费者第"+i+"次消费"+"  消费者获得的生产数据:"+value+"\n";
            jt.append(jieguo[i]);
            try{
                //sleep((int)(Math.random()*100));
                sleep(1000);
            }catch(InterruptedException e){}
            }
        }
}

class hConsumer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========消费者线程==========\n\n");

public hConsumer(Share s){
    shared=s;
    }
    public void run(){
        int value=0;
        for(int i=1;i<11;i++){
            value=shared.hget();
            jieguo[i]=i+" 消费者第"+i+"次消费"+"  消费者获得的生产数据:"+value+"\n";
            jt.append(jieguo[i]);
            /*try{
                //sleep((int)(Math.random()*100));
                sleep(1000);
            }catch(InterruptedException e){}*/
            }
        }
}

//程序入口
public class PCmx{

public static void main(String[] args){
    window win=new window();
    BHandler h=new BHandler();
    BHandler1 h1=new BHandler1();
    JButton jb=new JButton("开始同步线程读写");
    JButton jb1=new JButton("开始互斥线程读写");
    JButton jb2=new JButton("暂停");
    win.jf.setTitle("生产者与消费者同步与互斥演示");
    jb.addActionListener(h);
    jb1.addActionListener(h1);

    win.jp3.add(jb);
    win.jp3.add(jb1);
    h.winadd(win.jf,win.sp1,win.sp2,win.sp3,win.jp3,win.jp3);
    h1.winadd(win.jf,win.sp1,win.sp2,win.sp3,win.jp3,win.jp3);
    }
}

//同步按钮事件
class BHandler implements ActionListener{
private ScrollPane sp1,sp2,sp3;
private JPanel jp1,jp2;
private JFrame jf;
JTextArea jt1=new JTextArea();
JTextArea jt2=new JTextArea();
public void actionPerformed(ActionEvent e){
jf.setTitle("生产者与消费者的同步与互斥- 同步演示");
Share s=new Share();
Producer p=new Producer(s);
Consumer c=new Consumer(s);
sp1.add(p.jt);
sp2.add(c.jt);
sp3.add(s.jt);
p.start();
c.start();
}
public void winadd(JFrame jff,ScrollPane s1,ScrollPane s2,ScrollPane s3,JPanel j1,JPanel j2){
jf=jff;
sp1=s1;
sp2=s2;
sp3=s3;
jp1=j1;
jp2=j2;
}
}

//互斥按钮事件
class BHandler1 implements ActionListener{
private ScrollPane sp1,sp2,sp3;
private JPanel jp1,jp2;
private JFrame jf;
JTextArea jt1=new JTextArea();
JTextArea jt2=new JTextArea();
public void actionPerformed(ActionEvent e){
Share s=new Share();
hProducer p=new hProducer(s);
hConsumer c=new hConsumer(s);

sp1.add(p.jt);
sp2.add(c.jt);
sp3.add(s.jt);
jf.setTitle("生产者与消费者的同步与互斥- 互斥演示");
p.start();
c.start();
}
public void winadd(JFrame jff,ScrollPane s1,ScrollPane s2,ScrollPane s3,JPanel j1,JPanel j2){
jf=jff;
sp1=s1;
sp2=s2;
sp3=s3;
jp1=j1;
jp2=j2;
}
}

没看到你的问题,不知你想问什么

那你的意思就是想要点下暂停,线程就停住了,点下重启,线程又开始跑了,线程没有重启一说,只有唤醒,那么你可能就需要2个线程,但你可以换一种思路,线程点暂停,就关闭线程,点重启,又重新开启一个线程(这是我以前做爬虫的思路,是可以达到上述效果的),这样效果其实一样。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问