Swingطريقة الحصول على النص المدخل في JTextArea
المثال التالي يعلمك طريقة الحصول على النص المدخل في الـ Text Area.
مثال
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
public class Main {
static JFrame frame = new JFrame("JTextArea demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
static JTextArea textArea = new JTextArea(); // textArea إسمه Text Area هنا أنشأنا
static JScrollPane scrollPane = new JScrollPane(textArea); // بداخله textArea و وضعنا الـ Scroll Pane أي قمنا بإنشاء JScrollPane هنا أنشأنا كائن من الكلاس
static JLabel label = new JLabel("Enter any thing"); // label إسمه Label هنا أنشأنا
static JLabel labelResult = new JLabel(); // فارغ labelResult إسمه Label هنا أنشأنا
static JButton button = new JButton("Get text"); // button إسمه Button هنا أنشأنا
public static void main(String[] args) {
label.setBounds(40, 40, 100, 30); // frame في الـ label هنا قمنا بتحديد حجم و موقع الكائن
scrollPane.setBounds(150, 40, 200, 100); // frame في الـ scrollPane هنا قمنا بتحديد حجم و موقع الكائن
button.setBounds(40, 160, 80, 30); // frame في الـ button هنا قمنا بتحديد حجم و موقع الكائن
labelResult.setBounds(40, 200, 340, 30); // frame في الـ labelResult هنا قمنا بتحديد حجم و موقع الكائن
textArea.setLineWrap(true); // ينزل على سطر جديد في حال كان عدد الأحرف المدخلة أكبر من عدد الأحرف التي يستطيع السطر إستيعابها textArea هنا جعلنا النص الذي ندخله في كائن الـ
textArea.setWrapStyleWord(true); // هنا جعلنا الكلمة تظهر على سطر جديد في حال كانت لا تسع في السطر
frame.add(label); // frame في الـ label هنا أضفنا الكائن
frame.add(scrollPane); // frame في الـ scrollPane هنا أضفنا الكائن
frame.add(button); // frame في الـ button هنا أضفنا الكائن
frame.add(labelResult); // frame في الـ labelResult هنا أضفنا الكائن
frame.setSize(400, 300); // هنا قمنا بتحديد حجم النافذة. عرضها 400 و طولها 300
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // لذلك قمنا بتحديد مكان كل شيء قمنا بإضافته في النافذة Layout Manager أي لم نستخدم أي null هنا وضعنا
frame.setVisible(true); // هنا جعلنا النافذة مرئية
button.addActionListener(new ActionListener() { // button هنا نضع الأوامر التي نريد تنفيذها عند النقر على الزر
@Override
public void actionPerformed(ActionEvent e) {
labelResult.setText(textArea.getText()); // button عند النقر على الـ label و وضعه كنص للكائن textArea سيتم جلب النص الموجود في الكائن
}
});
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
public class Main {
static JFrame frame = new JFrame("JTextArea demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
static JTextArea textArea = new JTextArea(); // textArea إسمه Text Area هنا أنشأنا
static JScrollPane scrollPane = new JScrollPane(textArea); // بداخله textArea و وضعنا الـ Scroll Pane أي قمنا بإنشاء JScrollPane هنا أنشأنا كائن من الكلاس
static JLabel label = new JLabel("Enter any thing"); // label إسمه Label هنا أنشأنا
static JLabel labelResult = new JLabel(); // فارغ labelResult إسمه Label هنا أنشأنا
static JButton button = new JButton("Get text"); // button إسمه Button هنا أنشأنا
public static void main(String[] args) {
label.setBounds(40, 40, 100, 30); // frame في الـ label هنا قمنا بتحديد حجم و موقع الكائن
scrollPane.setBounds(150, 40, 200, 100); // frame في الـ scrollPane هنا قمنا بتحديد حجم و موقع الكائن
button.setBounds(40, 160, 80, 30); // frame في الـ button هنا قمنا بتحديد حجم و موقع الكائن
labelResult.setBounds(40, 200, 340, 30); // frame في الـ labelResult هنا قمنا بتحديد حجم و موقع الكائن
textArea.setLineWrap(true); // ينزل على سطر جديد في حال كان عدد الأحرف المدخلة أكبر من عدد الأحرف التي يستطيع السطر إستيعابها textArea هنا جعلنا النص الذي ندخله في كائن الـ
textArea.setWrapStyleWord(true); // هنا جعلنا الكلمة تظهر على سطر جديد في حال كانت لا تسع في السطر
frame.add(label); // frame في الـ label هنا أضفنا الكائن
frame.add(scrollPane); // frame في الـ scrollPane هنا أضفنا الكائن
frame.add(button); // frame في الـ button هنا أضفنا الكائن
frame.add(labelResult); // frame في الـ labelResult هنا أضفنا الكائن
frame.setSize(400, 300); // هنا قمنا بتحديد حجم النافذة. عرضها 400 و طولها 300
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // لذلك قمنا بتحديد مكان كل شيء قمنا بإضافته في النافذة Layout Manager أي لم نستخدم أي null هنا وضعنا
frame.setVisible(true); // هنا جعلنا النافذة مرئية
button.addActionListener(new ActionListener() { // button هنا نضع الأوامر التي نريد تنفيذها عند النقر على الزر
@Override
public void actionPerformed(ActionEvent e) {
labelResult.setText(textArea.getText()); // button عند النقر على الـ label و وضعه كنص للكائن textArea سيتم جلب النص الموجود في الكائن
}
});
}
}
ستظهر لك النافذة التالية عند التشغيل.
قم بإدخال أي نص في الـ Text Area ثم أنقر على الـ Button.