火柴人大战java代码 火柴人程序代码( 三 )


private JTextField jLabel1;
private JTextField jLabel2;
private JButton bntOk;
private JLabel l1;
private JLabel l2;
private JLabel l3;
int total;
public static void main(String[] args)
{
game ff=new game();

}
public game(){
initGUI();
Random r=new Random();
total=r.nextInt(100);
jLabel2.setText(String.valueOf(total));
this.setSize(500, 500);
this.setLocation(300, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getRootPane().setDefaultButton(bntOk);
bntOk.addActionListener(this);
}
public void actionPerformed(java.awt.event.ActionEvent evt)
{
int a=Integer.parseInt(jLabel1.getText());
if((a3)||(a==0)){
JOptionPane.showMessageDialog(game.this,"输入错误\n请重新输入");
return;
}
total=total-a;
jLabel2.setText(String.valueOf(total));
if(total=0){
JOptionPane.showMessageDialog(game.this,"恭喜你,胜利了!");
return;
}
Random r=new Random();
int b=r.nextInt(4);
while(b==0)
b=r.nextInt(4);
pwd.setText(String.valueOf(b));
total=total-b;
jLabel2.setText(String.valueOf(total));
if(total=0){
JOptionPane.showMessageDialog(game.this,"很遗憾,你输了!");
}
}
private void initGUI(){
frame=new JPanel();
getContentPane().add(frame, BorderLayout.CENTER);
frame.setLayout(null);
bntOk =new JButton();
bntOk.setText("OK");
bntOk.setBounds(150, 200, 66, 30);
frame.add(bntOk);

jLabel2= new JTextField();
jLabel2.setBounds(150, 50, 100, 50);
frame.add(jLabel2);
l1=new JLabel();
l1.setText("火柴数量");
l1.setBounds(90, 50, 100, 50);
frame.add(l1);
jLabel1= new JTextField();
jLabel1.setText("");
jLabel1.setBounds(200,149, 77, 22);
frame.add(jLabel1);
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(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e1) {
int a=Integer.parseInt(jLabel1.getText());
if((a3)||(a==0)){
JOptionPane.showMessageDialog(game.this,"输入错误\n请重新输入");
return;
}
total=total-a;
jLabel2.setText(String.valueOf(total));
if(total=0){
jLabel2.setText("0");
JOptionPane.showMessageDialog(game.this,"恭喜你,胜利了!");
return;
}
Random r=new Random();
int b=r.nextInt(4);
while(b==0)
b=r.nextInt(4);
pwd.setText(String.valueOf(b));
total=total-b;
jLabel2.setText(String.valueOf(total));
if(total=0){
JOptionPane.showMessageDialog(game.this,"很遗憾,你输了!");
}
}
}
);

}
}
【火柴人大战java代码 火柴人程序代码】火柴人大战java代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于火柴人程序代码、火柴人大战java代码的信息别忘了在本站进行查找喔 。

推荐阅读