Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public.

Similar presentations


Presentation on theme: "Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public."— Presentation transcript:

1 Java layout managers

2 import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public ButtonPanel() { yellowButton = new JButton("Yellow"); blueButton = new JButton("Blue"); redButton = new JButton("Red"); readButton = new JButton("read this"); text = new JTextArea(1,20); label = new JLabel(); add(readButton); add(text); add(label); yellowButton.addActionListener(this); blueButton.addActionListener(this); redButton.addActionListener(this); readButton.addActionListener(this); }

3 public void actionPerformed(ActionEvent event) { Object source = event.getSource(); Color color = getBackground(); if (source == yellowButton) color = Color.yellow; else if (source == blueButton) color = Color.blue; else if (source == redButton) color = Color.red; else if (source == readButton) { System.out.println("read it"); System.out.println( text.getText() ); label.setText( text.getText() ); } setBackground(color); repaint(); } private JButton yellowButton; private JButton blueButton; private JButton redButton; private JTextArea text; private JButton readButton; private JLabel label; }

4 Why do we need Layout Managers? zWindow expanded but Components Stay put zcomponents designed for a specific look- and-feel or font size

5 What are Layout Managers? zA layout manager is a class that encapsulates an algorithm for positioning and sizing GUI components.

6 What are Layout Managers? zRather than building the layout algorithms into your code and watch for window resizing, the algorithm is kept separate. zThis allows that algorithm to be used for several applications, while simplifying the code.

7

8 FlowLayout z The components line up horizontally until there is no more room and then starts a new row of components.

9 BorderLayout zContainer contentPane = getContentPane(); zcontentPane.setLayout(new BorderLayout()); z zcontentPane.add(new JButton("Button 1 (NORTH)"), z BorderLayout.NORTH); zcontentPane.add(new JButton("2 (CENTER)"), z BorderLayout.CENTER); zcontentPane.add(new JButton("Button 3 (WEST)"), z BorderLayout.WEST); zcontentPane.add(new JButton("Long-Named Button 4 (SOUTH)"), z BorderLayout.SOUTH); zcontentPane.add(new JButton("Button 5 (EAST)"), z BorderLayout.EAST);

10 BoxLayout  The BoxLayout class puts components in a single row or column. zIt respects the components' requested maximum sizes, and also lets you align components.

11 GridLayout z Lays out components equal in size and displays them in the requested number of rows and columns

12 How to Choose a Layout Manager  Scenario: You need to display a component in as much space as it can get. -- use BorderLayout  Scenario: You need to display a few components in a compact row at their natural size. -- Consider using a JPanel to group the components and using either the JPanel's default FlowLayout manager or the BoxLayout manager FlowLayoutBoxLayout  Scenario: You need to display a few components of the same size in rows and columns -- GridLayout is perfect for this. GridLayout  Scenario: You need to display a few components in a row or column, possibly with varying amounts of space between them, custom alignment, or custom component sizes. -- BoxLayout BoxLayout

13 Homework zPlace 8 buttons on a container (e.g. JPanel) using FlowLayout, BorderLayout, BoxLayout (either horizontal or vertical) and GridLayout zYou can just re-compile the same Java program 4 times. Each time change the Layout Manager.


Download ppt "Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public."

Similar presentations


Ads by Google