Halo sahabat blogger yang suka ngoding. ini ada sedikit koding dari saya untuk membuat game random number. silahkan kalo mau dicoba. biar cepat copas langsung terus jalankan programnya. semoga bermanfaat. terima kasih
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author yasir
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class RandomGame extends JFrame{
int RandomNum, Guess;
private JButton button;
private JTextField textfield;
private JLabel label;
private JLabel promptlabel;
private JLabel resultlabel;
private JLabel randomlabel;
public RandomGame(){
setLayout(new FlowLayout());
promptlabel= new JLabel("Please enter your random number 1-10 :");
add(promptlabel);
textfield = new JTextField(5);
add(textfield);
button = new JButton("Guess");
add(button);
resultlabel= new JLabel("");
add(resultlabel);
randomlabel= new JLabel("");
add(randomlabel);
event e = new event();
button.addActionListener(e);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
RandomNum = (int)(Math.random()*10+1);
try{
Guess=(int)(Double.parseDouble(textfield.getText()));
if(Guess==RandomNum){
resultlabel.setText("You are the win");
}
else if(Guess!=RandomNum){
resultlabel.setText("You are the lost in the game");
}
resultlabel.setText("the random number generated was : "+RandomNum);;
}
catch(Exception ex){
randomlabel.setText("Please enter only the number");
}
}
}
public static void main(String args[]){
RandomGame gui = new RandomGame();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setTitle("Random Game");
gui.setSize(300,300);
}
}
0 komentar:
Posting Komentar