سلام للجميع....
اذا كان عندي مجموعة من JTextField و أريد أن أضع سلسلة معينة و لتكن كلمة hello في ثلاثة منها فقط و بشكل عشوائي
كيف أعمل ذلك؟؟؟؟
و شكرا جزيلاً.................
سلام للجميع....
اذا كان عندي مجموعة من JTextField و أريد أن أضع سلسلة معينة و لتكن كلمة hello في ثلاثة منها فقط و بشكل عشوائي
كيف أعمل ذلك؟؟؟؟
و شكرا جزيلاً.................
السلام عليكم
بحسب ما فهمت .. انت تريد ان تكون لديك واجهة بخمس مربعات نص
و المطلوب هو ادخال سلسلة نص (Hello مثلاً) الى ثلاث فقط من مربعات
النص الخمسه ... و لكن عشوائياً ...
اذا كان فهمي صح لسؤالك ... فإليك كود بهذا المفهوم .. مع اني لا احبذ
اعطاء الحلول جاهزه ... لكنه كان تمرين جيد لي .. فلم اقاوم :)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test01 extends JFrame implements ActionListener
{
//Here's a list of five JTextField
JTextField text_list[] = new JTextField[5];
//and here's a button to inter the word HELLO randomly in three fields in the above list
JButton button = new JButton("Inter HELLO Randomly in Three Fields");
public Test01()
{
// for the frame to work
super("Test");
Container con = getContentPane();
con.setLayout(new FlowLayout());
//loop to insert the text_list's items in the frame ( items = 5 JTextFielsd)
for(int i=0; i < text_list.length; i++)
{
text_list = new JTextField(10);
con.add(text_list);
}
//to add an ActionListener to the button, then to add it to the frame
button.addActionListener(this);
con.add(button);
// for the frame to be showen
setContentPane(con);
setDefaultCloseOperation(3);
setSize(700,150);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
//to clear the 5 JTextFields from the previous intered values
for(int i=0; i < text_list.length; i++)
text_list.setText("");
//a counter works till 3
int count = 0;
//this loop will work as long as the counter hasn't reach 3
//(because you want to enter the word HELLEO 3 times)
while (count != 3)
{
// to generate an integer to represent an index
int index = (int)(Math.random() * 5);
//to check if the JTextField with the generated index is empty
//if so, inter the word HELLEO inside then add one to the counter
if ( text_list[index].getText().equals("") )
{
text_list[index].setText("HELLO");
count++;
}
}
}
public static void main(String[] args)
{
new Test01();
}
}و ملف الكود ايضاً ضمن المرفقات ..
ارجوا ان يكون مفيد لك ..
بنت اليمن ...
لا اله الا الله .. محمد رسول الله
(* ربِ اجعلني مقيم الصلاة و من ذريتي ، ربنا و تقبل دعـاء *)
يا حيّ يا قيـوم ... برحمتك استغيث ... اصلح لي شأني كله و لا تكلني الى نفسي طرفة عين
أشكركم. جميعا...
هذا ما كنت أعمله أختي بنت اليمن و لكن ربما هناك خطأ بسيط سأحاول اكتشافه....
شكرا مرة ثانية...
تم تعديل هذه المشاركة بواسطة LJD في 29 أغسطس 2007 في 20:15
هذا الموضوع مغلق.