La presentazione è in caricamento. Aspetta per favore

La presentazione è in caricamento. Aspetta per favore

Layouts and Graphics. component - container - layout Un Container contiene [0 o +] Components Il Layout specifica come i Components sono disposti nel.

Copie: 2
Layouts and Graphics. component - container - layout Un Container contiene [0 o +] Components Il Layout specifica come i Components sono disposti nel.

Inner Classes Ancora eventi. 2 Design considerations The most important rule to keep in mind about event listeners that they should execute very quickly.

Presentazioni simili


Presentazione sul tema: "Layouts and Graphics. component - container - layout Un Container contiene [0 o +] Components Il Layout specifica come i Components sono disposti nel."— Transcript della presentazione:

1 Layouts and Graphics

2 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

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

4 Layouts

5

6

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

8 class YellowWindow package it.unitn.science.prog2.guiApp; import java.awt.*; import javax.swing.*; public class YellowWindow extends JFrame { private JPanel contentPane; public YellowWindow() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { contentPane=(JPanel)this.getContentPane(); this.setSize(new Dimension(400, 300)); contentPane.setBackground(Color.YELLOW); }

9 class App package it.unitn.science.prog2.guiApp; 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.setupGraphicEnvironment(); }

10 class App private void setupGraphicEnvironment() { finestra = new YellowWindow(); //new MyWindow // trova le dimensioni dello schermo e della finestra Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = finestra.getSize(); // assicurati che la finestra non sia più grande dello schermo if (frameSize.height > screenSize.height) frameSize.height = screenSize.height; if (frameSize.width > screenSize.width) frameSize.width = screenSize.width;

11 class App // centra la finestra nello schermo finestra.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); // fai in modo che la chiusura della finestra // termini l'applicazione finestra.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE ); // rendi la finestra visibile finestra.setVisible(true); }

12 BorderLayout In YellowWindow cambio il metodo jbInit: private void jbInit() throws Exception { JPanel f=(JPanel)this.getContentPane(); 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.setLayout(new BorderLayout(2, 2)); f.add(north, BorderLayout.NORTH); f.add(south, BorderLayout.SOUTH); f.add(east, BorderLayout.EAST); f.add(west, BorderLayout.WEST); f.add(center, BorderLayout.CENTER); this.setSize(300,300); }

13 FlowLayout In YellowWindow cambio il metodo jbInit: private void jbInit() throws Exception { JPanel f=(JPanel)this.getContentPane(); 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.setLayout(new FlowLayout()); f.add(one); f.add(six); f.add(five); // attenzione allordine! f.add(two); f.add(three); f.add(four); // aggiungo five per la seconda volta: - la prima viene eliminata ! f.add(five); this.setSize(300,300); }

14 BorderLayout In YellowWindow cambio il metodo jbInit: private void jbInit() throws Exception { JPanel f=(JPanel)this.getContentPane(); f.setLayout(new GridLayout(3,4)); JButton b[]=new JButton[10]; // VETTORE DI 10 BOTTON for (int k=0; k<10; k++){ b[k]=new JButton(); // Integer.toString(int a) traduce lintero a in una String b[k].setLabel(Integer.toString(k)); f.add(b[k]); } this.setSize(300,300); }

15 BorderLayout In YellowWindow cambio il metodo jbInit: private void jbInit() throws Exception{ JPanel f=(JPanel)this.getContentPane(); CardLayout cl=new CardLayout(); f.setLayout(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].setBackground(c[k]); f.add(Integer.toString(k),p[k]); // il primo parametro è una stringa con la quale riferirsi al componente aggiunto } this.setSize(300,300); this.setVisible(true); while (true) { // ciclo infinito try { Thread.sleep(800); // dormi per 500 millisecondi } catch (Exception e) { } cl.next(f); // richiama il prossimo componente }

16 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.getContentFrame(); f.setSize(200,200); // Dimensione della finestra f.setLocation(50,100); // Posizione della finestra JButton b=new JButton(("Push me"); f.setLayout(null); f.add(b); b.setSize(25,75); // Dimensiono il Bottone b.setLocation(10,100); // Posiziono il bottone nella finestra g.setVisible(true); }


Scaricare ppt "Layouts and Graphics. component - container - layout Un Container contiene [0 o +] Components Il Layout specifica come i Components sono disposti nel."

Presentazioni simili


Annunci Google