วันพุธที่ 31 สิงหาคม พ.ศ. 2554

ตัวอย่าง เกม JAVA ง่ายๆ เกมทายเลข ลองเอาไปเป็น Ex ครับ

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class JJJ extends JFrame implements ActionListener{
 JButton bt = new JButton("ok");
 int r = -1;
 JTextField tx = new JTextField(10000);
 JJJ(){
  Random t = new Random();
   r = t.nextInt(1000);
   System.out.print(r);
  setLayout(null);
  setVisible(true);
  setBounds(10,10,500,500);
  add(bt);add(tx);
  tx.setText("0");
  bt.addActionListener(this);
  bt.setBounds(0, 0, 200, 30);
  tx.setBounds(0, 50, 200, 30);
 }
 public static void main(String[] args) {
  new JJJ();
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  if(e.getSource()==bt){
  
  
   int num = Integer.parseInt(tx.getText());
  
 
    if(r>num){
     JOptionPane.showMessageDialog(null, "low");
    }
    else if(r<num){
     JOptionPane.showMessageDialog(null, "much");
    }
    else if(r==num){
     JOptionPane.showMessageDialog(null, "win");
     System.exit(0);
    }
   
   }
  
  }
  
 
 

}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น