I have a problem with JFrame and the key word “this”. When I use frame.getContentPane, components don’t show up unless I replace frame with “this”. It seems getContentPane doesn’t get the JFrame content pane, it gets something else I don’t know what it is. I’m afraid I have two different JFrame components even though I declared only one. Can anybody explain this problem?
This is my code:
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();
}
}
Your class is a frame and also has a
Frameattribute namedframe. Of course there are two frames!Change this..
To something more like (definite answers come with an SSCCE in the question)..