Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

Course 11 OOP

Graphical user interfaces in Swing

Types of containers

Types of components

2021
Graphical user interface in Swing
The properties of the Component class
- location
void setLocation(int x, int y)
void setLocation(Point p)
void setBounds(int x, int y, int latime, int inaltime)
Point getLocation()
Point getLocationOnScreen()
setLayout(null)=> setBounds()
- size
void setSize(int lat, int inalt) – important pt container-er JPanel, JWindow
void setSize(Dimension d)
Dimension getSize()

- Parent container: applet, panel, scrolling panel or a frame that contains


the component.

 
The properties of the Component class (cont.)

- font:
void setFont(Font f) , Font.BOLD, Font.ITALIC
Font getFont() , new Font(“Arial”, Font.BOLD, 12);
 
- color:
void setBackground(Color c) , Color.pink, Color.red, Color.RED
Color getBackground()
void setForeground(Color c) , new Color(int rosu, int verde, int
albastru, int alfa) [0,255]
Color getForeground()

- visibility:
void setVisible(boolean b) – important: JWindow, JFrame, etc
boolean isVisible()
boolean isShowing()

-activation:
boolean isEnabled()
Container class
Method prototype Effect – returned value
Component add(Component Adds component c in a row to the current
c) container.
Component add(Component Adds component c to position p in the current
c, int p) container.
void remove(Component c) Removes component c from the current
container.
void remove(int p) Removes the component from position p in the
current container.
void removeAll() Removes all components contained in the
container.
void validate() The container validates all contained
components.
Component getComponent When added to the container, each component
(int p) receives an order number used by this method
to provide the component with the order number
equal to its value p.
void It changes the current way of arranging the
setLayout(LayoutManager lm) components in the container with the one
Types of layouts
 FlowLayout puts the 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 nl, int nc)
GridLayout(int nl, int nc, int sc, int sl)

Each component is added to an array cell in the order in which the add()
method is called from left to right and top to bottom.
Types of layouts (cont.) GridBagLayout class
Element Semantic
Variabilă instanţă
int Specifies the number of cells on a column. By default, gridwidth is
gridwidth set to 1.
int Specifies the number of cells on a line. By default, gridheight is set
gridheight to 1.
int gridx Specifies the column of the cell in which the current component is
located, that is, the number of the column to which the cell
belongs. The first component is placed on the first column, i.e.
gridx = 0.
int gridy Specifies the line of the cell in which the current component is
located, that is, the number of the line on which the cell is located.
The first component is placed on the first line, i.e. gridy = 0.
int fill Specifies how much space the component occupies in a larger
space than is required for 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. By default, the component is centered.
GridBagLayout class (cont)

Element Semantic
Instance variable
double Specifies how the horizontal surplus space on each column is
weightx distributed. By default, weightx = 0.
double Specifies how the surplus vertical space on each line is
weighty distributed. By default, weighty = 0.
Insets insets Specifies the space between the component and the edges of the
cell in which it is located.
int ipadx Specifies the space to be added to the width of the component in
the cell in which it is located. Space is 2 * ipadx pixels.
int ipady Specifies the space to be added to the height of the component in
the cell in which it is located. Space is 2 * ipady pixels.
Constant
int RELATIVE The component is placed after the previous component in its
column or line, or after the previously added component.

int REMAINDER The component is the last one in its column or line.
GridBagLayout class (cont)

public void adaugaConstrangeri(Component c, int linie, int col, int


latime, int inalt){

gbc.gridx=col;

gbc.gridy=linie;

gbc.gridwidth=latime;

gbc.gridheight=inalt;

gb.setConstraints(c,gbc);//gbc-obiect GridBagConstraints

p.add(c);//p – obiect JPanel

}
JPanel class
 A panel is a container of graphical components that must sit in an applet,
window, or other panel. If we are in the latter case, the same rule applies
to the second panel, and so on, but in the end, the last panel must be
contained in an applet or window.

 Graphically, a panel is a rectangular surface in which the components it


contains are added.

Method prototype Effect– returned value


Constructors
JPanel() Create an empty panel, FlowLayout
JPanel(LayoutManager Creates an empty panel with a specific layout,
lm) specified by the lm parameter.
JWindow class
 JWindow class objects are high-level windows with no border and no
menu bar. Generally, one JWindow window has another JFrame or
JWindow window as owner, specified in the constructor.
Method prototype Effect – returned value
Constructors
JWindow(JFrame f) Creates a window above the window f whose reference
is passed as a parameter.
JWindow(JWindow Creates a window above the window w whose reference
w) is passed as a parameter.
Methods
pack() Calculates the width and the height of the window
according to the dimensions of the contained
components.
void setIconImage ( Sets the image used as a window icon.
Image i)
Toolkit getToolkit() Returns a Toolkit object of the current window. The
object can be used to position the window on the
monitor screen.
JFrame class

