Curs 1OOP2
Curs 1OOP2
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()
- 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
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)
gbc.gridx= column;
gbc.gridy=line;
gbc.gridwidth=w;
gbc.gridheight=h;
gb.setConstraints(c,gbc);
}
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
- 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.
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.
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