עקרונות תכנות מונחה עצמים תרגול 5 - GUI
בשיעור הקודם : Introduction to GUI Swing Basic components Event handling
Outline Containers Layouts Advanced examples
Top-Level Containers Swing uses tree top-level contauners:
Top-Level Containers
General-Purpose Containers JPanel Contains other components (JButton,JLabel,…) Can be contained by Top-Level Containers We can set its own features such as Background Color, Layout, size,etc... JPanel buttonPanel = new JPanel( new FlowLayout()); buttonPanel.setBackground(Color.magenta); buttonPanel.add(convertButton); buttonPanel.add(resetButton); buttonPanel.add(changeButton); getContentPane().add(buttonPanel);
General-Purpose Containers Scroll Pane Takes another component(the ViewportView) and displays it with scrollers (if necessary) textArea = new JTextArea(10,30); JScrollPane sp = new JScrollPane(textArea); …
General-Purpose Containers Split Pane Takes two component and displays them with a moveable splitter in the middle splitPane = new JSplitPane(Jsplitpane.HORIZONTAL_SPLIT, listScrollPane, pictureScrollPane); splitPane.setDividerLocation(150);
General-Purpose Containers Tabbed Pane Can be given any number of components as tabs by using addTab JTabbedPane tp = new JTabbedPane(); Tp.addTab(“Tab 1”, icon, panel1);
General-Purpose Containers Toolbar Lays out buttons conveniently
Layouts
Simple Layouts
Example 1 PIZZA PANEL
Example 1: Pizza panel
The Pizza Class
Recalculating
Making Spinners
Making Checkboxes
Making the Frame
Toppings Panel
Cost Panels
Wrapping Up the Frame
Advanced Layouts Spring Layout Allows specifying distances between pairs of components (“springs”)
Spring Layout
Advanced Layouts GridBag Layout Like grid, but inner components can span over several cells and respond differently to re-sizing.
GridBag Constraints
Desigging GridBag Layouts
Example 2 Personal Details Panel
Reminder: Gridbag Constaints
Personal Details Panel
Personal Details Panel Constructor
Prototype Constraints
Name and Surname
Gender Panel
Age Spinner
Address
Comments Panel
Putting in all together
Layout Considerations What to consider when planning a layout? Not distorted by resize Aligned, visually attractive Put important data in the center, auxiliaries on the sides Flexible, user-controlled Usable