1 / 26

G raphical U ser I nterfaces GUIs

G raphical U ser I nterfaces GUIs. The Plan. Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI. Components. JLabel text/image display JTextField single line for text input/output JTextArea multiple lines for text input/output JButton used for decisions

Télécharger la présentation

G raphical U ser I nterfaces GUIs

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. Graphical User InterfacesGUIs

  2. The Plan • Components • Flat Layouts • Hierarchical Layouts • Designing a GUI • Coding a GUI

  3. Components • JLabeltext/image display • JTextFieldsingle line for text input/output • JTextAreamultiple lines for text input/output • JButtonused for decisions • JFramea basic window

  4. Components • JLabeltext/image display • JTextFieldsingle line for text input/output • JTextAreamultiple lines for text input/output • JButtonused for decisions • JFramea basic window

  5. Components • JLabeltext/image display • JTextFieldsingle line for text input/output • JTextAreamultiple lines for text input/output • JButtonused for decisions • JFramea basic window

  6. GridLayout BorderLayout Flat Layouts CENTER NORTH WEST EAST SOUTH

  7. GridLayout Added left to right, top to bottom Expands to fill horizontally and vertically Each space equal width and height BorderLayout Not all positions must be filled CENTER expands horizontally and vertically NORTH and SOUTH expand horizontally WEST and EAST expand vertically Flat Layouts

  8. Flat Layouts BorderLayout

  9. Flat Layouts GridLayout

  10. Hierarchical Layouts You can put layouts within layouts:

  11. Hierarchical Layouts Identify the BorderLayout and GridLayouts in the application on the right.

  12. Hierarchical Layouts CENTER EAST

  13. Hierarchical Layouts GridLayout

  14. Hierarchical Layouts GridLayout

  15. Hierarchical Layouts CENTER SOUTH

  16. Hierarchical Layouts CENTER SOUTH

  17. Hierarchical Layouts

  18. Hierarchical Layouts • Virtually every layout we make is a hierarchy of GridLayout and BorderLayout • Other Layouts include • BoxLayout • GridBagLayout • FlowLayout • CardLayout

  19. Designing a GUI • What components are needed? • Which components are of primary importance? Secondary? • How do the components relate to each other? • How big are the components? • How can they be arranged into BorderLayout and GridLayout?

  20. Coding a GUI • Declare the components as instance variables • Write a makeComponents method to initialize the components • Write a layoutComponents methods to arrange the components • Write a constructor to call the above two methods • Write a setVisible method to set the primary component’s visibility (usually a JFrame).

  21. Examples • BorderExample.java (today) • In code directory (GUIs.jar) • GridExample.java • CombinedExample.java

  22. BorderExample.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BorderExample extends JApplet { JFrame frame; JTextArea middle; JTextField bottom; JButton left, right; JLabel title;

  23. BorderExample.java private void makeComponents() { frame=new JFrame("BorderExample"); middle=new JTextArea(10, 40); bottom=new JTextField(); left=new JButton("left"); right=new JButton("right"); title=new JLabel("Title"); }

  24. BorderExample.java private void makeLayout() { Container container=frame.getContentPane(); container.setLayout(new BorderLayout()); container.add(new JScrollPane(middle), BorderLayout.CENTER); container.add(title, BorderLayout.NORTH); container.add(left, BorderLayout.WEST); container.add(right, BorderLayout.EAST); container.add(bottom, BorderLayout.SOUTH); frame.pack(); }

  25. BorderExample.java public BorderExample() { makeComponents(); makeLayout(); } public void setVisible(boolean vis) { frame.setVisible(vis); }

  26. BorderExample.java public void init() { main(null); } public static void main(String[] args) { BorderExample example=new BorderExample(); example.setVisible(true); }

More Related