1 / 9

Lab 4: GUIs , Panels , Mouse and Key Listeners

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.

Download Presentation

Lab 4: GUIs , Panels , Mouse and Key Listeners

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  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 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);

  6. 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);

  7. 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

  8. 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.

  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(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.

More Related