Object-Oriented Software Engineering JFrames with Swing
Jan 05, 2016
Object-Oriented Software Engineering
JFrames with Swing
22UniS
Contents
• JFrame
• Content Pane
• Layout Managers
• Java Interfaces
• ActionListeners
33UniS
JFrameAny interesting GUI will use the JFrame class.
public class FrameDemo { private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }}
44UniS
JFrameprivate static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.pack(); frame.setVisible(true); }
55UniS
JFrame
Problem is createAndShowGUI is declared static:
private static void createAndShowGUI()
Static methods can not reference any non-static variables.This includes any reference to a non-static class.
This has to be, because main has to be static, and can onlyreference static methods.
66UniS
JFrame
For example, move declaration of frame outside scopeof createAndShowGUI:
private JFrame frame;private static void createAndShowGUI() {
............frame = new JFrame("FrameDemo");............
}
$ javac FrameDemo.javaFrameDemo.java:10: non-static variable frame cannot be referenced from a static context frame = new JFrame("FrameDemo");
77UniS
Content Pane
The menu bar and content pane provide functionalityfor a JFrame.
88UniS
Content Pane, JPanel
public class MyContents extends JPanel {public MyContents ( ) {// put contents of JFrame in this// non-static constructor method}private static void createAndShowGUI() {.....................JFrame frame = new JFrame("Person");.....................MyContents newContentPane = new MyContents( );.....................frame.setContentPane(newContentPane);.....................}
}
99UniS
Layout Managers
A JPanel must have a layout manager to control wherebuttons, icons, text areas etc are to be placed with respect toeach other inside the content pane.
newContentPane LayoutManager
BorderLayout
CardLayout
FlowLayout
GridLayout
GridBagLayout
JPanel
{ choice of } *
*
*
*
*
1010UniS
BorderLayout
Every content pane is initialized to use a BorderLayout.
BorderLayout has five areas. These areas are specified by the BorderLayout constants • PAGE_START, • PAGE_END, • LINE_START, • LINE_END, • and CENTER.
1111UniS
Code for adding buttons to BorderLayout
JButton button = new JButton("Button 1 (PAGE_START)"); newContentPane.add(button, BorderLayout.PAGE_START);
//Make the center component big, since that's the //typical usage of BorderLayout. button = new JButton("Button 2 (CENTER)"); button.setPreferredSize(new Dimension(200, 100)); newContentPane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");newContentPane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)"); newContentPane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)"); newContentPane.add(button, BorderLayout.LINE_END);
Code taken from Java tutorial, also on course website.
1212UniS
BorderLayout: Note
Behaviour of buttons depends on the object towhich it belongs.
In a later lecture we will see that buttons attached toJToolBar will seem to behave differently when the JToolBar is then attached toa BorderLayout within a JFrame!
1313UniS
BoxLayout
BoxLayout stacks components either as column or in row.
1414UniS
BoxLayout Code
public static void addComponentsToPane(Container pane) { pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
addAButton("Button 1", pane); addAButton("Button 2", pane); addAButton("Button 3", pane); addAButton("Long-Named Button 4", pane); addAButton("5", pane); }
private static void addAButton(String text, Container container) { JButton button = new JButton(text); button.setAlignmentX(Component.CENTER_ALIGNMENT); container.add(button); }
Code taken from Java tutorial, also on course website.
1515UniS
GridBagLayout
• GridBagLayout aligns components by placing them within a grid of cells
• Components can span more than one cell.
• The rows in the grid can have different heights, and grid columns can have different widths.
1616UniS
GridBagLayout
Grid (0,0) Grid (0,1) Grid (0,2)
Grid (1,0)
Grid (1,1)
Grid (1,2)
Grid (2,0) Grid (2,1) Grid (2,2)
1717UniS
GridBagLayout
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
Constraint controls where buttons appear within the GridBagLayout.
Resizing component will causehorizontal space to be sharedacross buttons
1818UniS
GridBagLayout button = new JButton("Button 1"); c.gridx = 0; c.gridy = 0; pane.add(button, c);
button = new JButton("Button 2"); c.gridx = 1; c.gridy = 0; pane.add(button, c);
button = new JButton("Button 3"); c.gridx = 2; c.gridy = 0; pane.add(button, c);
button = new JButton("Long-Named Button 4"); c.ipady = 40; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 1; pane.add(button, c);
1919UniS
GridBagLayout
button = new JButton("5"); c.ipady = 0; //reset to default c.weighty = 1.0; //request any extra vertical space c.anchor = GridBagConstraints.PAGE_END; //bottom of space c.insets = new Insets(10,0,0,0); //top padding c.gridx = 1; //aligned with button 2 c.gridwidth = 2; //2 columns wide c.gridy = 2; //third row pane.add(button, c);
2020UniS
Event ListenersFor a class to provide interaction it might implement the ActionListener and/or the MouseInputListener interfaces in Java as well as extend JPanel.
MyNewClass
«interface» ActionListenerJPanel «interface»
MouseInputListener
JFrame
1
1..*
2121UniS
Java Interfaces
• Interface represents a contract defining a protocol.
• An interface is a named collection of method definitions, without implementations.An interface can also declare constants.
• An interface cannot implement any methods.
• An interface has no constructor.
• All interface methods are public.
• No static methods allowed.
• An interface is not part of the class hierarchy. Unrelated classes can implement the same interface.
• A class can implement many interfaces but can have only one superclass.
2222UniS
MouseInputListener and ActionListener <<interface>>
MouseInputListener
void mouseClicked (MouseEvent)
void mouseMoved (MouseEvent)
void mouseExited (MouseEvent)
void mouseReleased (MouseEvent)
void mouseEntered (MouseEvent)
void mousePressed (MouseEvent)
void mouseDragged (MouseEvent)
void actionPerformed(ActionEvent)
ActionListener
2323UniS
Handling Events
• GUI Components communicate with each other by sending events.
• A listener is an object that is notified when an eventoccurs and can examine the contents of that eventto perform some action.
Event Listener ObjectGUI Component
Eventfired by GUI
Registers to listen for particular type of events
2424UniS
Handling Events
Event Listener ObjectEvent Listener Object
Event Listener ObjectEvent Listener Object
Event Listener ObjectEvent Listener Object
GUI Component
• Any number of objects can register to listen forsame type of event
• Events are multicast to all objects that are• listening
Event
Event
Event
2525UniS
Common Events and their Listeners<<interface>>
java.util.EventListenerjava.awt.AWTEvent
MouseEvent
ActionEvent
<<interface>>
MouseListener
<<interface>>
MouseMotionListener
<<interface>> ActionListener
2626UniS
Listening For Events
aSource aListenerregister( )
anEvent<<create>>
query( )
notify(anEvent )
2727UniS