Java游戏

根据题目要求,人取完之后电脑自动取火柴,判定胜负。用鼠标单击ok或用键盘按enter键,找出提交者获得的匹配数。图形界面如下:

导入Java . awt . *;

导入Java . awt . event . action listener;

导入Java . awt . event . mouse adapter;

导入Java . awt . event . mouse event;

导入Java . SQL . prepared statement;

导入Java . SQL . resultset;

导入Java . util . random;

导入javax . swing . joptionpane;

导入javax . swing . jframe;

导入javax . swing . jbutton;

导入javax . swing . jlabel;

导入javax . swing . joptionpane;

导入javax . swing . jpanel;

导入javax . swing . jtextfield;

导入javax . swing . jpasswordfield;

公共类game扩展javax.swing.JFrame实现ActionListener{

私有JPanel框架;

私人JTextField pwd

private JTextField jlabel 1;

private JTextField jLabel2

私人JButton bntOk

private JLabel l 1;

private JLabel l2

private JLabel l3

int total

公共静态void main(String[] args)

{

game ff = new game();

}

公共游戏(){

init GUI();?

Random r = new Random();

total = r . nextint(51);

while(总计& lt20){

total = r . nextint(51);

}

jlabel 2 . settext(string . value of(total));

this.setSize(500,500);?

this.setLocation(300,400);

this . setlocationrelativeto(null);

this . set visible(true);

this . setdefaultcloseoperation(JFrame。EXIT _ ON _ CLOSE);

getRootPane()。setDefaultButton(bnt ok);

bnt ok . addactionlistener(this);

}

public void action performed(Java . awt . event . action event evt)

{

int a = integer . parse int(jlabel 1 . gettext());

如果((a & gt3)||(a==0)){

Joptionpane。显示消息对话框(游戏。这个,“输入错误\n请重新输入”);

返回;

}

total = total-a;

jlabel 2 . settext(string . value of(total));

if(总计& lt=0){

Joption窗格。显示消息对话框(游戏。这个,“恭喜,胜利!”);

返回;

}

Random r = new Random();?

int b = r . nextint(4);

while(b==0)

b = r . nextint(4);

pwd . settext(string . value of(b));

total = total-b;

jlabel 2 . settext(string . value of(total));

if(总计& lt=0){

Joptionpane。显示消息对话框(游戏。这个,“对不起,你输了!”" );

}

}

私有void initGUI(){

frame = new JPanel();?

getContentPane()。添加(frame,BorderLayout。中心);

frame.setLayout(空);?

bn Tok = new JButton();?

bnt OK . settext(" OK ");

bntOk.setBounds(150,200,66,30);

frame . add(bnt ok);

jlabel 2 = new JTextField();

jLabel2.setBounds(150,50,100,50);

frame . add(jlabel 2);

l 1 = new JLabel();

L1.setText("匹配数");

l1.setBounds(90,50,100,50);?

frame . add(l 1);

jlabel 1 = new JTextField();

jlabel 1 . settext(" ");

jLabel1.setBounds(200,149,77,22);?

frame . add(jlabel 1);

L2 = new JLabel();

L2.setText("我得到匹配");

l2.setBounds(100,149,77,22);

frame . add(L2);

L3 = new JLabel();

L3.setText("对方拿火柴");

l3.setBounds(100,249,77,22);

frame . add(L3);

pwd = new JTextField();

pwd.setBounds(200,249,77,22);

frame . add(pwd);

bntOk.addMouseListener(新的MouseAdapter()

{?

@覆盖

public void mouse clicked(mouse event e 1){

int a = integer . parse int(jlabel 1 . gettext());

如果((a & gt3)||(a==0)){

Joptionpane。显示消息对话框(游戏。这个,“输入错误\n请重新输入”);

返回;

}

total = total-a;

jlabel 2 . settext(string . value of(total));

if(总计& lt=0){

jlabel 2 . settext(" 0 ");

Joption窗格。显示消息对话框(游戏。这个,“恭喜,胜利!”);

返回;

}

Random r = new Random();?

int b = r . nextint(4);

while(b==0)

b = r . nextint(4);

pwd . settext(string . value of(b));

total = total-b;

jlabel 2 . settext(string . value of(total));

if(总计& lt=0){

Joptionpane。显示消息对话框(游戏。这个,“对不起,你输了!”" );

}

}

}

);

}

}