1 / 12

Osnove programiranja (v Javi)

Osnove programiranja (v Javi). 10. del 31. januar 2005 Marko Štamcar http://java.stamcar.com/. Uvod v delo z grafiko. Dve knjižnici za programiranje GUI (graphical user interface) AWT (Abstract Window Toolkit) Starejša knjižnica Na različnih platformah različen izgled Swing

kiefer
Download Presentation

Osnove programiranja (v Javi)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Osnove programiranja (v Javi) 10. del 31. januar 2005 Marko Štamcar http://java.stamcar.com/

  2. Uvod v delo z grafiko • Dve knjižnici za programiranje GUI (graphical user interface) • AWT (Abstract Window Toolkit) • Starejša knjižnica • Na različnih platformah različen izgled • Swing • Novejša knjižnica • Na vseh platformah enak izgled • V programih kombiniramo uporabo obeh

  3. Okno Frame  JFrame http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Frame.html

  4. Osnutek kode import java.awt.*; import javax.swing.*; public class GUIApp { public static void main(String[] args) { // } }

  5. Razred, ki deduje lastnosti razreda JFrame import java.awt.*; import javax.swing.*; public class GUIApp { public static void main(String[] args) { // } } class Okno extends JFrame { public Okno() { // “nastavitve” okna } }

  6. “Nastavitve” okna • setTitle("Naslov okna"); • setSize(570,500); • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  7. Popravek osnutka kode • Namesto // damo: • JFrame okno = new Okno(); • okno.show(); import java.awt.*; import javax.swing.*; public class GUIApp { public static void main(String[] args) { JFrame okno = new Okno(); okno.show(); } } class Okno extends JFrame { public Okno() { // “nastavitve” okna } }

  8. Dodamo “nastavitve” okna import java.awt.*; import javax.swing.*; public class GUIApp { public static void main(String[] args) { JFrame okno = new Okno(); okno.show(); } } class Okno extends JFrame { public Okno() { setTitle("Naslov okna"); setSize(570,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

  9. Dodajanje vsebine • Kodo dodajamo v konstruktor za okno: • Najprej potrebujemo Container, kamor bomo dodajali elemente: • Container vsebina = getContentPane(); • Izpeljati moramo nek razred iz razreda JPanel (naslednja prosojnica) • Nato dodamo risalno površino: • JPanel panel1 = new Panel1(); • vsebina.add(panel1);

  10. JPanel • Je “neviden”. Lahko ga uporabimo za risalno površino v JFrame: class Panel1 extends JPanel { public void paintComponent(Graphics grafika) { // ukazi za risanje } } http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html

  11. Če vse to sestavimo skupaj, dobimo: import java.awt.*; import javax.swing.*; public class GUIApp { public static void main(String[] args) { JFrame okno = new Okno(); okno.show(); } } class Okno extends JFrame { public Okno() { setTitle("Naslov okna"); setSize(570,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container vsebina = getContentPane(); JPanel panel1 = new Panel1(); vsebina.add(panel1); } } class Panel1 extends JPanel { public void paintComponent(Graphics grafika) { // ukazi za risanje } }

  12. Risanje • Poteka znotraj metodepublic void paintComponent(Graphics grafika) • Primer: • grafika.setColor(Color.black); • grafika.drawRect(15,15,140,140); http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html

More Related