190 likes | 338 Views
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.
E N D
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
GUI Components and Screen Design Panel Instance Frame Instance GUI Demo Textfield Instance Message Hello World Label Instance Button Instance Display Clear Close
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);
ScreenDesign Layout Managers
Predefined Layout Manager Subclasses • FlowLayout • GridLayout • BorderLayout • BoxLayout • CardLayout • GridBagLayout
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() );
North West Center East South Border Layout (default for JFrame) • Defines five areas or regions into which components can be added
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); } }
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); } } }
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
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>)
Project 3 SmartButton Passing a panel as a parameter
Choices • Radio buttons • Check boxes • Combo boxes
Radio Buttons • mutually exclusive choices • When a button is selected, previously selected button in set is automatically turned off
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);
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
Border Examples • JPanel panel = new JPanel(); panel.setBorder(new EtchedBorder()); • panel.setBorder(new TitledBorder(new EtchedBorder(), "Size")); Border with a title