當前位置:編程學習大全網 - 編程語言 - 求壹個“JAVA拿火柴小遊戲”的程序要求如下!!!!!!!跪求!!!!!

求壹個“JAVA拿火柴小遊戲”的程序要求如下!!!!!!!跪求!!!!!

按照題目要求,人拿完火柴後計算機自動拿火柴,判斷勝利者。鼠標點擊ok或者鍵盤按enter鍵即可提交人拿的火柴個數。圖形界面如下,

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.Random;

import javax.swing.JOptionPane;

import javax.swing.JFrame;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.JPasswordField;

public class game extends javax.swing.JFrame implements ActionListener{

private JPanel frame;

private JTextField pwd;

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((a>3)||(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((a>3)||(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,"很遺憾,妳輸了!");

}

}

}

);

}

}

  • 上一篇:計算機編程哪個網絡比較好?
  • 下一篇:日語輸入法的手提電話
  • copyright 2024編程學習大全網