lab 4: guis, panels, mouse and key listeners icom4015: fall 2014 a n i ntroduction to p anels and l...

9
Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 AN INTRODUCTION TO PANELS AND LAYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H. CHINAEI

Upload: kelly-harmon

Post on 17-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

JPanel(cont.)  To create a new JPanel, we need to use the default constructor.  To add a component to a panel, we invoke the add(Component comp) method on the panel. Keep in mind that you can only add objects of classes that extend/inherit from JComponent. You can easily look this up in the Java API. Note: The order in which you add Components can matter. It depends on the Layout you are working with.  Finally, to set the layout of a panel to a specifc layout manager, we invoke the setLayout(LayoutManager mgr) method on the panel. We can create a new object of the corresponding Layout class as a parameter.

TRANSCRIPT

Page 1: Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H

Lab 4: GUIs, Panels, Mouse and Key ListenersICOM4015: FALL 2014AN INTRODUCTION TO PANELS AND LAYOUTSCREATED BY KATYA I. BORGOSREVISED BY AMIR H. CHINAEI

Page 2: Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H

JPanelsThe JPanel class provides general-purpose containers, known as panels.

These panels allow us to organize the components that make up our GUI; Each component will represent an important element of our GUI such as a button, label, etc.

JPanels can be given specific layouts to reflect how we want the GUI components to be organized.

Page 3: Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H

JPanel(cont.)To create a new JPanel, we need to use the default constructor.

To add a component to a panel, we invoke the add(Component comp) method on the panel. Keep in mind that you can only add objects of classes that extend/inherit from JComponent. You can easily look this up in the Java API. Note: The order in which you add Components can matter. It depends on the Layout you are working with.

Finally, to set the layout of a panel to a specifc layout manager, we invoke the setLayout(LayoutManager mgr) method on the panel. We can create a new object of the corresponding Layout class as a parameter.

Page 4: Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H

Our Goal

Page 5: Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H

GridLayoutA JPanel with a GridLayout manager places the components that are added to it in a grid of cells.

Each component takes all the available space within its cell, and each cell is exactly the same size.

Components are added starting at the first row until it is filled, then the second row, and so on.

Example:JPanel grid = new JPanel();grid.setLayout(new GridLayout(5, 4));JButton rTCButton = new JButton(“Rtc”);grid.add(rTCButton);JButton cEButton = new JButton(“CE”);grid.add(cEButton);

Page 6: Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H

BoxLayoutA BoxLayout manager arranges components either on top of each other or in a row.

The way to state which orientation you would like the BoxLayout to have is by establishing a constant when you use its constructor. These constants are:BoxLayout.Y_AXIS (stacks components on top of

each other)BoxLayout.X_AXIS (places components in a row)

Example:JPanel box = new JPanel();box.setLayout(box, BoxLayout.Y_AXIS);

Page 7: Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H

BorderLayoutA JPanel with a set BorderLayout manager arranges and resizes its components to fit in five regions: north, south, east, west, and center.

Each region may contain only one component. However, that one component may have several components.

Just like the BoxLayout, the regions are identified by a corresponding constant:BorderLayout.NORTHBorderLayout.WESTBorderLayout.CENTERBorderLayout.SOUTHBorderLayout.EAST

Page 8: Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H

BorderLayout(cont.)Example:

JPanel myPanel = new JPanel();myPanel.setLayout(new BorderLayout());JButton button = new Jbutton();myPanel.add(button, BorderLayout.EAST);

Unlike BoxLayout, you specify locations on the BorderLayout when you add a component.

Page 9: Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H

Listeners Hints:

Use the methods added to the WindowMainPanel class to your convenience (setDisplayText(), getDisplayText(), setStatusBarText() and getStatusBarText().

Consider using substrings and String concatenation.Look up the String class method substring(int

beginIndex, int endIndex) in the Java API.String concatenation is the operation of joining

character strings end-to-end using the plus (+) operator.For example, System.out.println(“Hello, “ +

“world!”); prints “Hello, world!” to the console.