LayoutManager kan godt give folk grå hår de første gange.
Prøv at bygge og køre dette program.
LMFun.java:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class LMFun extends JFrame {
private static final long serialVersionUID = 1L;
public LMFun() {
setTitle("LayoutManager fun");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JLabel("West"), BorderLayout.WEST);
getContentPane().add(new JLabel("East"), BorderLayout.EAST);
JPanel north = new JPanel();
north.setLayout(new FlowLayout(FlowLayout.LEFT));
north.add(new JLabel("North 1"));
north.add(new JLabel("North 2"));
north.add(new JLabel("North 3"));
getContentPane().add(north, BorderLayout.NORTH);
JPanel south = new JPanel();
south.setLayout(new FlowLayout(FlowLayout.LEFT));
south.add(new JLabel("South 1"));
south.add(new JLabel("South 2"));
south.add(new JLabel("South 3"));
getContentPane().add(south, BorderLayout.SOUTH);
JPanel center = new JPanel();
center.setLayout(new GridLayout(2, 3));
center.add(new JLabel("Center (1,1)"));
center.add(new JLabel("Center (1,2)"));
center.add(new JLabel("Center (1,3)"));
center.add(new JLabel("Center (2,1)"));
center.add(new JLabel("Center (2,2)"));
center.add(new JLabel("Center (2,3)"));
getContentPane().add(center, BorderLayout.CENTER);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new LMFun();
f.setVisible(true);
}
});
}
}