首页 新闻 会员 周边 捐助

我想要的结果是在GUI界面的第一行第二列的JLable添加字符串,控制台输出正常,但是就是添加不到JLable里!!怎么回事啊?

0
[待解决问题]

package 发牌;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Program extends Thread{
private JFrame jf;
public JLabel jl[] = new JLabel[10];
public String num[] = new String[53];
public Program(){
for(int i=1; i<=52; i++){
num[i] = i + "";
}
jf = new JFrame("发牌器");
jf.setSize(500, 400);
jf.setLocation(100, 100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new GridLayout(3, 3));
for(int i=1; i<=9; i++){
if(i%2!=0){
jl[i] = new JLabel();
jl[i].setBackground(new Color(106, 106, 106));
jl[i].setOpaque(true);
jf.add(jl[i]);
}
else {

            jl[i] = new JLabel(); 
            jl[i].setBackground(new Color(140, 140, 115));
            jl[i].setOpaque(true);
            jf.add(jl[i]);
        }
    }
    jf.setVisible(true);
}

public static void main(String[] args) {
    Program pr = new Program();
    CardBuffer cd = new CardBuffer();
    PutCard pt = new PutCard(pr, cd);
    GetCard gc = new GetCard(pr, cd);
    pt.start();
    gc.start();

}

}
class CardBuffer{
private String card;
public synchronized void put(String card){
this.card = card;
}

public synchronized String get(){
    return card;
}

}
class PutCard extends Thread {
private Program pr = new Program();
private CardBuffer cd = new CardBuffer();
public static boolean empty = true;
Random cardNum = new Random();
public PutCard(Program pr, CardBuffer cd){
this.pr = pr;
this.cd = cd;
}
public void run(){

    synchronized (cd) {
        for(int i=0; i<52; i++){
        while(!empty){
            try{
                cd.wait();
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
        try{
            sleep(3000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        int index = (int)(Math.random()*pr.num.length);
        String str = pr.num[index];
        cd.put(str);
        System.out.print(str+"  ");
        empty = false;
        cd.notify();
     }
    }
}

}
class GetCard extends Thread{
private String str = "";
private int n = 2;
Program pr = new Program();
CardBuffer cd = new CardBuffer();
public GetCard(Program pr, CardBuffer cd){
this.pr = pr;
this.cd = cd;
}
public void run(){

    synchronized (cd) {
        for(int i=0; i<52; i++){
            
        while(PutCard.empty){
            try{
                cd.wait();
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
        try{
            sleep(3000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        str += cd.get();
        System.out.print(str+"  ");
        pr.jl[n].setText(str);

        PutCard.empty = true;
        cd.notify();
        
     }
    }
}

}

凡凡凡凡的主页 凡凡凡凡 | 菜鸟二级 | 园豆:202
提问于:2019-11-30 18:33
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册