?? zxjframe.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.ImageIcon;
//////////////////////////////////////////////////
//
class Bamboo {
boolean bState; //?
JLabel bLabel; //??JLabel
ImageIcon emptyImage,bambooImage; //??empty?????
public Bamboo(JLabel bLabel, ImageIcon image) {
emptyImage=new ImageIcon("?.jpg");
this.bLabel=bLabel;
this.bambooImage=image;
bState=true; //?????
}
public synchronized void gainBamboo() {
while(!bState) { //????
try {
wait();
}catch(Exception e) {
e.printStackTrace();
}
}
bLabel.setIcon(emptyImage); //??? ???
bState=false; //???????
}
public synchronized void takeBamboo() { //??????
bState=true;
bLabel.setIcon(bambooImage);
notify(); //????е??????ж
}
}
//////////////////////////////////////////////////////////
//??
class Zexuejia extends Thread {
int thinkSpeed, eatSpeed; // ???
Bamboo left, right; // ???
int num; // ??
JLabel zxjLable, leftLable, rightLable, zxjLeftLabel, zxjRightLabel; // zxjLable?????
// leftLable,rightLable,???
// zxjLeftLabel,zxjRightLabel???
ImageIcon leftImage, rightImage; //???
Zexuejia(int num, JLabel zLabel, Bamboo left, JLabel lLabel,
Bamboo right, // ???
JLabel rLabel, ImageIcon lImage, ImageIcon rImage, JLabel zLLabel,
JLabel zRLabel) {
this.num = num;
this.zxjLable = zLabel;
this.left = left;
this.leftLable = lLabel;
this.right = right;
this.rightLable = rLabel;
this.leftImage = lImage;
this.rightImage = rImage;
this.zxjRightLabel = zRLabel;
this.zxjLeftLabel = zLLabel;
}
public void run() {
ImageIcon thinkImage = new ImageIcon("thinking.gif");
ImageIcon huangryImage = new ImageIcon("huangry.gif");
ImageIcon eatImage = new ImageIcon("eating.gif");
ImageIcon empty = new ImageIcon("empty.jpg");
while (true) {
zxjLable.setIcon(thinkImage);
do {
thinkSpeed = (int) (Math.random() * 10000); // ??
} while (thinkSpeed < 1500);
try {
sleep(thinkSpeed); // ????????Щ?
} catch (Exception e) {
}
zxjLable.setIcon(huangryImage);
left.gainBamboo(); // ??
zxjLeftLabel.setIcon(leftImage);
right.gainBamboo(); // ???
zxjRightLabel.setIcon(rightImage);
zxjLable.setIcon(eatImage);
do {
eatSpeed = (int) (Math.random() * 10000); // ???
} while (eatSpeed < 1500);
try {
sleep(eatSpeed); // ?????????Щ?
} catch (Exception e) {
}
synchronized (left) {
left.takeBamboo(); // ??
zxjLeftLabel.setIcon(empty);
}
synchronized (right) {
right.takeBamboo(); // ???
zxjRightLabel.setIcon(empty);
}
}
}
}
public class ZxjFrame extends JFrame implements ActionListener {
JButton start_Button,end_Button;
JLabel bambooLabel[];
JLabel h[];
JLabel zxjLable[];
Bamboo bamboo[]; // ?
Zexuejia zxj[]; // ??
ImageIcon bambooImage[],empty,thinkImage,huangryImage,eatImage;
ZxjFrame()
{ // ?
super("---????---??3105007364?3----");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container ct=this.getContentPane();
ct.setBackground(java.awt.Color.WHITE);
thinkImage=new ImageIcon("thinking.gif");
huangryImage=new ImageIcon("huangry.gif");
eatImage=new ImageIcon("eating.gif");
empty=new ImageIcon("?.gif");
JPanel panel=new JPanel();
panel.setLayout(new FlowLayout());
zxjLable=new JLabel[6];
for(int i=1;i<=5;i++) {
zxjLable[i]=new JLabel();
zxjLable[i].setIcon(thinkImage); //????
}
bambooImage=new ImageIcon[6];
bambooImage[1]=new ImageIcon("bamboo.gif");
bambooImage[2]=new ImageIcon("bamboo.gif");
bambooImage[3]=new ImageIcon("bamboo.gif");
bambooImage[4]=new ImageIcon("bamboo.gif");
bambooImage[5]=new ImageIcon("bamboo.gif");
bambooLabel=new JLabel[6];
bambooLabel[1]=new JLabel(); bambooLabel[1].setIcon(bambooImage[1]);
bambooLabel[2]=new JLabel(); bambooLabel[2].setIcon(bambooImage[2]);
bambooLabel[3]=new JLabel(); bambooLabel[3].setIcon(bambooImage[3]);
bambooLabel[4]=new JLabel(); bambooLabel[4].setIcon(bambooImage[4]);
bambooLabel[5]=new JLabel(); bambooLabel[5].setIcon(bambooImage[5]);
bamboo=new Bamboo[6];
for(int i=1;i<=5;i++) bamboo[i]=new Bamboo(bambooLabel[i],bambooImage[i]); //???
h=new JLabel[13];
for(int i=1;i<=12;i++) {
h[i]=new JLabel();
h[i].setIcon(empty);
}
zxj=new Zexuejia[6];
panel=new JPanel();
panel.setLayout(new GridBagLayout());
//GridBagLayout ???
//??С??????
//? GridBagLayout ????ε?
//???????
GridBagConstraints gbc=new GridBagConstraints();
//GridBagConstraints ?? GridBagLayout ???
//?Eapi
gbc.weightx=1; gbc.weighty=1;
gbc.gridwidth=1; gbc.gridheight=1;
gbc.gridy=0;
gbc.gridx=4; panel.add(h[2],gbc);
gbc.gridx=6; panel.add(h[1],gbc);
gbc.gridy=1;
gbc.gridx=5; panel.add(zxjLable[1],gbc);
gbc.gridy=2;
gbc.gridx=0; panel.add(h[3],gbc);
gbc.gridx=3; panel.add(bambooLabel[1],gbc);
gbc.gridx=7; panel.add(bambooLabel[5],gbc);
gbc.gridx=10; panel.add(h[10],gbc);
gbc.gridy=3;
gbc.gridx=1; panel.add(zxjLable[2],gbc);
gbc.gridx=9; panel.add(zxjLable[5],gbc);
gbc.gridy=4;
gbc.gridx=0; panel.add(h[4],gbc);
gbc.gridx=10; panel.add(h[9],gbc);
gbc.gridy=5;
gbc.gridx=2; panel.add(bambooLabel[2],gbc);
gbc.gridx=8; panel.add(bambooLabel[4],gbc);
gbc.gridy=7;
gbc.gridx=2; panel.add(h[5],gbc);
gbc.gridx=3; panel.add(zxjLable[3],gbc);
gbc.gridx=7; panel.add(zxjLable[4],gbc);
gbc.gridx=8; panel.add(h[8],gbc);
gbc.gridy=8;
gbc.gridx=3; panel.add(h[6],gbc);
gbc.gridx=5; panel.add(bambooLabel[3],gbc);
gbc.gridx=7; panel.add(h[7],gbc);
ct.add(panel,BorderLayout.CENTER);
zxj[1]=new Zexuejia(1, zxjLable[1], bamboo[5], bambooLabel[5],
bamboo[1], bambooLabel[1], bambooImage[5], bambooImage[1], h[1], h[2]);
zxj[2]=new Zexuejia(2, zxjLable[2], bamboo[2], bambooLabel[2],
bamboo[1], bambooLabel[1], bambooImage[2], bambooImage[1], h[4], h[3]);
zxj[3]=new Zexuejia(3, zxjLable[3], bamboo[2], bambooLabel[2],
bamboo[3], bambooLabel[3], bambooImage[2], bambooImage[3], h[5], h[6]);
zxj[4]=new Zexuejia(4, zxjLable[4], bamboo[4], bambooLabel[4],
bamboo[3], bambooLabel[3], bambooImage[4], bambooImage[3], h[8], h[7]);
zxj[5]=new Zexuejia(5, zxjLable[5], bamboo[4], bambooLabel[4],
bamboo[5], bambooLabel[5], bambooImage[4], bambooImage[5], h[9], h[10]);
start_Button=new JButton("?");
start_Button.setEnabled(true);
start_Button.addActionListener(this);
end_Button=new JButton("?");
end_Button.addActionListener(this);
panel=new JPanel(new FlowLayout());
panel.add(start_Button);
panel.add(end_Button);
ct.add(panel,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) { // ?????
if (e.getSource() == start_Button) {
for (int i = 1; i <= 5; i++)
zxj[i].start(); // ?
start_Button.setEnabled(false);
} else if (e.getSource() == end_Button) {
System.exit(0);
}
}
public static void main(String[] args){
ZxjFrame f = new ZxjFrame();
f.setSize(720, 550);
f.setVisible(true);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -