日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java swing 模拟发牌_用java设计一个发牌程序

發(fā)布時(shí)間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java swing 模拟发牌_用java设计一个发牌程序 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

展開全部

// 發(fā)牌程序。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class CardBuffer //加互斥鎖的緩沖區(qū)

{

private int value;

private boolean isEmpty = true; //value是否為空的信號(hào)量

private int order=0; //信號(hào)量,e68a8462616964757a686964616f31333236373165約定取牌線程的次序

synchronized void put(int i)

{

while (!isEmpty) //當(dāng)value不空時(shí),等待

{

try

{

this.wait(); //等待

}

catch(InterruptedException e) {}

}

value = i; //當(dāng)value空時(shí),value獲得值

isEmpty = false; //設(shè)置value為不空狀態(tài)

notifyAll(); //喚醒所有其他等待線程

}

synchronized int get(int order) //order是取牌線程約定的次序

{

while (isEmpty || (this.order!=order)) //當(dāng)value空或取牌次序不符時(shí)等待

{

try

{

this.wait();

}

catch(InterruptedException e) {}

}

isEmpty = true; //設(shè)置value為空狀態(tài),并返回值

notifyAll();

this.order = (this.order+1)%4; //加1使取牌次序輪轉(zhuǎn)

return value;

}

}

class Sender extends Thread //發(fā)牌線程類

{

private CardBuffer cardbuffer;

private int count;

public Sender(CardBuffer cardbuffer,int count)

{

this.cardbuffer = cardbuffer;

this.count = count;

}

public void run()

{

for (int i=1;i<=this.count;i++)

cardbuffer.put(i);

}

}

class Receiver extends Thread //取牌線程類

{

private CardBuffer cardbuffer;

private JTextArea text;

private int order; //信號(hào)量,約定取牌線程的次序

public Receiver(CardBuffer cardbuffer,JTextArea text,int order)

{

this.cardbuffer = cardbuffer ;

this.text = text ;

this.order = order;

}

public void run()

{

while(true)

{

text.append(" "+cardbuffer.get(this.order));

try

{

this.sleep(100);

}

catch(InterruptedException e) {}

}

}

}

class CardJFrame extends JFrame

{

public CardJFrame()

{

super("發(fā)牌程序");

this.setSize(430,200);

this.setLocation(300,240);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setLayout(new GridLayout(3,3,5,5)); //3行3列網(wǎng)格布局,間隔為5

JTextArea text_north,text_east,text_south,text_west; //獲得牌的4個(gè)文本區(qū)

text_north = new JTextArea();

text_east = new JTextArea();

text_south = new JTextArea();

text_west = new JTextArea();

text_north.setLineWrap(true); //設(shè)置文本區(qū)自動(dòng)換行

text_east.setLineWrap(true);

text_south.setLineWrap(true);

text_west.setLineWrap(true);

text_north.setEditable(false);

text_east.setEditable(false);

text_south.setEditable(false);

text_west.setEditable(false);

Font font = new Font("Helvetica", Font.PLAIN, 16);

text_north.setFont(font);

text_east.setFont(font);

text_south.setFont(font);

text_west.setFont(font);

this.add(new JPanel()); //網(wǎng)格布局的第1行

this.add(text_north);

this.add(new JPanel());

this.add(text_west); //網(wǎng)格布局的第2行

this.add(new JPanel());

this.add(text_east);

this.add(new JPanel()); //網(wǎng)格布局的第3行

this.add(text_south);

this.add(new JPanel());

this.setVisible(true);

CardBuffer cardbuffer = new CardBuffer();

Sender s = new Sender(cardbuffer,52);

s.setPriority(10); //設(shè)置最高優(yōu)先級(jí)

s.start(); //啟動(dòng)發(fā)牌線程

(new Receiver(cardbuffer,text_north,0)).start(); //創(chuàng)建并啟動(dòng)4個(gè)取牌線程,優(yōu)先級(jí)為5

(new Receiver(cardbuffer,text_east,1)).start();

(new Receiver(cardbuffer,text_south,2)).start();

(new Receiver(cardbuffer,text_west,3)).start();

}

public static void main(String arg[])

{

new CardJFrame();

}

}

本回答由提問者推薦

已贊過

已踩過<

你對(duì)這個(gè)回答的評(píng)價(jià)是?

評(píng)論

收起

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的java swing 模拟发牌_用java设计一个发牌程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。