szz958589978 2016-12-22 01:37 采纳率: 0%
浏览 2356

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

{
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条回答 默认 最新

  • szz958589978 2016-12-22 01:38
    关注

    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;
    }
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号