java 线程控制图片的交替显示
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassgame5extendsJF...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class game5 extends JFrame{
static JLabel label;
String path="E:/pic/";
static Icon p1,p2,p3;
static Boolean start;
public game5() {
this.setTitle("石头剪子布");
this.setSize(600,300);
this.setVisible(true);
Container c=this.getContentPane();
c.setLayout(new GridLayout(1,3));
JButton b1=new JButton("start");
JButton b2=new JButton("over");
label=new JLabel();
c.add(b1);
c.add(b2);
c.add(label);
p1=new ImageIcon(path+"g.gif");
p2=new ImageIcon(path+"o.gif");
p3=new ImageIcon(path+"f.gif");
b1.addActionListener(new start());
b2.addActionListener(new over());
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
class start extends Thread implements ActionListener{
public void actionPerformed(ActionEvent e){
game5.start=true;
Thread t1=new Thread("t1");
Thread t2=new Thread("t2");
Thread t3=new Thread("t3");
t1.start();
t2.start();
t3.start();
}
synchronized void set(){
while(start){
if(this.getName()=="t1"){
game5.label.setIcon(p1);
}
if(this.getName()=="t2"){
game5.label.setIcon(p2);
}
if(this.getName()=="t3"){
game5.label.setIcon(p3);
}
}
}
public void run(){
set();
}
}
class over implements ActionListener{
public void actionPerformed(ActionEvent e){
game5.start=false;
}
}
public static void main(String args[]){
new game5();
}
}
为什么运行后都没有图片显示 展开
import java.awt.event.*;
import javax.swing.*;
public class game5 extends JFrame{
static JLabel label;
String path="E:/pic/";
static Icon p1,p2,p3;
static Boolean start;
public game5() {
this.setTitle("石头剪子布");
this.setSize(600,300);
this.setVisible(true);
Container c=this.getContentPane();
c.setLayout(new GridLayout(1,3));
JButton b1=new JButton("start");
JButton b2=new JButton("over");
label=new JLabel();
c.add(b1);
c.add(b2);
c.add(label);
p1=new ImageIcon(path+"g.gif");
p2=new ImageIcon(path+"o.gif");
p3=new ImageIcon(path+"f.gif");
b1.addActionListener(new start());
b2.addActionListener(new over());
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
class start extends Thread implements ActionListener{
public void actionPerformed(ActionEvent e){
game5.start=true;
Thread t1=new Thread("t1");
Thread t2=new Thread("t2");
Thread t3=new Thread("t3");
t1.start();
t2.start();
t3.start();
}
synchronized void set(){
while(start){
if(this.getName()=="t1"){
game5.label.setIcon(p1);
}
if(this.getName()=="t2"){
game5.label.setIcon(p2);
}
if(this.getName()=="t3"){
game5.label.setIcon(p3);
}
}
}
public void run(){
set();
}
}
class over implements ActionListener{
public void actionPerformed(ActionEvent e){
game5.start=false;
}
}
public static void main(String args[]){
new game5();
}
}
为什么运行后都没有图片显示 展开
3个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class game5 extends JFrame {
static JLabel label;
String path = "E:/pic/";
static Icon p1, p2, p3;
static Boolean start;
public game5() {
this.setTitle("石头剪子布");
this.setSize(600, 300);
this.setVisible(true);
Container c = this.getContentPane();
c.setLayout(new GridLayout(1, 3));
JButton b1 = new JButton("start");
JButton b2 = new JButton("over");
label = new JLabel();
c.add(b1);
c.add(b2);
c.add(label);
p1 = new ImageIcon(path + "g.gif");
p2 = new ImageIcon(path + "o.gif");
p3 = new ImageIcon(path + "f.gif");
b1.addActionListener(new start(""));
b2.addActionListener(new over());
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class start extends Thread implements ActionListener {
// 添加构造函数
public start(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
game5.start = true;
// 问题中这里创建的三个线程是Thread空对象
Thread t1 = new start("t1");
Thread t2 = new start("t2");
Thread t3 = new start("t3");
t1.start();
t2.start();
t3.start();
}
synchronized void set() {
while (start) {
// 使用equals比较对象
if (this.getName().equals("t1")) {
game5.label.setIcon(p1);
}
if (this.getName().equals("t2")) {
game5.label.setIcon(p2);
}
if (this.getName().equals("t3")) {
game5.label.setIcon(p3);
}
}
}
public void run() {
set();
}
}
class over implements ActionListener {
public void actionPerformed(ActionEvent e) {
game5.start = false;
}
}
public static void main(String args[]) {
new game5();
}
}
展开全部
这个使用一个javax.swing.Timer就可以了,没必要使用三个线程。
ImageIcon[] icons = ...; // 3 icons
Timer timer = new Timer(1000, // 1s
new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
label.setIcon(icons[ThreadLocalRandom.current().nextInt(0,3)]);
}
});
timer.setRepeats(true);
在开始和结束的按钮事件处理中启动、停下 timer
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-10-05
展开全部
线程没启动,你的线程对象弄错了。改一下试试。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询