السلام عليكم ورحمة الله و بركاته
ممكن المساعده
في هذا البرنامج انا حليته بس واجهة مشاكل في بعض الاشياء ممكن المساعده حلي موجود بعد السؤال
السؤال:
write a program that plays "guess the number" as follows: your
program chooses the number to be guessed by selecting an integer at random in the range 1-1000. the program then displays in a label: I have a number between 1 and 1000 can you guess my number? Please enter your first guess.
A JTextField should be used to input the guess. As each guess is input the background color should change to either red or blue.Red indicates that the user is getting "warmer" and blue indicates that the user is getting "colder" . AJLabel should display either "Too High" or "Too Law" to help the user zero in on the correct answer.When the user gets the correct answer,"Correct!" should be displayed and the JTextField used for input should be changed to uneditable. A JButton should be provided to allow the user to play the game again.When the JButton is clicked, a new random number should be generated and the input JTextField changed to editable
جوابي:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Guess extends JFrame{
private JLabel label,l;
private JTextField field;
private int number;
private JButton button;
private Container container;
private JPanel p2 ;
public Guess()
{
super("Guess The Number");
p2 = new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
number =100;
label= new JLabel("I have a number between 1 and 1000 can you guess my number?");
p2.add(label);
JLabel label2 = new JLabel("Please enter you first guess.");
p2.add(label2);
container =getContentPane();
field = new JTextField(35);
field.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
if (event.getSource()== field)
{
int r=Integer.parseInt(event.getActionCommand());
if (Integer.parseInt(event.getActionCommand()) == number) {
p2.add(new JLabel("Correct!"));
field.setEditable(false);
}
else if (Integer.parseInt(event.getActionCommand()) < number) {
if(80 {
p2.setBackground(Color.red);
}
else if (r<80)
{
p2.setBackground(Color.blue);
JLabel l = new JLabel("Too Low");
p2.add(l);
}
/*JLabel l = new JLabel("Too Low");*/
// p2.add(l);
}
else if (Integer.parseInt(event.getActionCommand()) > number) {
if(r>number && r<150)
{
p2.add(new JLabel("Too High"));
p2.setBackground(Color.red);
}
/* else
p2.setBackground(Color.blue);
p2.add(new JLabel("Too High"));*/
}
}
}
}
);
p2.add(field);
getContentPane().add(p2);
JPanel panel= new JPanel();
panel.setLayout(new
BorderLayout());
button = new JButton("Play Again");
panel.add(button);
getContentPane().add(panel,BorderLayout.SOUTH);
button.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
if (event.getSource()== button)
{
System.exit(0);
Guess g=new Guess();
}
}
}
);
setSize(400,300);
show();
}
public static void main(String args[])
{
new Guess();
}
}ناقص كيف اخلي الرقم الي في البرنامج يتغير وذالك بستخدام الراندوم بس ما ضبط معايه و اللون يتغير لازم حسب الرقم الي يدخله اليوزر و المسج الي يقول حق اليوزر too low and too high and correct
و شكرا