各位高手:题目是这样的,.在一个窗口中,在四个位置循环显示四种不同颜色的正方形,当鼠标点击时,停止循环显示,再次点击,恢复显示
我写的程序只能做到点击是其循环显示,但不能使其停止循环,请问我哪里出错了?import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class CycleRecTest extends JFrame //implements ActionListener
{
//static int times = 1;
// final CycleRec pane;
JButton playButton;
JButton stopButton; public CycleRecTest()
{
setTitle("图形循环显示");
setSize(470, 470);
final CycleRec pane = new CycleRec();
//JButton playButton = new JButton("Play");
//playButton.addActionListener(this);
//JButton stopButton = new JButton("Stop");
//stopButton.addActionListener(this); Container content = getContentPane();
content.setLayout(new BorderLayout());
//JPanel pane1 = new JPanel();
//pane1.add(playButton);
//pane1.add(stopButton);
//content.add(pane1, "South");
content.add(pane, "Center");
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (pane.isPlay())
pane.stop();
else
pane.play();
}
});
} public static void main(String[] args)
{
JFrame app = new CycleRecTest();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}class CycleRec extends JPanel implements Runnable
{
private Thread show;
private Color blackCor = Color.black;
private Color blueCor = Color.blue;
private Color cyanCor = Color.cyan;
private Color greenCor = Color.green; int imgID = 0;
boolean bPlay = true;
public CycleRec()
{
} public void paintComponent(Graphics g)
{
if (imgID == 0)
{
fillR(g, 100, 100, 100, blackCor);
fillR(g, 200, 100, 100, blueCor);
fillR(g, 100, 200, 100, cyanCor);
fillR(g, 200, 200, 100, greenCor);
}
else if (imgID == 1)
{
fillR(g, 100, 100, 100, cyanCor);
fillR(g, 200, 100, 100, blackCor);
fillR(g, 100, 200, 100, greenCor);
fillR(g, 200, 200, 100, blueCor);
}
else if (imgID == 2)
{
fillR(g, 100, 100, 100, greenCor);
fillR(g, 200, 100, 100, cyanCor);
fillR(g, 100, 200, 100, blueCor);
fillR(g, 200, 200, 100, blackCor);
}
else if (imgID == 3)
{
fillR(g, 100, 100, 100, blueCor);
fillR(g, 200, 100, 100, greenCor);
fillR(g, 100, 200, 100, blackCor);
fillR(g, 200, 200, 100, cyanCor);
}
} public static void fillR(Graphics g, int x, int y, int length, Color color)
{
g.setColor(color);
g.fillRect(x, y, length, length);
} public void run()
{
int showpic = 0;
while (bPlay = true)
{
imgID = showpic;
repaint();
pause(1000);
showpic++;
if (showpic > 3)
showpic = 0;
}
} public void pause(int time)
{
try
{
Thread.sleep(time);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
} public void play()
{
if (show == null)
{
show = new Thread(this);
bPlay = true;
show.start();
}
//show = new Thread(this);
// bPlay = true;
//show.start();
} public void stop()
{
//if (show != null)
// show.destroy();
if (show != null)
{
show = null;
bPlay = false;
}
} public boolean isPlay()
{
if (show != null)
return true;
else
return false;
}
}
我写的程序只能做到点击是其循环显示,但不能使其停止循环,请问我哪里出错了?import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class CycleRecTest extends JFrame //implements ActionListener
{
//static int times = 1;
// final CycleRec pane;
JButton playButton;
JButton stopButton; public CycleRecTest()
{
setTitle("图形循环显示");
setSize(470, 470);
final CycleRec pane = new CycleRec();
//JButton playButton = new JButton("Play");
//playButton.addActionListener(this);
//JButton stopButton = new JButton("Stop");
//stopButton.addActionListener(this); Container content = getContentPane();
content.setLayout(new BorderLayout());
//JPanel pane1 = new JPanel();
//pane1.add(playButton);
//pane1.add(stopButton);
//content.add(pane1, "South");
content.add(pane, "Center");
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (pane.isPlay())
pane.stop();
else
pane.play();
}
});
} public static void main(String[] args)
{
JFrame app = new CycleRecTest();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}class CycleRec extends JPanel implements Runnable
{
private Thread show;
private Color blackCor = Color.black;
private Color blueCor = Color.blue;
private Color cyanCor = Color.cyan;
private Color greenCor = Color.green; int imgID = 0;
boolean bPlay = true;
public CycleRec()
{
} public void paintComponent(Graphics g)
{
if (imgID == 0)
{
fillR(g, 100, 100, 100, blackCor);
fillR(g, 200, 100, 100, blueCor);
fillR(g, 100, 200, 100, cyanCor);
fillR(g, 200, 200, 100, greenCor);
}
else if (imgID == 1)
{
fillR(g, 100, 100, 100, cyanCor);
fillR(g, 200, 100, 100, blackCor);
fillR(g, 100, 200, 100, greenCor);
fillR(g, 200, 200, 100, blueCor);
}
else if (imgID == 2)
{
fillR(g, 100, 100, 100, greenCor);
fillR(g, 200, 100, 100, cyanCor);
fillR(g, 100, 200, 100, blueCor);
fillR(g, 200, 200, 100, blackCor);
}
else if (imgID == 3)
{
fillR(g, 100, 100, 100, blueCor);
fillR(g, 200, 100, 100, greenCor);
fillR(g, 100, 200, 100, blackCor);
fillR(g, 200, 200, 100, cyanCor);
}
} public static void fillR(Graphics g, int x, int y, int length, Color color)
{
g.setColor(color);
g.fillRect(x, y, length, length);
} public void run()
{
int showpic = 0;
while (bPlay = true)
{
imgID = showpic;
repaint();
pause(1000);
showpic++;
if (showpic > 3)
showpic = 0;
}
} public void pause(int time)
{
try
{
Thread.sleep(time);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
} public void play()
{
if (show == null)
{
show = new Thread(this);
bPlay = true;
show.start();
}
//show = new Thread(this);
// bPlay = true;
//show.start();
} public void stop()
{
//if (show != null)
// show.destroy();
if (show != null)
{
show = null;
bPlay = false;
}
} public boolean isPlay()
{
if (show != null)
return true;
else
return false;
}
}
while(bPlay=true)
改成while(bPlay);
应该明白什么意思把!