Sunteți pe pagina 1din 66

Proiectarea aplicațiilor orientate

obiect
Curs 12 – awt

J
J Acest curs
› Project start
› Awt
J Call stack in multithreding program
› Fie urmatorul apel de functii
– Main() → function1() → function2()

main() {
...
function1();
...
}
function1() {
...
function2();
...
}
function2() {
...
}
J Call stack in multithreding program
› function2() va fi executata intr-un thread separat
J
J Graphic user interface
› Modelul AWT
› Componentele AWT
J Introducere AWT (Abstract Windowing Toolkit)
› A fost primul framework GUI introdus începând cu versiunea 1.0
› Framework-urile mai avansate precum Swing și JavaFX sunt construite
deasupra AWT
› Conține clase și interfețe pentru a crea GUI
› Pachetul AWT este java.awt.
J
J Clasele AWT
› Component
– Este clasa abstracta rădăcina pentru toate clasele GUI
– Incapsuleaza toate proprietățile unei componente vizuale.
› Container
– Subclasa a clasei Component
– poate contine alte componente
– Responsabil pentru poziționarea componentelor
› Panel
– Clasa concreta a clasei Container
– Este o fereastra fără bara de titlu, fără bara de meniu, fără margini (border)
– Capabilă să dețină alte componente
› Window
– Subclasa a clasei container
– Creează un top-level container care poate deține toate celelalte componente
› Frame
– Clasa concreta a clasei Window (incapsuleaza notiunea de fereastra)
– Contine bara de titlu, bara de meniu, margini si colturi redimensionabile
J Clasa Frame
› Folosită la crearea ferestrelor standard
– Frame()
– Frame(String title)
› Ex. metode
– void setSize(int width, int height)
– void setSize(Dimension reference)
– Dimension getSize()
– void setVisible(boolean visibleFlag
– void setTitle(String title)
import java.awt.*;
import java.awt.event.WindowAdapter;

J import java.awt.event.WindowEvent;
public class Main extends Frame {
Main()
{
setSize(600, 300);
setTitle("My first app");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g){
g.drawString("Prima fereastra", 40, 80);
super.paint(g);
}
public static void main(String[] args) {
Main m = new Main();
}
}
J
J Frame
J
J Clasa Graphics
void drawLine(int startX, startY, endX, endY)

void drawRect(int startX, int startY, int width, int height)


void fillRect(int startX, int startY, int width, int height)

void drawRoundRect(int startX, int startY, int width, int height, int xDiam, int yDiam)
void fillRoundRect(int startX, int startY, int width, int height, int xDiam, int yDiam)

void drawOval(int startX, int startY, int width, int height)


void fillOval(int startX, int startY, int width, int height)

void drawArc(int startX, int startY, int width, int height, int startAngle, int sweepAngle)
void fillArc(int startX, int startY, int width, int height, int startAngle, int sweepAngle)

void drawPolygon(int x[], int y[], int numPoints)


void fillPolygon(int x[], int y[], int numPoints)
package paoo;
import java.awt.*;
import java.awt.event.WindowAdapter;
J import java.awt.event.WindowEvent;
public class Main extends Frame {
Main(){
setSize(600, 300);
setTitle("My first app");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g){
g.drawLine(0, 31, 600, 31);
g.drawLine(0, 100, 600, 100);
g.drawLine(0, 200, 600, 200);
g.drawLine(100, 0, 100, 300);
g.drawLine(200, 0, 200, 300);
g.drawLine(300, 0, 300, 300);
g.drawLine(400, 0, 400, 300);
g.drawLine(500, 0, 500, 300);
g.fillRect(30, 40, 60, 50);
g.drawRect(130, 40, 60, 40);

J g.drawRoundRect(320, 40, 70, 40, 20, 20);


g.fillRoundRect(530, 60, 50, 30, 20, 20);

g.drawOval(520, 120, 65, 40);


g.fillOval(120, 120, 55, 30);

g.drawArc(20, 120, 70, 40, 90, -90);


g.fillArc(410, 120, 70, 40, 90, -90);

int[] x = {20, 20, 80, 95};


int[] y = {220, 290, 270, 210};
g.drawPolygon(x, y, 4);

int[] fillx = {320, 310, 395, 380};


int[] filly = {215, 280, 287, 220};
g.fillPolygon(fillx, filly, 4);
}
public static void main(String[] args) {
Main m = new Main();
}
}
J
J Clasa Color
› Constructori
Color(int red, int green, int blue)
Color(int rgbValue)
Color(float red, float green, float blue)

› Se pot obține valorile componentelor de roșu, verde și albastru


int getRed()
int getGreen()
int getBlue()

› Se poate seta proprietatea de culoare obiectelor grafice


void setColor(Color newColor)
Color getColor()
g.setColor(Color.BLUE);
g.fillRect(30, 40, 60, 50);
g.drawRect(130, 40, 60, 40);

J g.setColor(Color.RED);
g.drawRoundRect(320, 40, 70, 40, 20, 20);
g.fillRoundRect(530, 60, 50, 30, 20, 20);

g.setColor(Color.ORANGE);
g.drawOval(520, 120, 65, 40);
g.fillOval(120, 120, 55, 30);

g.setColor(Color.MAGENTA);
g.drawArc(20, 120, 70, 40, 90, -90);
g.fillArc(410, 120, 70, 40, 90, -90);

g.setColor(Color.GREEN);
int[] x = {20, 20, 80, 95};
int[] y = {220, 290, 270, 210};
g.drawPolygon(x, y, 4);

g.setColor(Color.PINK);
int[] fillx = {320, 310, 395, 380};
int[] filly = {215, 280, 287, 220};
g.fillPolygon(fillx, filly, 4);
J
J Label
› Constructori
Label()
Label(String str)
Label(String str, int how)

› Metode
void setText(String str)
String getText()
void setAlignment(int how)
int getAlignment()
import java.awt.*;
import java.awt.event.WindowAdapter;

J import java.awt.event.WindowEvent;

public class Main extends Frame {


Label txt;
Main(){
setSize(600, 300);
setTitle("My first app");
setLayout(new FlowLayout());
txt = new Label("PAOO");
add(txt);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
}
J
public class Main extends Frame {
Button b1, b2, b3;

J Clasa Button
Main(){
setSize(600, 300);
setTitle("My first app");
setLayout(new FlowLayout());
› Constructori b1 = new Button("Colocviu");
Button() b2 = new Button("Proiect");
Button(String str) b3 = new Button("Nimic");
add(b1);
add(b2);
add(b3);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
}
J
J Checkbox
› Constructori
Checkbox()
Checkbox(String str)
Checkbox(String str, boolean on)
Checkbox(String str, boolean on, CheckboxGroup cbGroup)
Checkbox(String str, CheckboxGroup cbGroup, boolean on)
› Metode
boolean getState()
void setState(boolean on)
String getLabel()
void setLabel(String str)
public class Main extends Frame {
Checkbox c1, c2;
J Main(){
setSize(600, 300);
setTitle("My first app");
setLayout(new FlowLayout());
c1 = new Checkbox("Test");
c2 = new Checkbox("Colocviu");
add(c1);
add(c2);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
}
J
public class Main extends Frame {
Checkbox c1, c2;
CheckboxGroup cbg;
J Main() {
setSize(600, 300);
setTitle("My first app");
setLayout(new FlowLayout());
cbg = new CheckboxGroup();
c1 = new Checkbox("Test", cbg, false);
c2 = new Checkbox("Colocviu", cbg, false);
add(c1);
add(c2);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
}
J
public class Main extends Frame {
Choice myList;

J Choise Main() {
setSize(600, 300);
setTitle("My first app");
› Metode setLayout(new FlowLayout());
setVisible(true);
void add(String name) myList = new Choice();
String getSelectedItem() myList.add("Abstractizare");
int getSelectedIndex() myList.add("Mostenire");
int getItemCount() myList.add("Polimorfism");
void select(int index) add(myList);
void select(String name) addWindowListener(new WindowAdapter() {
void getItem(int index) public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public static void main(String[] args) {
Main m = new Main();
}
}
J
J List
› Constructori
List()
List(int numRows)
List(int numRows, boolean multipleSelect)

› Metode
void add(String name)
void add(String name, int index)
String getSelectedItem()
int getSelectedIndex()
String[] getSelectedItems()
int[] getSelectedIndexes()
int getItemCount()
void select(int index)
String getItem(int index)
public class Main extends Frame {
List myList;

J Main() {
setSize(600, 300);
setTitle("My first app");
setLayout(new FlowLayout());
setVisible(true);
myList = new List();
myList.add("Abstractizare");
myList.add("Mostenire");
myList.add("Polimorfism");
add(myList);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public static void main(String[] args) {
Main m = new Main();
}
}
J
J TextField
› Constructori
TextField()
TextField(int numChars)
TextField(String str)
TextField(String str, int numChars)
› Metode
String getText()
void setText(String str)
String getSelectedText()
void select(int startindex, int endindex)
boolean isEditable()
void setEditable(boolean canEdit)
void setEchoChar(char ch)
boolean echoCharIsSet()
char getEchoChar()
public class Main extends Frame {
Label myLabel;
J TextField tf;
Main() {
setSize(600, 300);
setTitle("My first app");
setLayout(new FlowLayout());
setVisible(true);
myLabel = new Label("Enter name: ");
tf = new TextField(20);
add(myLabel);
add(tf);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public static void main(String[] args) {
Main m = new Main();
}
}
J
J TextArea
› Constructori
TextArea()
TextArea(int numLines, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int sBars)
SCROLLBARS_BOTH
› Metode SCROLLBARS_NONE
String getText() SCROLLBARS_HORIZONTAL_ONLY
void setText(String str) SCROLLBARS_VERTICAL_ONLY
String getSelectedText()
void select(int startindex, int endindex)
boolean isEditable()
void setEditable(boolean canEdit)
void append(String str)
void insert(String str, int index)
void replaceRange(String str, int startIndex, int endIndex)
J
J Layout Managers
› Instanța a unei clase ce implementează interfața LayoutManager
› Gestionează automat aranjamentul diferitelor componente dintr-un
container
› Fiecare container va avea un layout manager implicit
– Panel - FlowLayout
– Jpanel – FlowLayout
– Applet – FlowLayout
– JApplet – BorderLayout
– Frame – BorderLayout
– JFrame – BorderLayout
› Este setat prin apelul funcției
void setLayout(LayoutManager layoutObj)
J Layout Managers
› Poziția fiecărei componente poate fi stabilita manual (nerecomandat)
– setLayout(null);
– setBounds(…); pentru fiecare componenta
› LayoutManagers disponibili în AWT
– FlowLayout
– BorderLayout
– GridLayout
– CardLayout
– GridBagLayout
J FlowLayout
› Aranjează componentele una după alta, de la stânga la dreapta, de sus
în jos
› Costructori
FlowLayout()
FlowLayout(int how)
FlowLayout(int how, int hspace, int vspace)

FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
FlowLayout.LEADING
FlowLayout.TRAILING
public class Main extends Frame {
Panel p1, p2, p3;

J Label l1, l2;


TextField t1, t2;
Button b;
Main() {
setSize(400, 200);
setTitle("My first app");
setLayout(new FlowLayout());
p1 = new Panel();
l1 = new Label("Enter Username: ");
t1 = new TextField(20);
p1.add(l1);
p1.add(t1);
p1.setPreferredSize(new Dimension(400, 30));
p2 = new Panel();
l2 = new Label("Enter Password: ");
t2 = new TextField(20);
t2.setEchoChar('*');
p2.add(l2);
p2.add(t2);
p2.setPreferredSize(new Dimension(400, 30));
J p3 = new Panel();
b = new Button("Login");
p3.add(b);
add(p1);
add(p2);
add(p3);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
}
J BorderLayout
› Divide suprafața în cinci regiuni numite: nord, sud, est, vest și centru
› Regiunea implicită este centru
› Atenție deoarece controalele se pot suprapune
› Constructori
BorderLayout()
BorderLayout(int hspace, int vspace)
public class Main extends Frame {
Button bnorth, bsouth, beast, bwest, bcenter;
Main() {
J setSize(400, 200); setTitle("My Application");
bnorth = new Button("North");
bsouth = new Button("South");
beast = new Button("East");
bwest = new Button("West");
bcenter = new Button("Center");
add(bnorth, BorderLayout.NORTH);
add(bsouth, BorderLayout.SOUTH);
add(beast, BorderLayout.EAST);
add(bwest, BorderLayout.WEST);
add(bcenter, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
J GridLayout
› Aranjează componentele într-un grid bidimensional
› Se poate specifica numărul de rânduri și coloane
› Atenție: numărul de celule din grid nu trebuie depășit de numărul
componentelor
› Constructori:
GridLayout()
GridLayout(int numRows, int numCols)
GridLayout(int numRows, int numCols, int hspace, int vspace)
public class Main extends Frame {
Button b1, b2, b3, b4;
Main() {
J setSize(300, 300);
setTitle("My Application");
setLayout(new GridLayout(2, 2));
b1 = new Button("Unu");
b2 = new Button("Doi");
b3 = new Button("Trei");
b4 = new Button("Patru");
add(b1);
add(b2);
add(b3);
add(b4);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
J CardLayout
› Permite crearea unui așa zis pachet de cărți (carduri)
› Fiecare card poate conține diferite componente
› Doar un singur card poate fi afișat
› Implementarea presupune utilizarea unui panel ce actioneaza ca un
container pentru celelalte carduri
› Fiecare card poate fi un panel care să conțină mai multe componente
› Constructori
CardLayout()
CardLayout(int hspace, int vspace)
› Metode
void add(Component panelRef, Object name)
//Object name este un string ce reprezinta numele cardului
J
› Metode
void first(Container deck)
void last(Container deck)
void next(Container deck)
void previous(Container deck)
void show(Container deck, String cardName)
import java.awt.*;
import java.awt.event.*;

J public class Main extends Frame implements ActionListener


{
Button first, last, next, prev;
Panel bpanel, deck;
Label l1, l2, l3;
Panel card1, card2, card3;
CardLayout cl;
Main()
{
setSize(300, 300);
setTitle("My Application");
first = new Button("First");
last = new Button("Last");
next = new Button("Next");
prev = new Button("Previous");
first.addActionListener(this);
last.addActionListener(this);
next.addActionListener(this);
prev.addActionListener(this);
bpanel = new Panel();
bpanel.add(first);

J bpanel.add(last);
bpanel.add(next);
bpanel.add(prev);
add(bpanel, BorderLayout.NORTH);
cl = new CardLayout();
l1 = new Label("This is card 1");
l2 = new Label("This is card 2");
l3 = new Label("This is card 3");
card1 = new Panel();
card2 = new Panel();
card3 = new Panel();
card1.add(l1);
card2.add(l2);
card3.add(l3);
deck = new Panel();
deck.setLayout(cl);
deck.add(card1, "card1");
deck.add(card2, "card2");
deck.add(card3, "card3");
add(deck, BorderLayout.CENTER);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){

J });
}
System.exit(0);

setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("First"))
cl.first(deck);
else if(ae.getActionCommand().equals("Last"))
cl.last(deck);
else if(ae.getActionCommand().equals("Next"))
cl.next(deck);
else
cl.previous(deck);
}
public static void main(String[] args)
{
Main mf = new Main();
}
}
J GridBagLayout
› Grid cu un număr diferit de coloane de pe fiecare rand
› Locația și dimensiunea fiecărei componente sunt specificate printr-un set
de constrangeri conținute într-un obiect de tip GridBagConstraints
› Aceste constrangeri includ înălțimea, lățimea și plasarea componentei
J
import java.awt.*;
import java.awt.event.*;
public class Main extends Frame
J {
Button b1, b2, b3, b4;
Main() {
setSize(420, 300);
setTitle("My Application");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
b1 = new Button("Button 1");
b2 = new Button("Button 2");
b3 = new Button("Button 3");
b4 = new Button("Button 4");
gbc.weightx = 1.0;
gbc.ipadx = 100;
gbc.insets = new Insets(4, 4, 0, 0);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbl.setConstraints(b1, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbl.setConstraints(b2, gbc);
gbc.weighty = 1.0;
J gbc.gridwidth = GridBagConstraints.RELATIVE;
gbl.setConstraints(b3, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbl.setConstraints(b4, gbc);
add(b1);
add(b2);
add(b3);
add(b4);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
Main mf = new Main();
}
}
J Swing
› O limitare a AWT este aceea ca se bazează pe sistemul de operare
pentru crearea controalelor GUI (buttons, checkbox etc.). Acest lucru
poate sa difere de la un sistem de operare la altul.
J Caracteristici ale Swing
› Lightweight Components
– componente GUI nu implica sistemul de operare în creare acestora.
– Swing este parte a JFC – Java Fundation Classes
– Componente swing necesita mai puține resurse comparativ cu echivalentele lor AWT
› Pluggable Look and Feel
– Aparența poate fi separată de comportament. Acest lucru permite programatorilor sa
asigneze diferite teme pentru aceeași componentă fără ai schimba comportamentul
J Diferente AWT - Swing
J Model – View – Controller (MVC)
› Este un sablon arhitectural folosit pe larg în dezvoltare software.
› Fiecare control GUI sau componentă conține trei aspecte:
– Aparența – felul în care arată componenta când este randată pe ecran
– Felul în care componenta reacționează la acțiunile utilizatorului
– Informația de stare asociată componentei
› MVC poate fi folosit pentru a separa aceste aspecte. Odată separate,
acestea pot fi modificate separat
J MVC
› Model – gestionează
datele, logica și regulile
aplicației
› View – reprezintă datele
utilizatorului
› Controller – acceptă acțiuni
ale utilizatorului și le
convertește în comenzi
către Model și View
J Componente
J Containers
› Swing furnizează două tipuri de containere
– heavyweight containers (top-level containers): JFrame, JApplet, JWindow, și
Jdialog. Toate containerele moștenesc clasele AWT Componente și Container
– lightweight containers: pot fi imbricate în “heavyweight containers”. Toate moștenesc
clasa JComponent: ex. JPanel

S-ar putea să vă placă și