0% found this document useful (0 votes)
9 views25 pages

Curs 1OOP2

The document provides an overview of building graphical user interfaces (GUIs) using Swing in Java, covering key concepts such as component classes, containers, layouts, and various UI components like buttons, text fields, and lists. It details methods for manipulating component properties, layout management, and specific classes like JPanel, JFrame, and JScrollBar. Additionally, it explains different layout types including FlowLayout, GridLayout, and BoxLayout, along with their respective constraints and functionalities.

Uploaded by

crengutabogdan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views25 pages

Curs 1OOP2

The document provides an overview of building graphical user interfaces (GUIs) using Swing in Java, covering key concepts such as component classes, containers, layouts, and various UI components like buttons, text fields, and lists. It details methods for manipulating component properties, layout management, and specific classes like JPanel, JFrame, and JScrollBar. Additionally, it explains different layout types including FlowLayout, GridLayout, and BoxLayout, along with their respective constraints and functionalities.

Uploaded by

crengutabogdan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture 1 OOP2

 Graphical interfaces in Swing

 Types of containers.

 Layouts

 Types of components

2025
Building GUIs with Swing
Component class
- dimension
void setSize(int lat, int inalt)
void setSize(Dimension d)
Dimension getSize()
int getWidth()
int getHeight()

- location
void setLocation(int x, int y) – (x,y) are the coordinates of the upper left
corner of the current component
void setLocation(Point p)
void setBounds(int x,int y, int width, int height) iff layout=null
Point getLocation()
Point getLocationOnScreen()

- A container: applet, panel, scroll panel, box, or frame that contains


component.
Component class (cont.)

- font:
void setFont(Font f)
Font getFont() .deriveFont(Font.BOLD, “24”)
- color:
void setBackground(Color c) , Color.pink, Color.RED
Color getBackground()
void setForeground(Color c)
Color getForeground()
Color c=new Color(int r, int g, int b), r,g,b is in [0,255]
new Color(int r, int g, int b, int alpha), alpha is the degree of transparency of
the color
new Color(double r, double g, double b), [0,1]
- visibility
void setVisible(boolean b)
boolean isVisible()
boolean isShowing()
- activation
void setEnabled(boolean b)
boolean isEnabled()
Container class
Method prototype Semantic - effect
Component add(Component c) Adds the c component to the row in the
current container according to the
container's layout
void remove(Component c) Removes the c component c in the current
container.
void removeAll() Removes all components contained by the
container.
void validate() The container validates all contained
components.
Component getComponent (int p) When added to the container, each
component receives a sequence number
used by this method to provide the
component with the sequence number equal
to the value of p.
void remove(int p) Removes the component at position p from
the current container.
void setLayout(LayoutManager lm) Changes the current arrangement of the
components in the container to the one
passed as a parameter
Types of layouts

 FlowLayout arranges container components in a row, from left to right, in


the order they are added to the container.
 BorderLayout :

 GridLayout places the components in an invisible matrix grid whose


number of rows and columns is specified by the constructor :
GridLayout(int numberOfLines, int numberOfColumns)
GridLayout(int numberOfLines, int numberOfColumns, int spaceOX, int
spaceOY)
Each component is added to a cell of the array in the order in which the
add() method is called starting from left to right and top to bottom.
Types of layouts (cont.) GridBagConstraints class
Element Semantic
Instance variables
int gridwidth Specifies the number of cells on a line. By default, gridwidth has
the value 1.
int gridheight Specifies the number of cells on a column. By default, gridheight
has the value 1.
int gridx Specifies the column of the cell in which the current component is
placed, that is, the number of the column to which the respective
cell belongs. The first component is placed on the first column, ie
gridx=0.
int gridy Specifies the line of the cell in which the current component is
placed, that is, the number of the column to which the respective
cell belongs. The first component is placed on the first line, ie
gridy=0.
int fill Specifies how much space the component occupies in a larger
space than it needs to display. Value: NONE, HORIZONTAL,
VERTICAL, BOTH.
int anchor Specifies where the component will be anchored in its space (if
larger than necessary). Value: CENTER, NORTH, NORTHEAST,
EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST,
NORTHWEST. Implicit, componenta este centrată.
GridBagConstraints class (cont)

