Here I found this code:
import java.awt.*;
import javax.swing.*;
public class FunWithPanels extends JFrame {
public static void main(String[] args) {
FunWithPanels frame = new FunWithPanels();
frame.doSomething();
}
void doSomething() {
Container c = getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(new JButton("A"), BorderLayout.NORTH);
p1.add(new JButton("B"), BorderLayout.WEST);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(3, 2));
p2.add(new JButton("F"));
p2.add(new JButton("G"));
p2.add(new JButton("H"));
p2.add(new JButton("I"));
p2.add(new JButton("J"));
p2.add(new JButton("K"));
JPanel p3 = new JPanel();
p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
p3.add(new JButton("L"));
p3.add(new JButton("M"));
p3.add(new JButton("N"));
p3.add(new JButton("O"));
p3.add(new JButton("P"));
c.setLayout(new BorderLayout());
c.add(p1, BorderLayout.CENTER);
c.add(p2, BorderLayout.SOUTH);
c.add(p3, BorderLayout.EAST);
pack();
setVisible(true);
}
}
I do not understand how "doSomething" use the fact that "frame" is an instance of the class JFrame. It is not clear to me because there is no reference to "this" in the code for the method "doSomething".
ADDED:
Maybe this is related. In this code:
import java.awt.*;
import java.applet.Applet;
public class ButtonGrid extends Applet {
public void init() {
setLayout(new GridLayout(3,2));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
}
}
In the "init" method we use methods "setLayout" and "add" and we do not bind them to a particular object (for example objectName.setLayout(...)
). Why is that?