Yes ,I can ! 2019-11-30 18:26 采纳率: 0%
浏览 131

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

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();

     }
    }
}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
    • ¥15 onvif+openssl,vs2022编译openssl64