Download presentation
Presentation is loading. Please wait.
Published byKelly Harmon Modified over 9 years ago
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. CHINAEI
2
JPanels The 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.
3
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.
4
Our Goal
5
GridLayout A 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);
6
BoxLayout A 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);
7
BorderLayout A 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.NORTH BorderLayout.WEST BorderLayout.CENTER BorderLayout.SOUTH BorderLayout.EAST
8
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.
9
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.