Element Semantic
Instance variable
double weightx Specifies how the excess horizontal space on each column is
distributed. By default, weightx=0.
double weighty Specifies how the excess vertical space on each line is
distributed. By default, weighty=0.
Insets insets Specifies the space between the component and the edges of
the cell in which it is placed.
int ipadx Specifies the space to be added to the width of the component
in the cell in which it is placed. The space is equal with 2*ipadx
pixels.
int ipady Specifies the space to be added to the height of the
component in the cell in which it is placed. The space is equal
with 2*ipady pixels.
Constants
int RELATIVE The component is placed after the previous component in its
column or row, or after the previously added component.
GridBagConstraints class (cont)

public void addConstraints(Component c, int line, int column, int w,


int h){

gbc.gridx= column;

gbc.gridy=line;

gbc.gridwidth=w;

gbc.gridheight=h;

gb.setConstraints(c,gbc);

p.add(c);//p refers an object JPanel

}
GridBagConstraints class(cont)
private void addConstraints(JComponent c, int line, int col, int w, int h, int anchor, int fill, int spaceOX, int spaceOY)
{
gbc.gridx=col;
gbc.gridy=line;
gbc.gridwidth=w;
gbc.gridheight=h;

gbc.anchor=anchor;
double weightx = 0.0;
double weighty = 0.0;
if(w > 1) weightx = 1.0;
if(h > 1) weighty = 1.0;
switch(fill){
case GridBagConstraints.HORIZONTAL -> {
gbc.weightx = weightx;
gbc.weighty = 0.0;
}
case GridBagConstraints.VERTICAL -> {
gbc.weighty = weighty;
gbc.weightx = 0.0;
}
case GridBagConstraints.BOTH -> {
gbc.weightx = weightx;
gbc.weighty = weighty;
}
case GridBagConstraints.NONE -> {
gbc.weightx = 0.0;
gbc.weighty = 0.0;
}

}
gbc.fill = fill;
gbc.insets=new Insets(0, 2*spaceOX, 0, 2*spaceOY);
p.add(c, gbc);
}
Examples. Classes: GridBagLayout and
GridBagConstraints
Example. Classes: GridBagLayout and
GridBagConstraints

0 1 2 3

1
2
3
Types of layouts (cont.) BoxLayout and Box classes
Constructor prototype Semantic
BoxLayout(Container target, Takes a reference to the Container component
int axis) it will manage and a direction
(BoxLayout.X_AXIS or BoxLayout.Y_AXIS).
This layout arranges components either on top
of each other or in a row. Components are laid
out according to their preferred sizes and they
are not wrapped, even if the container does
not provide enough space.
Box(int axis) Creates an object of the class Box that
displays its components along the specified
axis.
Methods of the class Box Semantic
static Component createGlue() Creates an invisible component that expands
as much as necessary to fill the space
between its neighboring components.
static Component Creates an invisible component that's always
createRigidArea(Dimension d) the specified size.
static Component createHorizo Creates an invisible, fixed-width component.
ntalStrut(int width)
static Component createVertica Creates an invisible, fixed-height component.
Example. Classes: BoxLayout and Box
Class JPanel

 A panel is a container of graphic components that must sit inside an


applet, window, or other panel. If we are in the last case, the same rule
applies to the second panel, and so on, but finally, the last panel must be
contained in an applet or a window.

 Graphically, a panel is a rectangular surface where the components it


contains are added.

Method prototype Semantic


Constructors
JPanel() Creează un panou vid cu layout-ul FlowLayout
JPanel(LayoutManager Creates an empty panel with the layout
lm) specified by the lm parameter
Class JWindow

 Objects of the JWindow class represent high-level windows without a


border and without a menu bar. In general, a JWindow has another Frame
or Window as its owner, specified in the constructor.
Method prototype Semantic
Constructors
JWindow(Frame f) Creates a window above frame f whose reference is
passed as a parameter.
JWindow(Window w) Creates a window above window w whose reference is
passed as a parameter.
Methods
pack() Calculates the width and height of the window based on
the dimensions of the contained components.
void setIconImage Sets the image used as the window icon.
(Image i)
Class JFrame. Constructors and properties

- Constructors:
JFrame()
JFrame(String s) // creates a JFrame object with the title s
- title:
void setTitle(String t)
String getTitle()
- state:
void setExtendedState(int s)
int getExtendedState()
- ability to be resized by the user:
void setResizable(boolean b)
boolean isResizable()
- menu bar:
void setJMenuBar(JMenuBar mb)
JMenuBar getJMenuBar()
Class JLabel
Method prototype Semantic
Constructors
JLabel() Creates a JLabel object that represents an empty label.

JLabel(String t) Creates a JLabel object that represents a label


