بسم الله الرحمن الرحيم
المشكلة
كيف يمكن منع JTextArea من التمدد نتيجة وضع بيانات اثناء زمن التنفيذ
الحل
يكمن الحل فى وضع JTextArea بداخل JScrollPane مما يمكن من اظهار Scroll bars و بالتالى تثبيت حجم منطقة النص كما يلى:-
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class TestTextArea extends JFrame
{
public TestTextArea()
{
JPanel panel = new JPanel();
setContentPane( panel );
panel.setPreferredSize( new Dimension( 200, 200) );
JTextArea textArea = new JTextArea( "one two",3, 15 );
JScrollPane scrollPane = new JScrollPane(textArea );
panel.add( scrollPane );
JTextArea textArea2= new JTextArea( "abcd", 2,10 );
textArea2.setPreferredSize( new Dimension( 50,10 ) );
panel.add( textArea2 );
}
public static void main(String[] args)
{
TestTextArea frame = new TestTextArea();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}