Download presentation
1
Object-Oriented Software Engineering
JFrames with Swing
2
Contents JFrame Content Pane Layout Managers Java Interfaces ActionListeners
3
JFrame Any 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(); });
4
JFrame 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); }
5
JFrame Problem is createAndShowGUI is declared static:
private static void createAndShowGUI() This has to be, because main has to be static, and can only reference static methods. Static methods can not reference any non-static variables. This includes any reference to a non-static class.
6
JFrame For example, move declaration of frame outside scope
of createAndShowGUI: private JFrame frame; private static void createAndShowGUI() { frame = new JFrame("FrameDemo"); } $ javac FrameDemo.java FrameDemo.java:10: non-static variable frame cannot be referenced from a static context frame = new JFrame("FrameDemo");
7
Content Pane The menu bar and content pane provide functionality
for a JFrame.
8
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);
9
Layout Managers A JPanel must have a layout manager to control where buttons, icons, text areas etc are to be placed with respect to each other inside the content pane. JPanel LayoutManager newContentPane * BorderLayout { choice of } * CardLayout * FlowLayout * GridLayout * GridBagLayout
10
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.
11
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.
12
BorderLayout: Note Behaviour of buttons depends on the object to
which it belongs. In a later lecture we will see that buttons attached to JToolBar will seem to behave differently when the JToolBar is then attached to a BorderLayout within a JFrame!
13
BoxLayout BoxLayout stacks components either as column or in row.
14
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.
15
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.
16
GridBagLayout Grid (0,0) Grid (0,1) Grid (0,2) Grid (1,0) Grid (1,1)
17
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 cause horizontal space to be shared across buttons
18
GridBagLayout button = new JButton("Button 1"); c.gridx = 0;
c.gridy = 0; pane.add(button, c); button = new JButton("Button 2"); c.gridx = 1; button = new JButton("Button 3"); c.gridx = 2; button = new JButton("Long-Named Button 4"); c.ipady = 40; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridy = 1;
19
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);
20
Event Listeners For a class to provide interaction it might implement the ActionListener and/or the MouseInputListener interfaces in Java as well as extend JPanel. «interface» ActionListener JPanel «interface» MouseInputListener MyNewClass JFrame 1 1..*
21
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.
22
MouseInputListener and ActionListener <<interface>>
void mouseClicked (MouseEvent) void mouseMoved (MouseEvent) void mouseExited (MouseEvent) void mouseReleased (MouseEvent) void mouseEntered (MouseEvent) void mousePressed (MouseEvent) void mouseDragged (MouseEvent) ActionListener void actionPerformed(ActionEvent)
23
Handling Events GUI Components communicate with each other by sending events. A listener is an object that is notified when an event occurs and can examine the contents of that event to perform some action. Registers to listen for particular type of events GUI Component Event Listener Object Event fired by GUI
24
Handling Events Any number of objects can register to listen for same type of event Events are multicast to all objects that are listening Event GUI Component Event Event
25
Common Events and their Listeners
<<interface>> java.util.EventListener java.awt.AWTEvent ActionEvent MouseEvent <<interface>> ActionListener <<interface>> MouseListener <<interface>> MouseMotionListener
26
Listening For Events aSource aListener anEvent register( )
<<create>> notify(anEvent ) query( )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.