ChanKian 2017-09-08 12:21 采纳率: 0%
浏览 2852

JAVA 如何暂停一个正在运行的线程,再启动另一个已经暂停的线程。

package MahJong;

import java.awt.Image;
import java.util.Iterator;

import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Card {
int i;
String img;
Card(int i,String img){
this.i = i;
this.img = img;
}
}
public class Frame extends JFrame{
Frame(){
new JFrame("MahJong Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(880, 660);
setLocation( 0, 0);
setVisible(true);
setResizable(false);
Container container = this.getContentPane();
container.setLayout(null);
ImageIcon background =new ImageIcon("MahJong.jpg");
background = new ImageIcon(background.getImage().getScaledInstance(880, 660, Image.SCALE_SMOOTH));
JLabel label =new JLabel(background);
label.setBounds(0, 0, this.getWidth(), this.getHeight());
JPanel mainPanel = (JPanel)this.getContentPane();
mainPanel.setOpaque(false);
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
repaint();
setVisible(true);
}
}
public class Button {
Card card ;
JButton btn = new JButton();
Button(Frame frame,Card card,int x,int y){
this.card = card;
String ima = this.card.img;
System.out.println(ima);
ImageIcon img = new ImageIcon(ima);
img = new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
btn.setLocation(x, y);
btn.setSize(45, 50);
btn.setIcon(img);
frame.add(btn);
frame.repaint();
}
Button(Frame frame,int x,int y){
btn.setLocation(x, y);
btn.setSize(45, 50);
btn.setContentAreaFilled(false);
frame.add(btn);
frame.repaint();
}
}
public class tools {
static int time=0,TIME = 0;
static void rank(Button[] btn,Frame Frame){
int i,j;Card card = null;
for(i=0;i for(j=0;j if(btn[j].card.i>btn[j+1].card.i){
card = btn[j].card;
btn[j].card=btn[j+1].card;
btn[j+1].card=card;
} } }

    for(i=0;i<13;i++){
        String ima = btn[i].card.img;
        ImageIcon img = new ImageIcon(ima);
        img = new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
        btn[i].btn.setIcon(img);}}
static void deal(Iterator ite,Button btn){
    Card card = (Card)ite.next();
    btn.card = card;
    String ima=card.img;
    ImageIcon img = new ImageIcon(ima);
    img = new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
    btn.btn.setIcon(img);
}
static void out(Button[] btn,Button Jbtn,Frame Frame,Button BTN,Iterator ite){
    Card out;
    out = Jbtn.card;
    TIME = time/17+TIME;
    if(time%17==0){time = 0;}
    JButton lose = new JButton();
    lose.setSize(45, 50);
    lose.setLocation((50+(time)*45), 135+TIME*55);
    String ima = out.img;
    ImageIcon img = new ImageIcon(ima);
    img =new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
    lose.setIcon(img);
    Frame.add(lose);
    Jbtn.card = BTN.card;
    ima = Jbtn.card.img;
    img = new ImageIcon(ima);
    img =new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
    Jbtn.btn.setIcon(img);
    BTN.card = null;
    BTN.btn.setIcon(null);
    BTN.btn.setContentAreaFilled(false);
    rank(btn,Frame);
    tools.deal(ite, BTN);
    Frame.repaint();
    time++;
}
static void out2(Button BTN,Frame frame,Iterator ite){
    Card out;
    out = BTN.card;
    TIME = time/17+TIME;
    if(time%17==0){time = 0;}
    JButton lose = new JButton();
    lose.setSize(45, 50);
    lose.setLocation((50+(time)*45), 135+TIME*55);
    String ima = out.img;
    ImageIcon img = new ImageIcon(ima);
    img =new ImageIcon(img.getImage().getScaledInstance(60, 50, Image.SCALE_SMOOTH));
    lose.setIcon(img);
    frame.add(lose);
    BTN.card = null;
    BTN.btn.setIcon(null);
    BTN.btn.setContentAreaFilled(false);
    tools.deal(ite, BTN);
    frame.repaint();
    time++;
}

}
public class Mythread extends Thread{
Frame frame;
Iterator ite;
public Mythread(String Name,Frame frame,Iterator ite){
setName(Name);
this.frame = frame;
this.ite =ite;
}
public void run(){
int x,y;
y =500;
Button[] btn = new Button[13];
for(int i=0;i<13;i++){
x =50+i*45;btn[i] = new Button(frame,(Card)ite.next(),x,y);
}tools.rank(btn,frame);
Button BTN = new Button(frame,660,y);
tools.deal(ite, BTN);
for(int i=0;i<13;i++){
Button jbtn = btn[i];
btn[i].btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tools.out(btn,jbtn,frame,BTN,ite);
}
});
}
BTN.btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tools.out2(BTN, frame,ite);
}
});
this.suspend();
}
}
public class AIthread extends Thread{
Frame frame;
Iterator ite;
public AIthread(String Name,Frame frame,Iterator ite){
setName(Name);
this.frame = frame;
this.ite =ite;
}
public void run(){
int x,y;
y =80;
Button[] btn = new Button[13];
for(int i=0;i<13;i++){
x =50+i*45;btn[i] = new Button(frame,(Card)ite.next(),x,y);
}tools.rank(btn,frame);
Button BTN = new Button(frame,660,y);
tools.deal(ite, BTN);
for(int i=0;i<13;i++){
Button jbtn = btn[i];
btn[i].btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tools.out(btn,jbtn,frame,BTN,ite);
}
});
}
BTN.btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tools.out2(BTN, frame,ite);
}
});

}

}
public class test {
public static void main(String[] agrs){
int i,j;
Frame mainFrame = new Frame();
ArrayList list = new ArrayList();
Card[] MJ = new Card[64];
String IMG[]={"A.jpg","B.jpg","C.jpg","D.jpg","E.jpg","F.jpg","G.jpg","H.jpg"
,"I.jpg","J.jpg","K.jpg","L.jpg","M.jpg","N.jpg","O.jpg","P.jpg"};
for(i=0;i<16;i++){
for(j=0;j<4;j++){
MJ[j+i*4] = new Card(i,IMG[i]);
list.add(MJ[j+i*4]);} }
Collections.shuffle(list);//打乱list
Iterator ite = list.iterator();
Mythread player = new Mythread("player",mainFrame,ite);
AIthread AI = new AIthread("AI",mainFrame,ite);
player.start();
AI.start();
}
}

  • 写回答

3条回答

  • lianjunzongsiling 2017-09-08 12:59
    关注

    你代码乱掉了


    大致看了下,你应该是写个swing小程序
    你想暂停一个线程,然后重启一个已经暂停的线程
    建议你百度 java wait notifyAll
    估计有你想要的东西

    评论

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)