سؤال

لدي jtextarea في jpanel. كيف يمكنني ملء JPanel بالكامل وتغيير حجمه عندما يتم تغيير حجم JPanel وتمريره عند كتابة الكثير من النص؟

هل كانت مفيدة؟

المحلول

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());  //give your JPanel a BorderLayout

JTextArea text = new JTextArea(); 
JScrollPane scroll = new JScrollPane(text); //place the JTextArea in a scroll pane
panel.add(scroll, BorderLayout.CENTER); //add the JScrollPane to the panel
// CENTER will use up all available space

نرى http://download.oracle.com/javase/6/docs/api/javax/swing/jscrollpane.html أو http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html لمزيد من التفاصيل حول jscrollpane

نصائح أخرى

ضع jtextarea داخل JsCrollpane ، ووضع ذلك في jpanel مع تخطيط يحدد الحجم. مثال مع Gridbaglayout ، على سبيل المثال يمكن أن تبدو هكذا:

JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());

JScrollPane scrollpane = new JScrollPane();
GridBagConstraints cons = new GridBagContraints();
cons.weightx = 1.0;
cons.weighty = 1.0;
panel.add(scrollPane, cons);

JTextArea textArea = new JTextArea();
scrollPane.add(textArea);

هذا مجرد رسم تقريبي ، ولكن يجب أن يوضح كيفية القيام بذلك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top