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
GUI Components and Screen Design GUI Demo Message Hello World CloseClearDisplay Panel Instance Textfield Instance Label Instance Button Instance Frame Instance
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);
Layout Managers
FlowLayout GridLayout BorderLayout BoxLayout CardLayout GridBagLayout
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() );
Defines five areas or regions into which components can be added North South CenterEastWest
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); }
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); } }
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
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 (, )
LAB
1. SmartButton 2. 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) 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);
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