الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ممكن المساعده

مغلق
بدأه سلامه في 16 يوليو 2003 · 11 رد · 984 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله و بركاته

ممكن المساعده

في هذا البرنامج انا حليته بس واجهة مشاكل في بعض الاشياء ممكن المساعده حلي موجود بعد السؤال

السؤال:

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

و شكرا

#2

بالنسبه للرسائل، استخدم

JOptionPane.showMessageDialog(null, "My MSG.","My Title",JOptionPane.PLAIN_MESSAGE);

بالنسبه لاختيار رقم عشوائي .. باشوف لك كود راح يساعدك ...

#3

في موضوع للأخ 2pac بخصوص الرقم العشوائي

#4

السلام عليكم

هلا اخ صلاح

شكرا على الرد بس انا اعرف هذا الحل بس في السؤال مطلوب الرساله بستخدام Jlabel

AJLabel should display either "Too High" or "Too Law" to help the user zero in on the correct answer

شكرا على الرد(f)

#6

شكرا

بس انا هذا الي ما فاهمته

#7

استخدم jlabel1=new Jlabel(" ") مرة واحدة فقط بعد ذلك عدل البرنامج كما يلي

if (x>80)
   jlable1.setText(" Too High")
else 

   jlable1.setText(" Too Low")
اللهم انا نسألك علما نافعا ونعوذ بك من حماقة الجهلاء
#8

يا أخت سلامه في مشكلة في برنامجك. المفروض إنه يعطيني نتيجة إذا ظغطت Enter لكن الواقع لا :D

وعندما أظغط ال JButton الرنامج يتوقف :confused:

#9

هذا هو الحل :)

guess2.zip

#10

شكرا اخويه محمد

مشكووووووووووووووووووووووووور وايد

(f)انشاله اقدر اردها حقك

جزاك الله الف خير

#11

هل يعمل بالشكل المطلوب :)

#12

نعم(f)

هذا الموضوع مغلق.

مواضيع مشابهة