CPSC 124, Spring 2021: Sample Answers to Quiz #7
These are sample answers only.
Other answers can receive full credit.
Question 1. Briefly explain what is meant by each of the following aspects of GUI programming:
a) events: GUI programs are driven by events. which are generated by user actions (or other things, like a timer or a network connection). Examples would include clicking the mouse, pressing a key on the keyboard, or selecting a command from a menu. The GUI is programmed by writing event-handlers that specify what happens when an event occurs.
b) components: The interface of a GUI program is built out of components, which are visual elements that can appear on the screen such as buttons, menus, canvases, and windows.
c) layout: The various components that make up the contents of a window in a GUI program have to be arranged in the window. This is called layout. Layout means setting the size and position of each of the components that appear in the window. In JavaFX, layout is usually done by adding the components to a container, such as a BorderPane, that implements some policy about how its contents should be laid out.
Question 2. Write JavaFX code that will create a Button that contains the text "Clear", and installs an event handler for the ActionEvent that is generated when the user clicks the button. The event handler should simply call the method doClear() (which would have to be defined elsewhere).
Button clearBtn = new Button("clear"); clearBtn.setOnAction( e -> doClear() );
Question 3. What would the following JavaFX code accompilish in a start() method? What would makeButtonBar() have to be?
Canvas canvs = new Canvas(800,600); BorderPane pane = new BorderPane(canvas); pane.setBottom( makeButtonBar() );
Answer. This would lay out components for the program's GUI. A BorderPane is a container that has five possible positions for the components that it contains: center, top, bottom, left, and right. Here, it has an 800-by-600 canvas in its center position. The third line would add another component below the canvas, in the bottom position of the BorderPane. The method makeButtonBar() would have to create that component (presumeably a container of some kind that holds a row of buttons, but it could be any component).