Layouts and Graphics component container layout Un Container

  • Slides: 16
Download presentation
Layouts and Graphics

Layouts and Graphics

component - container - layout Un Container contiene [0 o +] Components Il Layout

component - container - layout Un Container contiene [0 o +] Components Il Layout specifica come i Components sono disposti nel Container Un Container è un Component (quindi il contenimento è ricorsivo) Un Component ha una Graphics associata

Layouts Gestione del Layout Vedi anche: http: //java. sun. com/docs/books/tutorial/uiswing/layout/index. html

Layouts Gestione del Layout Vedi anche: http: //java. sun. com/docs/books/tutorial/uiswing/layout/index. html

Layouts

Layouts

Layouts

Layouts

Layouts

Layouts

Layouts http: //java. sun. com/docs/books/tutorial/uiswing/layout/spring. html

Layouts http: //java. sun. com/docs/books/tutorial/uiswing/layout/spring. html

class Yellow. Window package it. unitn. science. prog 2. gui. App; import java. awt.

class Yellow. Window package it. unitn. science. prog 2. gui. App; import java. awt. *; import javax. swing. *; public class Yellow. Window extends JFrame { private JPanel content. Pane; public Yellow. Window() { try { jb. Init(); } catch(Exception e) { e. print. Stack. Trace(); } } private void jb. Init() throws Exception { content. Pane=(JPanel)this. get. Content. Pane(); this. set. Size(new Dimension(400, 300)); content. Pane. set. Background(Color. YELLOW); } }

class App package it. unitn. science. prog 2. gui. App; import javax. swing. *;

class App package it. unitn. science. prog 2. gui. App; import javax. swing. *; import java. awt. *; public class App { JFrame finestra=null; public static void main(String[ ] a){ new App(); } public App() { // aggiungere qui: set look&feel (vedi oltre) this. setup. Graphic. Environment(); }

class App private void setup. Graphic. Environment() { finestra = new Yellow. Window(); //new

class App private void setup. Graphic. Environment() { finestra = new Yellow. Window(); //new My. Window // trova le dimensioni dello schermo e della finestra Dimension screen. Size = Toolkit. get. Default. Toolkit(). get. Screen. Size(); Dimension frame. Size = finestra. get. Size(); // assicurati che la finestra non sia più grande dello schermo if (frame. Size. height > screen. Size. height) frame. Size. height = screen. Size. height; if (frame. Size. width > screen. Size. width) frame. Size. width = screen. Size. width;

class App // centra la finestra nello schermo finestra. set. Location((screen. Size. width -

class App // centra la finestra nello schermo finestra. set. Location((screen. Size. width - frame. Size. width) / 2, (screen. Size. height - frame. Size. height) / 2); // fai in modo che la chiusura della finestra // termini l'applicazione finestra. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE ); // rendi la finestra visibile finestra. set. Visible(true); } }

Border. Layout In Yellow. Window cambio il metodo jb. Init: private void jb. Init()

Border. Layout In Yellow. Window cambio il metodo jb. Init: private void jb. Init() throws Exception { JPanel f=(JPanel)this. get. Content. Pane(); JButton north, south, east, west, center; north = new JButton("North"); east = new JButton("East"); west = new JButton("West"); center = new JButton("Center"); south = new JButton("South"); f. set. Layout(new Border. Layout(2, 2)); f. add(north, Border. Layout. NORTH); f. add(south, Border. Layout. SOUTH); f. add(east, Border. Layout. EAST); f. add(west, Border. Layout. WEST); f. add(center, Border. Layout. CENTER); this. set. Size(300, 300); }

Flow. Layout In Yellow. Window cambio il metodo jb. Init: private void jb. Init()

Flow. Layout In Yellow. Window cambio il metodo jb. Init: private void jb. Init() throws Exception { JPanel f=(JPanel)this. get. Content. Pane(); JButton one, two, three, four, five, six; one = new JButton("one"); two = new JButton("two"); three = new JButton("three"); four = new JButton("four"); five = new JButton("five"); six = new JButton("six"); f. set. Layout(new Flow. Layout()); f. add(one); f. add(six); f. add(five); // attenzione all’ordine! f. add(two); f. add(three); f. add(four); // aggiungo five per la seconda volta: - la prima viene eliminata ! f. add(five); this. set. Size(300, 300); }

Border. Layout In Yellow. Window cambio il metodo jb. Init: private void jb. Init()

Border. Layout In Yellow. Window cambio il metodo jb. Init: private void jb. Init() throws Exception { JPanel f=(JPanel)this. get. Content. Pane(); f. set. Layout(new Grid. Layout(3, 4)); JButton b[]=new JButton[10]; // VETTORE DI 10 BOTTON for (int k=0; k<10; k++){ b[k]=new JButton(); // Integer. to. String(int a) traduce l’intero a in una String b[k]. set. Label(Integer. to. String(k)); f. add(b[k]); } this. set. Size(300, 300); }

Border. Layout In Yellow. Window cambio il metodo jb. Init: private void jb. Init()

Border. Layout In Yellow. Window cambio il metodo jb. Init: private void jb. Init() throws Exception{ JPanel f=(JPanel)this. get. Content. Pane(); Card. Layout cl=new Card. Layout(); f. set. Layout(cl); JPanel p[ ]=new JPanel[5]; Color c[ ]={Color. red, Color. orange, Color. green, Color. blue, Color. pink}; for (int k=0; k<5; k++){ p[k]=new JPanel(); p[k]. set. Background(c[k]); f. add(Integer. to. String(k), p[k]); // il primo parametro è una stringa con la quale riferirsi al componente aggiunto } this. set. Size(300, 300); this. set. Visible(true); while (true) { // ciclo infinito try { Thread. sleep(800); // dormi per 500 millisecondi } catch (Exception e) { } cl. next(f); // richiama il prossimo componente } }

Posizionamento assoluto: Null Layout It's possible to set the layout manager to null: no

Posizionamento assoluto: Null Layout It's possible to set the layout manager to null: no layout control. You might do this to position an object on the display at some absolute coordinates. This is almost never the right approach. Components might have different minimum sizes on different platforms, and your interface would not be very portable. import java. awt. *; public class Applicazione { public static void main(String s[]) {Applicazione a=new Applicazione()} Applicazione() { JFrame g= new JFrame ("Finestra controllata da BORDER Layout"); JPanel f=(JPanel)(g. get. Content. Frame(); f. set. Size(200, 200); // Dimensione della finestra f. set. Location(50, 100); // Posizione della finestra JButton b=new JButton(("Push me"); f. set. Layout(null); f. add(b); b. set. Size(25, 75); // Dimensiono il Bottone b. set. Location(10, 100); // Posiziono il bottone nella finestra g. set. Visible(true); } }