Download presentation
Presentation is loading. Please wait.
Published byMavis Robertson Modified over 9 years ago
2
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button (Buttons other objects generate events: Source ) Object e.g. button OS Interrupt Listener or middleman (interface) Generates an Event
3
GUI Components and Screen Design GUI Demo Message Hello World CloseClearDisplay Panel Instance Textfield Instance Label Instance Button Instance Frame Instance
4
JButton button = new JButton("Add Interest"); JLabel label = new JLabel("balance: " + account.getBalance()); JPanel panel = 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
Layout Managers
6
FlowLayout GridLayout BorderLayout BoxLayout CardLayout GridBagLayout
7
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
Defines five areas or regions into which components can be added North South CenterEastWest
9
package buttonapp; import javax.swing.Jframe; public class ButtonTester { public static void main(String[] args) { JFrame frame; ButtonPanel b = 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
package buttonapp; import javax.swing.JButton; import javax.swing.JPanel; public class ButtonPanel extends JPanel { private JButton button1; public ButtonPanel () { button1 = new JButton ("Click Me"); this.add(button1); } }
11
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
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 (, )
13
LAB
14
1. SmartButton 2. Passing a panel as a parameter
15
Choices Radio buttons Check boxes Combo boxes
16
Radio Buttons mutually exclusive choices When a button is selected, previously selected button in set is automatically turned off
17
Radio Buttons (example from WileyPlus) JRadioButton smallButton = new JRadioButton("Small"); JRadioButton mediumButton = new JRadioButton("Medium"); JRadioButton largeButton = 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);
18
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
19
Border Examples JPanel panel = new JPanel(); panel.setBorder(new EtchedBorder()); panel.setBorder(new TitledBorder(new EtchedBorder(), "Size")); Border with a title
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.