90 likes | 184 Views
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. JPanels. The JPanel class provides general-purpose containers, known as panels.
E N D
Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: Fall 2014 An Introduction to Panels and Layouts Created By katyai. borgos Revised By Amir H. Chinaei
JPanels • The JPanelclass 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 howwe want the GUI components to be organized.
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.
GridLayout • A JPanel with a GridLayoutmanager 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)); • JButtonrTCButton = new JButton(“Rtc”); • grid.add(rTCButton); • JButtoncEButton= new JButton(“CE”); • grid.add(cEButton);
BoxLayout • A BoxLayoutmanager 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);
BorderLayout • A JPanel with a set BorderLayoutmanager 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
BorderLayout(cont.) • Example: • JPanelmyPanel= 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.
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(intbeginIndex, intendIndex) 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.