Java Reference
In-Depth Information
interfaces, especially once you start nesting containers and also throw smart components such
as JTabbedFrame or JSplitPane in the mix. Later in this chapter, however, you'll get a quick
tour of some visual GUI builders, which also allow you to see which kind of GroupLayout - or
SpringLayout ‐based code these editors come up with.
absolute positioning (no layout manager)
Earlier in this chapter, you read that it is also possible to pass null as a layout manager to compo-
nents. Doing so allows you to leave the positioning of components entirely up to you. Now explore
this with a simple example in the following Try It Out.
Going Solo: absolute positioning Without a Layout Manager
try it out
In this Try It Out, you see how to lay out components manually, without using a layout manager.
1.
As always, feel free to create a new project in Eclipse when you want to. Create a class called
ManualLayoutFrame with the following content:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ManualLayoutFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Manual layout frame");
frame.getContentPane().setLayout(null);
JPanel bluePanel = new JPanel(),
redPanel = new JPanel(),
greenPanel = new JPanel();
bluePanel.setBackground(Color.blue);
redPanel.setBackground(Color.red);
greenPanel.setBackground(Color.green);
bluePanel.setBounds(100, 100, 100, 100);
redPanel.setBounds(50, 200, 400, 200);
greenPanel.setBounds(150, 100, 50, 50);
frame.add(bluePanel);
frame.add(redPanel);
frame.add(greenPanel);
frame.pack();
frame.setVisible(true);
frame.setSize(500, 500);
}
}
 
Search WWH ::




Custom Search