- Constructors:
Frame()
Frame(String s)  
- title:
void setTitle(String t)
String getTitle()
- state:
void setExtendedState(int stare)
int getExtendedState()
- the ability for the user to resize the window:
void setResizable(boolean b)
boolean isResizable()
- menu bar:
void setMenuBar(JMenuBar mb)
JMenuBar getMenuBar()
JLabel class

Method prototype Effect– returned value


Constructors
JLabel() Create a JLabel object that represents an empty
label.
JLabel(String t) Creates a JLabel object that represents a label that
contains the left-aligned t-text.
JLabel(String t, int Creates a JLabel object that represents a label that
aliniere) contains the text t aligned left (JLabel.LEFT),
centered (JLabel.CENTER) or right (JLabel.RIGHT).
Instance methods
void setText(String t) Modifies the text of the current label with the
transmitted text as a parameter of the method.
String getText() Returns the text of the current label.
JButton class

Method prototype Effect – returned value


Constructors
JButton() Creates a JButton object with an empty label.

JButton(String t) Create a JButton object with the label t.

Instance methods
void setLabel(String t) Changes the text of the button with the
transmitted string as a parameter.
String getText() Returns the text of the current button’s label.
JTextField and JPasswordField classes
Method Effect
Constructors
JTextField(int l) Creates an object JTextField by width l
JTextField(String t) Creates an object JTextField which displays
the text t
JTextField(String t, int Creates an object JTextField which displays
l) the text t by width l.
JPasswordField(int l) Creates an object JPasswordField by width l

Instance methods inherited from the TextComponent class


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
Clasa JTextArea

Prototip metodă Efect – valoarea returnată


Constructori
JTextArea(int nl, int Creează un obiect JTextArea de inlatime nl si latime
nc) nc
JTextArea(String t) Creează un obiect JTextArea care afiseaza textul t.
Metode instanţă
void append(String t) Adauga sirul t la sfarsitul textului ariei.
void setText(String t) Modifică textul afisat de campul de text cu sirul
transmis ca parametru.

String getText() Returnează textul afisat de aria de text curenta.


JCheckBox and JRadioButton classes

A checkbox is a component with a graphical representation of a square or


circle that the user can select with a mouse click. It is also deselected
by clicking on a selected box. A check box may also contain text displayed
next to its graphic representation.
Method Effect
Constructors
JCheckbox(String t) Creates a checkbox with the label t
JRadioButton(String t)
JCheckbox(String t, Icon imag) Creates a checkbox with the label t
JRadioButton(String t, Icon and the image imag
imag)
JCheckbox(String t, boolean s) Creates a checkbox with the label t
JRadioButton(String t, boolean and the state s
s)
Lists

Lists are sequences of strings or other elements that can be selected by the
user or programmer through the programs.

There are three types of lists in Swing:


 lists that display a number of items 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 have values that are taken from a closed range that are not
displayed to the user. They are represented as JScrollbar objects.
Clasele JList si JComboBox
Prototip metodă Semnificaţie – valoarea returnată
Unii constructori
JList(E[] elem ) Creeaza o lista de tip JList cu elementele
JList(Vector<E> elem) transmise prin tabloul sau vectorul elem
JComboBox(E[] elem ) Creeaza o lista de tip JComboBox cu elementele
transmise prin tabloul sau vectorul elem
JComboBox(Vector<E> elem) Creeaza o lista de tip JComboBox cu elementele
transmise prin tabloul sau vectorul elem
Metode ale clasei JList
String getSelectedItem() Returneaza elementul listei ce a fost selectat de
catre utilizator
int getSelectedIndex() Returneaza pozitia elementului listei ce a fost
selectat de catre utilizator
Metode ale clasei JComboBox
Object getSelectedItem() Returneaza elementul listei ce a fost selectat de
catre utilizator
int getSelectedIndex() Returneaza pozitia elementului listei ce a fost
selectat de catre utilizator
void addItem(String s) Adauga elementul s la lista curenta
Clasa JScrollBar

Prototip metodă Semnificaţie – valoarea returnată


Unii constructori
JScrollBar(int o, int v, Creeaza o lista de tip JScrollBar plasata
int inc, int min, int max) orizontal sau vertical, cu valoarea initiala v,
pasul de incrementare inc si intervalul de
iteratie [min,max]
JScrollBar() Creeaza o lista de tip JScrollBar plasata
vertical cu elementele initiale: v=0, inc=10,
min=0, max=100.
JScrollBar(int o) Creeaza o lista de tip JScrollBar plasata
conform parametrului o cu elementele initiale:
v=0, inc=10, min=0, max=100.
Metode instanta
void setValue(int v) Seteaza noua valoare curenta a barei de
defilare
int getValue() Returneaza valoarea curenta a barei de
defilare

You might also like