containing the left-aligned text t.
JLabel(String t, int Creates a JLabel object that represents a label
alignment) containing the text t aligned left (Label.LEFT), centered
(Label.CENTER) or right (Label.RIGHT).
JLabel(String t, Icon Creates a JLabel object that represents a label
image, int alignment) containing the text t and an image placed according to
the alignment passed as a parameter.
Instance methods
void setText(String t) Modifies the text of the current label with the one
passed as a parameter to the method.
String getText() Returns the text of the current label.
void setIcon(Icon i) Set the label image
Icon getIcon() Returns the label icon
Class JButton
Method prototypes Semantic
Constructors
JButton() Creates a JButton object with an empty label.
JButton(String t) Creates a JButton object with the label t.
JButton(Icon i) Creates a JButton object with the image passed in
the parameter.
JButton(String t, Icon i) Creates a JButton object with the label and image
passed as parameters.
Instance methods
void setLabel(String t) Modifies the button text with the string passed as
parameter.
String getText() Returns the label text of the current button.
void setIcon(Icon i) Set the button image
Icon getIcon() Returns the image of the button
Classes JTextField and JPasswordField

Method prototype Semantic


Constructors
JTextField(int l) Creates a JTextField object of width l
JTextField(String t) Creates a JTextField object that displays the text t.
JTextField(String t, int l) Creates a JTextField object that displays the text t
of width l.
JPasswordField(int l) Creates a JPasswordField object of width l
Instance methods
void setText(String t) Modifies the text displayed by the text field with
the string passed as a parameter.

String getText() Returns the text displayed by the current text


field.
void setToolTipText(String t) Set the text used as a tooltip.
Class JTextArea

Method prototype Semantic


Constructors
JTextArea(int nl, int nc) Creates a JTextArea object of height nl and width
nc.
JTextArea(String t) Creates a JTextArea object that displays the text t.
Instance methods
void append(String t) Add the string t to the end of the area text.
void setText(String t) Modifies the text displayed by the text field with the
string passed as a parameter.

String getText() Returns the text displayed by the current text area.
Classes JCheckBox and JRadioButton
A checkbox is a square or circle graphic component that the user can select
with a mouse click. It can be de-selected by clicking on a selected box. A
checkbox can also contain text displayed next to its graphical
representation.

Method prototype Semantic


Some constructors
JCheckbox(String t) It creates a check box with the text t and by
JRadioButton(String t) default it is unselected.
JCheckbox(String t, Icon imag) It creates a check box with the text t, the
JRadioButton(String t, Icon imag) imag image and default is unchecked.
JCheckbox(String t, boolean s) Create a check box with text t with state s,
JRadioButton(String t, boolean s) which is true or false
Method
boolean isSelected() Returns the status of the current checkbox
Lists

Lists are sequences of character strings or other elements that can be


selected by the user or programmer through programs.

There are three types of lists in Swing:


 lists that display a number of elements and possibly have a scroll bar.
They are represented as JList objects;

 hidden lists that display a single item. They are represented as


JComboBox objects;

 scroll bars that take values from a closed range that are not displayed to
the user. They are represented as JScrollBar objects.
Classes JList and JComboBox
Method prototype Semantic
Some constructors
JList(E[] elem ) Creates a JList type list with the elements
JList(Vector<E> elem) transmitted through the array or the elem vector.
JComboBox(E[] elem ) Creates a JComboBox type list with the elements
JComboBox(Vector<E> elem) transmitted through the elem array or the vector.
Methods of class JList
String getSelectedItem() Returns the list element that was selected by the
user.
int getSelectedIndex() Returns the position of the list element that was
selected by the user
Methods of class JComboBox
Object getSelectedItem() Returns the list element that was selected by the
user
int getSelectedIndex() Returns the position of the list element that was
selected by the user
void addItem(String s) Add element s to the current list
Class JScrollBar

Method prototype Semantic


Some constructors
JScrollBar(int o, int v, Creates a JScrollBar type list placed
int inc, int min, int max) horizontally or vertically, with the initial value v,
the increment step inc and the iteration interval
[min,max]
JScrollBar() Creates a vertically placed JScrollBar type list
with initial elements: v=0, inc=10, min=0,
max=100.
JScrollBar(int o) Creates a JScrollBar type list placed according
to parameter o with the initial elements: v=0,
inc=10, min=0, max=100.
Instance methods
void setValue(int v) Sets the new current value of the scroll bar

int getValue() Returns the current value of the scroll bar

You might also like