More on adding components Adding a button to a panel: - - PowerPoint PPT Presentation

more on add ing components
SMART_READER_LITE
LIVE PREVIEW

More on adding components Adding a button to a panel: - - PowerPoint PPT Presentation

More on adding components Adding a button to a panel: buttonPanel.add (clickButton); buttonPanel lays out its components like words on a page Adding a panel to the frame: getContentPane().add (buttonPanel, BorderLayout.CENTER);


slide-1
SLIDE 1

More on “add”ing components

Adding a button to a panel: buttonPanel.add (clickButton);

➡ buttonPanel lays out its components like words on a

page Adding a panel to the frame:

getContentPane().add (buttonPanel, BorderLayout.CENTER);

➡ contentPane lays out its components using compass

directions: NORTH, SOUTH, EAST, WEST and CENTER

FlowLayout

FlowLayout lays components out from left-to-right, top-to-bottom, like words on a page Default for a JPanel.

1 2 Tuesday, February 9, 2010

slide-2
SLIDE 2

BorderLayout

NORTH WEST EAST SOUTH BorderLayout lays out components using compass directions + CENTER Default for a JFrame’ s content pane.

BorderLayout Rules

One component per compass point Component expands to fill the entire area

3 4 Tuesday, February 9, 2010

slide-3
SLIDE 3

What Gives???

There are 2 buttons in the EAST! The buttons do not fill the entire east area! Conclusion: My professor lied to me! EAST

Grouping Components with JPanel

JPanel buttonPanel = new JPanel(); buttonPanel.add (new JButton("Place order")); buttonPanel.add (new JButton("Cancel order")); contentPane.add(buttonPanel, BorderLayout.EAST);

5 6 Tuesday, February 9, 2010

slide-4
SLIDE 4

FlowLayout

A JPanel uses FlowLayout as its layout manager rather than BorderLayout. With BorderLayout, we say: contentPane.add (buttonPanel, BorderLayout.SOUTH); With FlowLayout, we omit the compass direction: buttonPanel.add(easy); Components are added from left to right.

FlowLayout

buttonPanel.add (orderButton); buttonPanel.add (cancelButton); There are 2 buttons in the button panel. There is 1 button panel in the EAST. FlowLayout sizes components in a more natural way. Conclusion: My professor didn’ t lie to me!

7 8 Tuesday, February 9, 2010

slide-5
SLIDE 5

BoxLayout

JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); buttonPanel.add (new JButton("Place order")); buttonPanel.add (new JButton("Cancel order")); contentPane.add(buttonPanel, BorderLayout.EAST);

BoxLayout

BoxLayout constructor: BoxLayout(Container target, int axis) target is the component whose layout we are setting axis is either BoxLayout.X_AXIS or BoxLayout.Y_AXIS colorPanel.setLayout ( new BoxLayout (colorPanel, BoxLayout.Y_AXIS));

9 10 Tuesday, February 9, 2010

slide-6
SLIDE 6

JPanel gives “natural” size

contentPane.add(comboBox, BorderLayout.SOUTH); toppingsPanel.add (comboBox); contentPane.add(toppingsPanel, BorderLayout.SOUTH);

GridLayout

Specify # of rows and # of columns to create a grid Each component gets the same size

11 12 Tuesday, February 9, 2010

slide-7
SLIDE 7

GridLayout

JPanel sliderPanel = new JPanel(); / / Use 3 rows and as many columns as necessary. sliderPanel.setLayout (new GridLayout(3, 0)); redSlider = new JSlider(...); redLabel = new JLabel("Red", JLabel.RIGHT); redValueLabel = new JLabel("0", JLabel.LEFT); / / Do similar stuff for blue and green / / Add red components. Each component goes / / in the next place in the grid. sliderPanel.add(redLabel); sliderPanel.add(redSlider); sliderPanel.add(redValueLabel); / / Add green and blue components Container contentPane = getContentPane(); contentPane.add(sliderPanel, BorderLayout.SOUTH);

JLabels

public void begin() { ... redSlider = new JSlider(...); redLabel = new JLabel("Red", JLabel.RIGHT); redValueLabel = new JLabel("0", JLabel.LEFT); ... } public void stateChanged(ChangeEvent evt) { / / Get component color values & update label int redValue = redSlider.getValue(); redValueLabel.setText("" + redValue); / / Repeat for blue and green / / Create the new color and make it the color for colorRect Color newColor = new Color(redValue, greenValue, blueValue); colorRect.setColor(newColor); }

13 14 Tuesday, February 9, 2010

slide-8
SLIDE 8

JLabel

JLabel constructor: JLabel(String text) JLabel(String text, int horizontalAlignment) where horizontalAlignment is JLabel.LEFT, JLabel.RIGHT or JLabel.CENTER Changing the label void setText(String text) A JLabel is for output only; there are no listeners or event handling associated with JLabels

Layout Manager Summary

BorderLayout - uses compass points FlowLayout - naturally-sized components, laid out left-to-right BoxLayout - X_AXIS or Y_AXIS GridLayout - fixed # of rows and columns, all cells equal size

15 16 Tuesday, February 9, 2010