






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lecture from Object Oriented Programming and Data Structures course with following key points: Graphical User Interface, Multi-Threaded, Placing Components, Jframe, Layout Manager, Basic Components, Boxlayout
Typology: Slides
1 / 11
This page cannot be seen from the preview
Don't miss anything!







GUI (Graphical User Interface)
JFrame object: associated with a window on your monitor. Generally, a GUI is a JFrame object with various components placed in it Class JFrame is in package javax.swing Some methods in a JFrame object hide() show() setVisible(boolean) getX() getY() (coordinates of top-left point) getWidth() getHeight() setLocation(int, int) getTitle() setTitle(String) getLocation() setLocation(int, int) Over 100 methods in a JFrame object!
Putting components in a JFrame import java.awt.; import javax.swing.; /** Demonstrate placement of components in a JFrame. Places five components in 5 possible areas: (1) a JButton in the east, (2) a JLabel in the west, (3) a JLabel in the south, (4) a JTextField in the north (5) a JTextArea in the center. / public class ComponentExample extends JFrame { /* Constructor: a window with title t and 5 components */ public ComponentExample(String t) { super (t); Container cp= getContentPane(); cp.add( new JButton("click me"), BorderLayout.EAST); cp.add( new JTextField("type here", 22), BorderLayout.NORTH); cp.add( new JCheckBox("I got up today"), BorderLayout.SOUTH); cp.add( new JLabel("label 2"), BorderLayout.WEST); cp.add( new JTextArea("type\nhere", 4, 10), BorderLayout.CENTER); pack(); } ComponentExample.java Add components to its contentPane
Packages --Components Packages that contain classes that deal with GUIs: java.awt: Old package. javax.swing: New package. javax.swing has a better way of listening to buttons, text fields, etc. Components are more flexible. JButton, Button: Clickable button JLabel, Label: Line of text JTextField, TextField: Field into which the user can type JTextArea, TextArea: Many-row field into which user can type JPanel, Panel: Used for graphics; to contain other components JCheckBox: Checkable box with a title JComboBox: Menu of items, one of which can be checked JRadioButton: Same functionality as JCheckBox Container: Can contain other components Box: Can contain other components Component : Something that can be placed in a GUI window. They are instances of certain classes, e.g. Jxxxx: in Swing, with xxxx in awt.
Components that can contain other components Component Box Container JComponent JPanel Panel Applet Window Frame JFrame JWindow java.awt is the old GUI package. javax.swing is the new GUI package. When they wanted to use an old name, they put J in front of it. (e.g. Frame and JFrame) When constructing javax.swing, the attempt was made to rely on the old package as much as possible. So, JFrame is a subclass of Frame. But they couldn’t do this with JPanel.
import java.awt.; import javax.swing.; /** Instance has labels in east /west, JPanel with four buttons in center. / public class PanelDemo extends JFrame { JPanel p= new JPanel(); /* Constructor: a frame with title "Panel demo", labels in east/west, blank label in south, JPanel of 4 buttons in the center */ public PanelDemo() { super ("Panel demo"); p.add( new JButton("0")); p.add( new JButton("1")); p.add( new JButton("2")); p.add( new JButton("3")); Container cp= getContentPane(); cp.add( new JLabel("east"), BorderLayout.EAST); cp.add( new JLabel("west"), BorderLayout.WEST); cp.add( new JLabel(" "), BorderLayout.SOUTH); cp.add(p, BorderLayout.CENTER); pack(); } }
JPanel layout manager default : FlowLayout. FlowLayout layout manager: Place any number of components. They appear in the order added, taking as many rows as necessary.
public class BoxDemo2 extends JFrame { /** Constructor: frame with title t and 3 columns with n, n+1, and n+2 buttons. */ public BoxDemo2(String t, int n) { super (t); // Create Box b1 with n buttons. Box b1= new Box(BoxLayout.Y_AXIS); for ( int i= 0; i != n; i= i+1) b1.add( new JButton(”1 " + i)); // Create Box b2 with n+1 buttons. Box b2= … // Create Box b3 with n+2 buttons. Box b3= … // Create horizontal box b containing b1, b2, b Box b= new Box(BoxLayout.X_AXIS); b.add(b1); b.add(b2); b.add(b3); Container cp= getContentPane(); cp.add(b, BorderLayout.CENTER); pack(); show(); }
3 vertical boxes, each a column of buttons, are placed in a horizontal box BoxLayout layout manager: Place any number of components. They appear in the order added, taking only one row.
To simulate using a BoxLayout manager for a JFrame, create a Box and place it as the sole component of the JFrame: JFrame jf= new JFrame(“title”); Box b= new Box(BoxLayout.X_AXIS); Add components to b; jf.add(b,BorderLayout.CENTER);
1. Start developing a GUI by changing an already existing one. A lot of details. Hard to get all details right when one starts from scratch and has little idea about the Java GUI package.