ورود

View Full Version : مشکل با نمایش اشیاء



hossein71
چهارشنبه 11 آبان 1390, 17:09 عصر
سلام
من بوسیله کدنویسی یک JTextArea ساختم ولی در هنگام اجرای برنامه نمایش داده نمیشه.
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Testclass extends JFrame {
JPanel P;
JTextArea TA;
public Test(){
this.P=new JPanel();
this.setContentPane(P);
this.TA=new JTextArea();
P.add(TA);
TA.setBounds(10, 10, 100, 100);
TA.setBackground(Color.red);
}
public static void main(String[] args) {
new Testclass().setVisible(true);
}
}

saban56
چهارشنبه 11 آبان 1390, 22:04 عصر
JPanel نیاز به تنظیم یک layout دارد به طور مثال در کد شما با BorderLayout به این صورت شیء TA را نشان می دهد



import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Testclass extends JFrame {
JPanel P;
JTextArea TA;

public Testclass() {
this.P = new JPanel();
this.setContentPane(P);
P.setLayout(new BorderLayout());
this.TA = new JTextArea();
P.add(TA,BorderLayout.CENTER);
TA.setBounds(10, 10, 100, 100);
TA.setBackground(Color.red);
}

public static void main(String[] args) {
new Testclass().setVisible(true);
}
}

sobaisobai
چهارشنبه 11 آبان 1390, 22:23 عصر
سلام
با تشکر از دوستمون saban56 (http://barnamenevis.org/member.php?137196-saban56)
اینم میتونی بذاری
this.P.setLayout(null);