public class demo extends JFrame{
public static void main(String[] args) {
demo d=new demo();
}
public demo()
{
this.setLayout(new GridLayout(2,2));
for(int i=0;i<4;i++)
{
TitledBorder border=new TitledBorder("售票窗口"+i);
Mypanel mp=new Mypanel();
mp.setBorder(border);
this.add(mp);
}
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Mypanel extends JPanel implements ActionListener{
JLabel jb1=null;
JLabel jb2 =null;
JButton jb=null;
int count =100;
public Mypanel(){
jb1=new JLabel();
jb2=new JLabel();
jb=new JButton("确认购票");
jb.addActionListener(this);
this.setLayout(new GridLayout(3,1));
this.add(jb1);
this.add(jb);
this.add(jb2);
}
public void actionPerformed(ActionEvent arg0) {
Sale sale=new Sale();
Thread t=new Thread (sale);
try {
t.sleep(50);
t.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(sale.count);
jb1.setText(sale.count+"");
System.out.println(sale.count);
System.out.println("完成一次操作");
}
}
class Sale implements Runnable{
static int count=100;
public void run(){
synchronized (this) {
if(count>0)
{
count--;
}
}
}
}