做了个生产者消费者,生产的时候缓冲池中的商品数就逐个增加的,可是消费的时候缓冲池中的商品不变,可是我的商品数是减少了,就是画不出来,我感觉就是这里有问题public void goodsShow()
{
Graphics g1=panel[1].getGraphics();
panel[1].setBackground(Color.white);
g1.setColor(Color.red);
System.out.println("商品数为:"+flag);
for(int i=0;i<flag;i++)
{
g1.fillRect(40*i, 0, 35, 40);
}
}全部代码如下
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class MainFrame extends JFrame implements ActionListener{
private JPanel[] panel=new JPanel[3];
private DrawThread[] draw;
private JButton button1,button2,button3;
private JLabel label6;
static int flag=0; public MainFrame()
{
setTitle("欢迎来到java世界");
setSize(1300,600);
draw=new DrawThread[3];
draw[0]=new DrawThread(100,80,0);
draw[1]=new DrawThread(400,280,1);
draw[2]=new DrawThread(700,480,2);
Font f1=new Font("宋体",Font.BOLD+Font.ITALIC,25);
Font f2=new Font("宋体",Font.BOLD+Font.HANGING_BASELINE,20);
button1=new JButton("开始生产");
button2=new JButton("开始消费");
button3=new JButton("退出");
JLabel label1=new JLabel("生产者-消费者 演示实验");
JLabel label2=new JLabel("生产者");
JLabel label3=new JLabel("缓冲池");
JLabel label4=new JLabel("消费者");
JLabel label5=new JLabel("制作人 韩旭辉");
JLabel label6=new JLabel("");
for(int i=0;i<panel.length;i++)
panel[i]=new JPanel();
label1.setFont(f1);
label5.setFont(f1);
label2.setFont(f2);
label3.setFont(f2);
label4.setFont(f2);
label6.setFont(f2);
label1.setBounds(900, 40, 400, 30);
label2.setBounds(20, 90, 80, 20);
label3.setBounds(320, 290, 80, 20);
label4.setBounds(620, 490, 80, 20);
label5.setBounds(100, 520, 400, 30);
label6.setBounds(420, 240, 400, 30);
panel[0].setBounds(100, 80, 300, 40);
panel[0].setBackground(Color.white);
panel[1].setBounds(400, 280, 320, 40);
panel[1].setBackground(Color.white);
panel[2].setBounds(700, 480, 300, 40);
panel[2].setBackground(Color.white);
button1.setBounds(520, 85, 100, 30);
button2.setBounds(1120, 485, 100, 30);
button3.setBounds(1160, 120, 100, 30);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(label4);
contentPane.add(label5);
contentPane.add(label6);
contentPane.add(panel[0]);
contentPane.add(panel[1]);
contentPane.add(panel[2]);
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(button3);
}
public void actionPerformed(ActionEvent e)
{
Object s=e.getSource();
if(s==button1)
{
if(button1.getText().equals("开始生产"))
{
draw[0].start();
button1.setText("暂停生产");
}
else if(button1.getText().equals("暂停生产"))
{
draw[0].suspend();
button1.setText("继续生产");
}
else if(button1.getText().equals("继续生产"))
{
draw[0].resume();
button1.setText("暂停生产");
}
}
else if(s==button2)
{
if(button2.getText().equals("开始消费"))
{
draw[2].start();
button2.setText("暂停消费");
}
else if(button2.getText().equals("暂停消费"))
{
draw[2].suspend();
button2.setText("继续消费");
}
else if(button2.getText().equals("继续消费"))
{
draw[2].resume();
button2.setText("暂停消费");
}
}
else if(s==button3)
System.exit(0);
}
class DrawThread extends Thread
{
int x,y,id;
int temp=0;
public DrawThread(int x,int y,int id)
{
this.x=x;
this.y=y;
this.id=id;
}
public void drawMe()
{
Graphics g=panel[id].getGraphics();
panel[id].setBackground(Color.white);
g.setColor(Color.red);
g.fillRect(0, 0, temp, 40);
}
public void goodsShow()
{
Graphics g1=panel[1].getGraphics();
panel[1].setBackground(Color.white);
g1.setColor(Color.red);
System.out.println("商品数为:"+flag);
for(int i=0;i<flag;i++)
{
g1.fillRect(40*i, 0, 35, 40);
}
}
public void run()
{
while(true)
{
try{
drawMe();
temp+=3;
sleep(10);
if(temp>=300)
{
Graphics g=panel[id].getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 300, 40);
temp=0;
if(id==0)
flag++;
else if(id==2)
flag--;
goodsShow();
}
}catch(Exception e){}
}
}
}
public static void main(String[] args)
{
MainFrame mainFrame=new MainFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int WIDTH=screenSize.width;
int HEIGHT=screenSize.height;
mainFrame.setLocation(WIDTH/36,HEIGHT/10);
mainFrame.setVisible(true);
}
}
{
Graphics g1=panel[1].getGraphics();
panel[1].setBackground(Color.white);
g1.setColor(Color.red);
System.out.println("商品数为:"+flag);
for(int i=0;i<flag;i++)
{
g1.fillRect(40*i, 0, 35, 40);
}
}全部代码如下
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class MainFrame extends JFrame implements ActionListener{
private JPanel[] panel=new JPanel[3];
private DrawThread[] draw;
private JButton button1,button2,button3;
private JLabel label6;
static int flag=0; public MainFrame()
{
setTitle("欢迎来到java世界");
setSize(1300,600);
draw=new DrawThread[3];
draw[0]=new DrawThread(100,80,0);
draw[1]=new DrawThread(400,280,1);
draw[2]=new DrawThread(700,480,2);
Font f1=new Font("宋体",Font.BOLD+Font.ITALIC,25);
Font f2=new Font("宋体",Font.BOLD+Font.HANGING_BASELINE,20);
button1=new JButton("开始生产");
button2=new JButton("开始消费");
button3=new JButton("退出");
JLabel label1=new JLabel("生产者-消费者 演示实验");
JLabel label2=new JLabel("生产者");
JLabel label3=new JLabel("缓冲池");
JLabel label4=new JLabel("消费者");
JLabel label5=new JLabel("制作人 韩旭辉");
JLabel label6=new JLabel("");
for(int i=0;i<panel.length;i++)
panel[i]=new JPanel();
label1.setFont(f1);
label5.setFont(f1);
label2.setFont(f2);
label3.setFont(f2);
label4.setFont(f2);
label6.setFont(f2);
label1.setBounds(900, 40, 400, 30);
label2.setBounds(20, 90, 80, 20);
label3.setBounds(320, 290, 80, 20);
label4.setBounds(620, 490, 80, 20);
label5.setBounds(100, 520, 400, 30);
label6.setBounds(420, 240, 400, 30);
panel[0].setBounds(100, 80, 300, 40);
panel[0].setBackground(Color.white);
panel[1].setBounds(400, 280, 320, 40);
panel[1].setBackground(Color.white);
panel[2].setBounds(700, 480, 300, 40);
panel[2].setBackground(Color.white);
button1.setBounds(520, 85, 100, 30);
button2.setBounds(1120, 485, 100, 30);
button3.setBounds(1160, 120, 100, 30);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(label4);
contentPane.add(label5);
contentPane.add(label6);
contentPane.add(panel[0]);
contentPane.add(panel[1]);
contentPane.add(panel[2]);
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(button3);
}
public void actionPerformed(ActionEvent e)
{
Object s=e.getSource();
if(s==button1)
{
if(button1.getText().equals("开始生产"))
{
draw[0].start();
button1.setText("暂停生产");
}
else if(button1.getText().equals("暂停生产"))
{
draw[0].suspend();
button1.setText("继续生产");
}
else if(button1.getText().equals("继续生产"))
{
draw[0].resume();
button1.setText("暂停生产");
}
}
else if(s==button2)
{
if(button2.getText().equals("开始消费"))
{
draw[2].start();
button2.setText("暂停消费");
}
else if(button2.getText().equals("暂停消费"))
{
draw[2].suspend();
button2.setText("继续消费");
}
else if(button2.getText().equals("继续消费"))
{
draw[2].resume();
button2.setText("暂停消费");
}
}
else if(s==button3)
System.exit(0);
}
class DrawThread extends Thread
{
int x,y,id;
int temp=0;
public DrawThread(int x,int y,int id)
{
this.x=x;
this.y=y;
this.id=id;
}
public void drawMe()
{
Graphics g=panel[id].getGraphics();
panel[id].setBackground(Color.white);
g.setColor(Color.red);
g.fillRect(0, 0, temp, 40);
}
public void goodsShow()
{
Graphics g1=panel[1].getGraphics();
panel[1].setBackground(Color.white);
g1.setColor(Color.red);
System.out.println("商品数为:"+flag);
for(int i=0;i<flag;i++)
{
g1.fillRect(40*i, 0, 35, 40);
}
}
public void run()
{
while(true)
{
try{
drawMe();
temp+=3;
sleep(10);
if(temp>=300)
{
Graphics g=panel[id].getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 300, 40);
temp=0;
if(id==0)
flag++;
else if(id==2)
flag--;
goodsShow();
}
}catch(Exception e){}
}
}
}
public static void main(String[] args)
{
MainFrame mainFrame=new MainFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int WIDTH=screenSize.width;
int HEIGHT=screenSize.height;
mainFrame.setLocation(WIDTH/36,HEIGHT/10);
mainFrame.setVisible(true);
}
}
原因是在消费以后,绘制的方块减少了。但最外边的方块没有清除。我这办法未必好,楼主参考一下: public void goodsShow()
{
Graphics g1=panel[1].getGraphics();
panel[1].setBackground(Color.white);
//---------------------------------加上这段
if(id==2)
{
g1.setColor(Color.white);
for(int i=0;i<flag+1;i++) //注意这是i+1.
{
g1.fillRect(40*i, 0, 35, 40);
}
}
//---------------------------------
g1.setColor(Color.red);
System.out.println("商品数为:"+flag);
for(int i=0;i<flag;i++)
{
g1.fillRect(40*i, 0, 35, 40);
}
}
还有最好不要用过时(deprecated)的方法。
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class MainFrame extends JFrame implements ActionListener{
private JPanel[] panel=new JPanel[3];
private DrawThread[] draw;
private listenerThread drawListener;
private JButton button1,button2,button3;
private JLabel label6;
static int flag=0;
static int last=0; //保存上一次的数目 public MainFrame()
{
setTitle("欢迎来到java世界");
setSize(1300,600);
draw=new DrawThread[3];
draw[0]=new DrawThread(100,80,0);//panel[0]控件在画
drawListener=new listenerThread();
draw[2]=new DrawThread(700,480,2);//panel[2]控件在画
Font f1=new Font("宋体",Font.BOLD+Font.ITALIC,25);
Font f2=new Font("宋体",Font.BOLD+Font.HANGING_BASELINE,20);
button1=new JButton("开始生产");
button2=new JButton("开始消费");
button3=new JButton("退出");
JLabel label1=new JLabel("生产者-消费者 演示实验");
JLabel label2=new JLabel("生产者");
JLabel label3=new JLabel("缓冲池");
JLabel label4=new JLabel("消费者");
JLabel label5=new JLabel("制作人 韩旭辉");
JLabel label6=new JLabel("111111111");
for(int i=0;i<panel.length;i++)
panel[i]=new JPanel();
label1.setFont(f1);
label5.setFont(f1);
label2.setFont(f2);
label3.setFont(f2);
label4.setFont(f2);
label6.setFont(f2);
label1.setBounds(900, 40, 400, 30);
label2.setBounds(20, 90, 80, 20);
label3.setBounds(320, 290, 80, 20);
label4.setBounds(620, 490, 80, 20);
label5.setBounds(100, 520, 400, 30);
label6.setBounds(420, 240, 400, 30);
panel[0].setBounds(100, 80, 300, 40);
panel[0].setBackground(Color.white);
panel[1].setBounds(400, 280, 320, 40);
panel[1].setBackground(Color.white);
panel[2].setBounds(700, 480, 300, 40);
panel[2].setBackground(Color.white);
button1.setBounds(520, 85, 100, 30);
button2.setBounds(1120, 485, 100, 30);
button3.setBounds(1160, 120, 100, 30);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(label4);
contentPane.add(label5);
contentPane.add(label6);
contentPane.add(panel[0]);
contentPane.add(panel[1]);
contentPane.add(panel[2]);
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(button3);
drawListener.start();
}
public void actionPerformed(ActionEvent e)
{
Object s=e.getSource();
if(s==button1)
{
if(button1.getText().equals("开始生产"))
{
draw[0].start();
button1.setText("暂停生产");
}
else if(button1.getText().equals("暂停生产"))
{
draw[0].suspend();
button1.setText("继续生产");
}
else if(button1.getText().equals("继续生产"))
{
draw[0].resume();
button1.setText("暂停生产");
}
}
else if(s==button2)
{
if(button2.getText().equals("开始消费"))
{
draw[2].start();
button2.setText("暂停消费");
}
else if(button2.getText().equals("暂停消费"))
{
draw[2].suspend();
button2.setText("继续消费");
}
else if(button2.getText().equals("继续消费"))
{
draw[2].resume();
button2.setText("暂停消费");
}
}
else if(s==button3)
System.exit(0);
}
class DrawThread extends Thread
{
int x,y,id;
int temp=0;
public DrawThread(int x,int y,int id)
{
this.x=x;
this.y=y;
this.id=id;
}
public void drawMe()
{
Graphics g=panel[id].getGraphics();
panel[id].setBackground(Color.white);
g.setColor(Color.red);
g.fillRect(0, 0, temp, 40);
}
public void changeCondition()
{
last=flag;
if(id==0)
flag++;
else if(id==2)
flag--;
}
public void goodsShow()
{
Graphics g1=panel[1].getGraphics();
panel[1].setBackground(Color.white);
System.out.println(last);
System.out.println("商品数为:"+flag);
if(last<=flag)
{ //当上次的数目小于等于这次的数目
g1.setColor(Color.red);
for(int i=0;i<flag;i++)
{
g1.fillRect(40*i, 0, 35, 40);
}
}
else
{ //当上次的数目大于这次的数目
for(int i=0;i<last;i++)
{
if(i<flag)
{
g1.setColor(Color.red); //符合这次生产数目的画红色
}
else
{
g1.setColor(Color.white); //超过本次生产数目,但上次已经画过的部分重新画成背景色
}
g1.fillRect(40*i, 0, 35, 40);
}
}
}
public void run()
{
while(true)
{
try{
drawMe();
temp+=3;
sleep(10);
if(temp>=300)
{
Graphics g=panel[id].getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 300, 40);
temp=0;
changeCondition();
goodsShow();
}
}catch(Exception e){}
}
}
}
class listenerThread extends Thread
{
public void run()
{
try{
label6.setText("缓冲池已满,不能生产!");
System.out.println("我在这");
sleep(50);
if(flag>=8)
{
label6.setText("缓冲池已满,不能生产!");
draw[0].suspend();
button1.setText("继续生产");
}
else if(flag<0)
{
label6.setText("缓冲池已空,不能消费!");
draw[2].suspend();
button2.setText("继续消费");
}
}catch(Exception e){}
}
}
public static void main(String[] args)
{
MainFrame mainFrame=new MainFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int WIDTH=screenSize.width;
int HEIGHT=screenSize.height;
mainFrame.setLocation(WIDTH/36,HEIGHT/10);
mainFrame.setVisible(true);
}
}
public void run()
{
while(true)
{
try{
if(id==0&&flag<8)
{
drawMe();
temp+=3;
sleep(10);
if(temp>=300)
{
Graphics g=panel[id].getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 300, 40);
temp=0;
changeCondition();
goodsShow();
}
}
else if(id==2&&flag>0)
{
drawMe();
temp+=3;
sleep(10);
if(temp>=300)
{
Graphics g=panel[id].getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 300, 40);
temp=0;
changeCondition();
goodsShow();
}
}
}catch(Exception e){}
}//end while
}