我的java程序只有一个错误!帮忙改改!谢谢急!!! - 爱问答

(爱问答)

我的java程序只有一个错误!帮忙改改!谢谢急!!!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BeerProduct extends JFrame implements ActionListener{
 JLabel jL1,jL2,jL3,jL4,jL5,jL6,jL7;
 JButton jB1;
 JTextField jF1,jF2;
 public BeerProduct(){
  super("啤酒生产流程模拟");
  jL1=new JLabel("瓶盖原始库存:");
  jL1.setBounds(15,5,100,30);
  jF1=new JTextField("100",15);//瓶盖瓶盖原始库存
  jF1.setBounds(120,5,100,30);
  jL2=new JLabel("已生产瓶盖数:");
  jL2.setBounds(15,40,100,30);
  jL4=new JLabel("0");//用于显示已生产的瓶盖数量
  jL4.setBounds(120,40,100,30);
  jL3=new JLabel("已使用瓶盖数:");
  jL3.setBounds(15,75,100,30);
  jL5=new JLabel("0");//用于显示已生产的瓶盖数量
  jL5.setBounds(120,75,100,30);
  jB1=new JButton("开始生产");
  jB1.setBounds(230,5,100,30);
  jB1.addActionListener(this);
  jL6=new JLabel("目前剩余瓶盖数:");
  jL6.setBounds(230,40,100,30);
  jL7=new JLabel("100");//用于显示目前剩余的瓶盖数量
  jL7.setBounds(230,75,100,30);
  Container w1Container=this.getContentPane();
  w1Container.setLayout(null);
  w1Container.add(jL1);
  w1Container.add(jL2);
  w1Container.add(jL3);
  w1Container.add(jL4);
  w1Container.add(jL5);
  w1Container.add(jL6);
  w1Container.add(jL7);
  w1Container.add(jB1);
  w1Container.add(jF1);
  this.setSize(350,150);
  this.setVisible(true);
 }
 public static void main (String[] args) {
  BeerProduct w1=new BeerProduct();
}
    public void actionPerformed(ActionEvent e){
     jL7.setText(jF1.getText());
     ProductCover p=new ProductCover();
     p.start();//生产瓶盖流程启动
     UseCover u=new UseCover();
     u.start();//啤酒罐装流程启动
}
//需要在ProduceBeer类中添加一个成员变量 String lock="",作为锁。
public class ProductCover extends Thread{
 public void run(){
  while(true){
   /*以下程序段需要对库存属性jL7进行改变,在改变的过程中不允许其他程序来改变,所以该程序段加上对象锁*/
   synchronized(lock){
   int nums=Integer.parseInt(jL7.getText());//获得当前的瓶盖数
   try{
    sleep(5000);//生产瓶盖需要5s
   }catch(InterruptedException e){}
     nums++;//将生产的瓶盖放入
     int product=Integer.parseInt(jL4.getText())+1;//生产1个瓶盖
       jL7.setText(nums+"");//更新库存
     jL4.setText(product+"");//更新生产数
     lock.notify();//通知系统,对象锁标志被释放
                    //激活被挂起的程序段。 
   }
   
  }
 }
 
}
public class UseCover extends Thread{
 public void run(){
  while(true){
   synchronized(lock){
    /*以下程序段需要对库存属性jL7进行改变,在改变的过程中不允许其他程序来改变,所以该程序段加上对象锁*/
   int nums=Integer.parseInt(jL7.getText());//获得当前瓶盖数
   if(nums<=0)   //如果库存不够
    try{
     /*挂起该对象锁,释放锁标志供其他程序使用,程序暂停运行*/
     lock.wait();
    }catch(InterruptedException e){}
     else{
      nums--;
      try{
    sleep(3000);
    }catch(InterruptedException e){}
    jL7.setText(nums+"");//更新库存
     int product=Integer.parseInt(jL5.getText())+1;//使用1个瓶盖
     jL5.setText(product+"");//更新消费数
     lock.notify();
    }
   }
  }
 }
}
 

程序中已提示你了

//需要在ProduceBeer类中添加一个成员变量 String lock="",作为锁。

你在

   JTextField jF1,jF2;

后面加一个

   String lock="";

另外,程序最后少了一个大括号 }

程序就可以运行了

我的java程序只有一个错误!帮忙改改!谢谢  急!!!


相关标签:谢谢java

下一篇:我现在网速8m/s,要是换成前兆会不会变快

上一篇:笔记本东芝satellitec600处理器奔腾nb950能把内存升级到6gm吗

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?