1 / 19

Events and GUI Interfaces

Events and GUI Interfaces. Screen. Press Button. (Buttons other objects generate events: Source ). Event Notification. Text field Object Responder. JAVA AWT Environment: Messages are sent between JAVA Objects. Event Driven Programs. OS Interrupt. Generates an Event.

alma
Download Presentation

Events and GUI Interfaces

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. Events and GUI Interfaces

  2. Screen Press Button (Buttons other objects generate events: Source) Event Notification Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Event Driven Programs OS Interrupt Generates an Event Object e.g. button Listener or middleman (interface) Event Handler Methods

  3. GUI Components and Screen Design Panel Instance Frame Instance GUI Demo Textfield Instance Message Hello World Label Instance Button Instance Display Clear Close

  4. JButtonbutton = new JButton("Add Interest"); JLabellabel = new JLabel("balance: " + account.getBalance()); JPanelpanel = new JPanel() or extends JPanel; panel.add(button); panel.add(label); frame.add(panel); (or the class that extends a panel) frame.add (aClass);

  5. ScreenDesign Layout Managers

  6. Predefined Layout Manager Subclasses • FlowLayout • GridLayout • BorderLayout • BoxLayout • CardLayout • GridBagLayout

  7. Layout Managers • Every container has a default layout manager • Programmer can set the layout manager as well • Each layout manager has its own particular rules governing how components will be arranged • setLayout () method panel.setLayout (new BorderLayout() );

  8. North West Center East South Border Layout (default for JFrame) • Defines five areas or regions into which components can be added

  9. BorderLayout(default for JFrame) package buttonapp; import javax.swing.Jframe; public class ButtonTester { public static void main(String[] args) { JFrame frame; ButtonPanelb = new ButtonPanel(); frame = new JFrame ("Button Frame"); frame.setSize(300, 400); frame.setDefaultCloseOperation (javax.swing.JFrame.EXIT_ON_CLOSE); // frame.add(b); // or frame.add (b, BorderLayout.CENTER); frame.pack (); frame.setVisible(true); } }

  10. ButtonPanel package buttonapp; import javax.swing.JButton; import javax.swing.JPanel; public class ButtonPanel extends JPanel { private JButtonbutton1; public ButtonPanel() { button1 = new JButton ("Click Me"); this.add(button1); } } }

  11. Flow Layout • Flow layout puts as many components as possible on a row, then moves to the next row • Rows are created as needed to accommodate all of the components • Components are displayed in the order they are added to the container • Each row of components is centered horizontally in the window by default, but could also be aligned left or right • Also, the horizontal and vertical gaps between the components can be explicitly set

  12. Grid Layout • A grid layout presents a container’s components in a rectangular grid of rows and columns • One component is placed in each cell of the grid, and all cells have the same size • As components are added to the container, they fill the grid from left-to-right and top-to-bottom (by default) • The size of each cell is determined by the overall size of the container setLayout (new GridLayout (<row>, <col>)

  13. Project 3 SmartButton Passing a panel as a parameter

  14. Choices • Radio buttons • Check boxes • Combo boxes

  15. Radio Buttons • mutually exclusive choices • When a button is selected, previously selected button in set is automatically turned off

  16. Radio Buttons (example from WileyPlus) JRadioButtonsmallButton = new JRadioButton("Small"); JRadioButtonmediumButton = new JRadioButton("Medium");JRadioButtonlargeButton = new JRadioButton("Large");// Add radio buttons into a ButtonGroup so that // only one button in group is on at any time ButtonGroup group = new ButtonGroup(); group.add(smallButton); group.add(mediumButton); group.add(largeButton);

  17. Radio Buttons • Button group does not place buttons close to each other on container • It is your job to arrange buttons on screen • isSelected: called to find out if a button is currently selected or not if(largeButton.isSelected()) size = LARGE_SIZE • Call setSelected(true) on a radio button in group before making the enclosing frame visible

  18. Border Examples • JPanel panel = new JPanel(); panel.setBorder(new EtchedBorder()); • panel.setBorder(new TitledBorder(new EtchedBorder(), "Size")); Border with a title

More Related