Oop lecture9 10

304 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
304
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Oop lecture9 10

  1. 1. Lecture 10Swing layouts Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  2. 2. Specifying no layout in JFramepublic class DefaultLayoutDemo { public static void main(String[] args) { JFrame frame = new JFrame("DefaultLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. frame.add(new JButton("a")); frame.add(new JButton("b")); //Display the window. frame.pack(); frame.setVisible(true); }}
  3. 3. Specifying positions of controlspublic class DefaultLayoutDemo { public static void main(String[] args) { JFrame frame = new JFrame("DefaultLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. frame.add(new JButton("a"), BorderLayout.NORTH); frame.add(new JButton("b"), BorderLayout.SOUTH); //Display the window. frame.pack(); frame.setVisible(true); }} The default layout for JFrame is BorderLayout
  4. 4. BorderLayoutpublic class BorderLayoutDemo { public static boolean RIGHT_TO_LEFT = false; public static void addComponentsToPane(Container pane) { if (RIGHT_TO_LEFT) { pane.setComponentOrientation( java.awt.ComponentOrientation.RIGHT_TO_LEFT); } JButton button = new JButton("Button 1 (PAGE_START)"); pane.add(button, BorderLayout.PAGE_START); button = new JButton("Button 2 (CENTER)"); button.setPreferredSize(new Dimension(200, 100)); pane.add(button, BorderLayout.CENTER); button = new JButton("Button 3 (LINE_START)"); pane.add(button, BorderLayout.LINE_START); button = new JButton("Long-Named Button 4 (PAGE_END)"); pane.add(button, BorderLayout.PAGE_END); button = new JButton("5 (LINE_END)"); pane.add(button, BorderLayout.LINE_END); }
  5. 5. public static void main(String[] args) { JFrame frame = new JFrame("BorderLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); } }If you stretch the frame the center part gets filled by the control in it
  6. 6. BoxLayoutpublic class BoxLayoutDemo { public static void addComponentsToPane(Container pane) { pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); addAButton("Button 1", pane); addAButton("Button 2", pane); addAButton("Button 3", pane); addAButton("Long-Named Button 4", pane); addAButton("5", pane); } private static void addAButton(String text, Container container) { JButton button = new JButton(text); button.setAlignmentX(Component.CENTER_ALIGNMENT); container.add(button); } public static void main(String[] args) { JFrame frame = new JFrame("BoxLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); }}
  7. 7. ResultThere is no resizing when the frame is stretched out
  8. 8. FlowLayoutpublic class FlowLayoutDemo extends JFrame{ FlowLayout experimentLayout = new FlowLayout(); public FlowLayoutDemo(String name) { super(name); } public void addComponentsToPane(final Container pane) { JPanel compsToExperiment = new JPanel(); compsToExperiment.setLayout(experimentLayout); experimentLayout.setAlignment(FlowLayout.LEADING); //Add buttons to the experiment layout compsToExperiment.add(new JButton("Button 1")); compsToExperiment.add(new JButton("Button 2")); compsToExperiment.add(new JButton("Button 3")); compsToExperiment.add(new JButton("Long-Named Button 4")); compsToExperiment.add(new JButton("5")); //Left to right component orientation is selected by default compsToExperiment.setComponentOrientation( ComponentOrientation.LEFT_TO_RIGHT); pane.add(compsToExperiment, BorderLayout.CENTER); }
  9. 9. public static void main(String[] args) { FlowLayoutDemo frame = new FlowLayoutDemo("FlowLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); }} If we stretch the controls are still pinned to the left because of this: experimentLayout.setAlignment(FlowLayout.LEADING);
  10. 10. GridLayoutpublic class GridLayoutDemo { public static void addComponentsToPane(Container pane) { pane.setLayout(new GridLayout(3, 2)); JButton b1 = new JButton("First"); pane.add(b1); b1 = new JButton("Second"); pane.add(b1); b1 = new JButton("Third"); pane.add(b1); b1 = new JButton("Fourth"); pane.add(b1); b1 = new JButton("Fifth"); pane.add(b1); b1 = new JButton("Sixth"); pane.add(b1); } public static void main(String[] args) { JFrame frame = new JFrame("GridBagLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); }
  11. 11. Result3 columns and 2 rows as set in:pane.setLayout(new GridLayout(3, 2));
  12. 12. GridBagLayoutpublic class GridBagLayoutDemo { public static void addComponentsToPane(Container pane) { JButton button; pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); button = new JButton("Button 1"); c.gridx = 0; c.gridy = 0; pane.add(button, c); button = new JButton("Button 2"); c.gridx = 1; c.gridy = 0; pane.add(button, c); button = new JButton("Button 3"); c.gridx = 2; c.gridy = 0; pane.add(button, c); button = new JButton("Long-Named Button 4"); c.gridx = 0; c.gridy = 1; pane.add(button, c);
  13. 13. button = new JButton("5"); c.gridx = 1; c.gridy = 2; pane.add(button, c); } public static void main(String[] args) { JFrame frame = new JFrame("GridBagLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); }} GridBagLayout is very flexible layout
  14. 14. Questions What is the most flexible of these layouts Whichone is easy to implement Which layout to choose when you want your control to take up as much space as possible Interested in more? - GroupLayout SpringLayout

×