2011-09-01 7 views
2

JFrame 및 "this"라는 키워드에 문제가 있습니다. frame.getContentPane을 사용할 때 프레임을 "this"로 바꾸지 않으면 구성 요소가 표시되지 않습니다. getContentPane이 JFrame 내용 창을 가져 오지 않는 것 같아요, 다른 내용을 얻었습니다. 두 가지 JFrame 구성 요소가 하나만 있다고하더라도 두려워합니다. 아무도이 문제를 설명 할 수 있습니까?
이 내 코드입니다 :
JFrame을 사용할 때 구성 요소가 표시되지 않습니다.

public class Form1 extends JFrame 
{ 
    private static final long serialVersionUID = 1L; 
    JFrame frame = new JFrame("TableLayout"); 
    Container content = frame.getContentPane();//this doesn't work unless I replace "frame" with the key word "this"/// 
public Form1()//constructor 
{ 
    String label[] = {"Top", "Bottom", "Add", "Delete", "Center", "Overlap"}; 
    double border = 5; 
    double size[][] = 
     {{border, 0.20, border, TableLayout.FILL, border, 0.80, border}, // Columns 
     {border, 0.15, border, TableLayout.FILL, border, 0.10, border}}; // Rows 
    JButton button[] = new JButton[label.length]; 
    for (int i = 0; i < label.length; i++) 
    { 
     button[i] = new JButton(label[i]); 
    } 
    content.add (button[0], "1, 1, 5, 1"); // Top (row,column) 
    content.add (button[1], " 1, 5, 5, 5"); // Bottom 
    content.add ((button[2], "1, 3  "); // Left 
    content.add (button[3],"5, 3,  "); // Right 
    this.pack(); 
    } 
} 
+0

을 슬프게도, 나는 한 번 다시 동안보다이 더 했어 고맙습니다. :-) – trashgod

답변

4
public class Form1 extends JFrame 
{ 
    private static final long serialVersionUID = 1L; 
    JFrame frame = new JFrame("TableLayout"); 

클래스 프레임이며, 또한 frame라는 Frame 속성이 있습니다. 물론 두 개의 프레임이 있습니다!

변경이 .. 더 같은 것으로

public class Form1 extends JFrame 
{ 
    private static final long serialVersionUID = 1L; 
    JFrame frame = new JFrame("TableLayout"); 
    Container content = frame.getContentPane();//this doesn't work unless I replace "frame" with the key word "this"/// 
public Form1()//constructor 
{ 

(명확한 답변을 질문에 SSCCE와 함께) ..

public class Form1 extends JFrame 
{ 
    private static final long serialVersionUID = 1L; 
    Container content; 

public Form1()//constructor 
{ 
    super("TableLayout"); 
    content = getContentPane(); 
4

귀하의 예를 모두 extends JFrame하고있다-A JFrame을. 전자는 this으로 참조됩니다. 후자는 frame입니다.

관련 문제