我的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="";
另外,程序最后少了一个大括号 }
程序就可以运行了