12-Layout - Sophisticated Layout Management 1 Layout...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 1 Sophisticated Layout Management 2 Layout Management In AWT, components are laid out inside containers Containers such as panels can be put inside other containers To organize the components, specify a layout manager by calling setLayout method The add method of the container passes the component and any placement instructions to the layout manager
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 3 Why Layout Management? Windows programming Drag and drop Layout management is not a big deal Problem – the resulting layout must be manually updated if the size of the components changes Font size changes, translation, etc. Windows dialog editor merely remembers the pixel position and size of each component It does not remember why they were arranged in this fashion 4 Layout Management With Java layout managers, the layout comes with instructions about the relationships between the components Multiple platforms Look and feel You can rely on the flexibility of layout managers to arrange the components To achieve complex layouts Use a sophisticated layout manager Appropriate use of multiple panels
Background image of page 2
3 5 Grid Layout Cells are always the same size panel.setLayout(new GridLayout(5, 4)); You can specify horizontal and vertical gaps (in pixels) panel.setLayout(new GridLayout(5, 4, 3, 3)); When the window is resized, the buttons grow and shrink, but all buttons still have identical sizes 6 Calculator import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator { public static void main(String[] args) { CalculatorFrame frame = new CalculatorFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class CalculatorFrame extends JFrame { public CalculatorFrame() { setTitle("Calculator"); Container contentPane = getContentPane(); CalculatorPanel panel = new CalculatorPanel(); contentPane.add(panel); pack(); } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 7 class CalculatorPanel extends JPanel { public CalculatorPanel() { setLayout(new BorderLayout()); result = 0; lastCommand = "="; start = true; display = new JButton("0"); display.setEnabled(false); add(display, BorderLayout.NORTH); ActionListener insert = new InsertAction(); ActionListener command = new CommandAction(); panel = new JPanel(); panel.setLayout(new GridLayout(4, 4)); addButton("7", insert); addButton("8", insert); addButton("9", insert); addButton("/", command); 8 addButton("4", insert); addButton("5", insert); addButton("6", insert); addButton("*", command); addButton("1", insert); addButton("2", insert); addButton("3", insert); addButton("-", command); addButton("0", insert); addButton(".", insert); addButton("=", command); addButton("+", command); add(panel, BorderLayout.CENTER); } private void addButton(String label, ActionListener listener) { JButton button = new JButton(label); button.addActionListener(listener); panel.add(button); }
Background image of page 4
5 private class InsertAction implements ActionListener { public void actionPerformed(ActionEvent event) { String input = event.getActionCommand(); if (start) { display.setText(""); start = false; } display.setText(display.getText() + input); } } private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); if (start) { if (command.equals("-")) { display.setText(command); start = false; } else
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2011 for the course CSE 333 taught by Professor Zao during the Fall '10 term at SUNY Stony Brook.

Page1 / 17

12-Layout - Sophisticated Layout Management 1 Layout...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online