Results 1 to 7 of 7
  1. #1

    Title
    Principal
    Join Date
    Nov 2006
    Posts
    31
    Thanks Thanks Given 
    0
    Thanks Thanks Received 
    0
    Thanked in
    0 Posts

    Bild aktualisieren

    hallo,

    ich habe folgendes Problem: ich schreib mir gerade ein Programm, das eine Bildergalerie realisiert, wenn ich ein Ereignis auslöse, will ich dass sich das aktuelle Bild ändert, tut es aber leider ned.

    ich poste mal mein Code-Fragment:

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.net.URL;
    
    public class Gallery extends JFrame 
    {
    //....
    
    public JLabel bild;
    public JComboBox year;
    public JComboBox event;
    public Icon icon;
    public URL path;
    public String year_path = "2007";
    public String event_path = "Test";
     // ....
    
    public Gallery ()
    {
    
    //....
    
    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild.jpg");
    icon = new ImageIcon(path);
    bild = new JLabel();
    bild.setIcon(icon);
    bild.addItemListener(this);
    
    //...
    
    }
    
    //...
    
    public void itemStateChanged (ItemEvent e)
    {
    if (...)
    {
    event_path = "andrerPfad";
    bild.setIcon(icon);
    // hier ist wohl der Fehler, aber was ist falsch???
    
    
    }
    
    }
    }
    vielen dank schon mal!!

  2. #2

    Title
    Elite
    Join Date
    Oct 2008
    Posts
    287
    Thanks Thanks Given 
    51
    Thanks Thanks Received 
    88
    Thanked in
    50 Posts
    Ist das im Pseudocode rausgeschnitten oder rufst du wirklich nach der Veränderung nirgends repaint() auf? Falls nicht, probier das mal.

  3. #3

    Title
    Principal
    Join Date
    Nov 2006
    Posts
    31
    Thanks Thanks Given 
    0
    Thanks Thanks Received 
    0
    Thanked in
    0 Posts
    nein das stimmt so schon, hab kein repaint(); drin, aber er zeigt mir des eine bild auch problemlos so an!!! bloß wenn ich aktualisieren will dann gehts eben ned...
    wie müsst ich denn des mit repaint dann machen??

  4. #4

    Title
    Elite
    Join Date
    Oct 2008
    Posts
    287
    Thanks Thanks Given 
    51
    Thanks Thanks Received 
    88
    Thanked in
    50 Posts
    ganz einfach repaint() für alle components aufrufen, die du nach einer änderung neu zeichnen willst.

  5. #5
    Erklärbär's Avatar
    Title
    Elite
    Join Date
    Apr 2005
    Location
    Wien
    Posts
    263
    Thanks Thanks Given 
    6
    Thanks Thanks Received 
    14
    Thanked in
    13 Posts
    Code:
    event_path = "andrerPfad";
    bild.setIcon(icon);
    ich glaub da fehlt ein
    Code:
    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild.jpg");
    icon = new Icon(path);
    vor dem setIcon.
    einfach nur genial: wenn man im wort "Mama" 4 buchstaben ändert, dann hat man auf einmal "Bier"

  6. #6

    Title
    Principal
    Join Date
    Nov 2006
    Posts
    31
    Thanks Thanks Given 
    0
    Thanks Thanks Received 
    0
    Thanked in
    0 Posts
    jo hattest recht, vielen dank :-)

  7. #7

    Title
    Principal
    Join Date
    Nov 2006
    Posts
    31
    Thanks Thanks Given 
    0
    Thanks Thanks Received 
    0
    Thanked in
    0 Posts
    habe leider wieder n kleines problem:

    hier mal der komplette code (als applet):

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.net.URL;
    
    
    
    public class Gallery extends JApplet implements ItemListener, ActionListener
    {
        public Color c;
        public Color c2;
        public JButton prev;
        public JButton next;
        public JLabel bild;
        public Border border;
        public JComboBox year;
        public JComboBox event;
        public JLabel anz;
        public JPanel p;
        public JPanel q;
        public JPanel r;
        public Icon icon;
        public URL path;
        public String[] year_feld = {"Jahr", "2007", "2008", "2009"};
        public String[] event_feld = {"Event", "Garten", "Lonetal", "Eurasburg", "Chiemsee", "Staffelsee", "Kanada", "Bot. Garten München",
                "Bot. Garten Augsburg", "Autosalon Augsburg", "Paardurchbruch"};
        public String year_path = "Main";
        public String event_path = "";
        public int[] insges = {1,36,25,11,12,7,51,13,18,9,17};
        public int akt = 1;
        public int eventid = 0;
        
        
    
        public void init ()
        {
            setSize(600,600);
            c = new Color(51,51,51);
            c2 = new Color(223,223,223);
            p = new JPanel();
            p.setLayout(new BorderLayout());
            prev = new JButton("Previous");
            next = new JButton("Next");
            p.add(prev, "West");
            p.add(next, "East");
            year = new JComboBox();
            event = new JComboBox();
            anz = new JLabel("Bild " + akt + "/" + insges[eventid]);
            anz.setForeground(c2);
            year.addItem(year_feld[0]);
            year.addItem(year_feld[1]);
            year.addItem(year_feld[2]);
            year.addItem(year_feld[3]);
            year.addItemListener(this);
            event.addItem(event_feld[0]);
            event.addItemListener(this);
            q = new JPanel();
            q.add(year, "West");
            q.add(anz, "East");
            q.add(event, "Center");
            p.add(q, "Center");
            q.setBackground(c);
            r = new JPanel();
            r.setLayout(new BorderLayout());
            r.add(p, "South");
            r.setBackground(c);
            path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild1.jpg");
            icon = new ImageIcon(path);
            bild = new JLabel();
            bild.setIcon(icon);
            r.add(bild, "North");
            next.addActionListener(this);
            prev.addActionListener(this);
            add(r);
            setVisible(true);
            
    
        }
    
    
        public void itemStateChanged (ItemEvent e)
        {
            if (e.getSource() == year)
            {
                if ((String)year.getSelectedItem() == year_feld[1])
                {
                    year.removeItem(year_feld[0]);
                    event.removeAllItems();
                    event.addItem(event_feld[1]);
                    event.addItem(event_feld[2]);
                    event.addItem(event_feld[3]);
                    event.addItem(event_feld[4]);
                    event.addItem(event_feld[5]);
                    event.addItem(event_feld[6]);
                }
    
                else if ((String)year.getSelectedItem() == year_feld[2])
                {
                    year.removeItem(year_feld[0]);
                    event.removeAllItems();
                    event.addItem(event_feld[7]);
                    event.addItem(event_feld[8]);
                    event.addItem(event_feld[9]);
                }
    
                else if ((String)year.getSelectedItem() == year_feld[3])
                {
                    year.removeItem(year_feld[0]);
                    event.removeAllItems();
                    event.addItem(event_feld[10]);
                }
            }
    
            else if (e.getSource() == event)
            {
                if (((String)year.getSelectedItem() == year_feld[1]) && ((String)event.getSelectedItem() == event_feld[1]))
                {
                    year_path = year_feld[1];
                    event_path = event_feld[1];
                    akt = 1;
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    eventid = 1;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                }
    
                else if (((String)year.getSelectedItem() == year_feld[1]) && ((String)event.getSelectedItem() == event_feld[2]))
                {
                    event_path = event_feld[2];
                    akt = 1;
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    eventid = 2;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    
                }
    
                else if (((String)year.getSelectedItem() == year_feld[1]) && ((String)event.getSelectedItem() == event_feld[3]))
                {
                    event_path = event_feld[3];
                    akt = 1;
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    eventid = 3;
                    
                }
    
                else if (((String)year.getSelectedItem() == year_feld[1]) && ((String)event.getSelectedItem() == event_feld[4]))
                {
                    event_path = event_feld[4];
                    akt = 1;
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    eventid = 4;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    
                }
    
                else if (((String)year.getSelectedItem() == year_feld[1]) && ((String)event.getSelectedItem() == event_feld[5]))
                {
                    event_path = event_feld[5];
                    akt = 1;
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    eventid = 5;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    
                }
    
                else if (((String)year.getSelectedItem() == year_feld[1]) && ((String)event.getSelectedItem() == event_feld[6]))
                {
                    event_path = event_feld[6];
                    akt = 1;
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    eventid = 6;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    
                }
    
                else if (((String)year.getSelectedItem() == year_feld[2]) && ((String)event.getSelectedItem() == event_feld[7]))
                {
                    year_path = year_feld[2];
                    event_path = event_feld[7];
                    akt = 1;
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    eventid = 7;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    
                }
    
                else if (((String)year.getSelectedItem() == year_feld[2]) && ((String)event.getSelectedItem() == event_feld[8]))
                {
                    year_path = year_feld[2];
                    event_path = event_feld[8];
                    akt = 1;
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    eventid = 8;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    
                }
    
                else if (((String)year.getSelectedItem() == year_feld[2]) && ((String)event.getSelectedItem() == event_feld[9]))
                {
                    year_path = year_feld[2];
                    event_path = event_feld[9];
                    akt = 1;
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    eventid = 9;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    
                }
    
                else if (((String)year.getSelectedItem() == year_feld[3]) && ((String)event.getSelectedItem() == event_feld[10]))
                {
                    year_path = year_feld[3];
                    event_path = event_feld[10];
                    akt = 1;
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    eventid = 10;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    
                }
            }
        }
    
        
    
    
        public void actionPerformed (ActionEvent e)
        {
            if (e.getSource() == next)
            {
                if (akt < insges[eventid])
                {
                    akt++;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    
                }
            }
    
            else if (e.getSource() == prev)
            {
                if ((akt <= insges[eventid]) && (akt > 1))
                {
                    akt--;
                    anz.setText("Bild " + akt + "/" + insges[eventid]);
                    path = Gallery.class.getResource(year_path + "\\" + event_path + "\\"+ "bild" + akt + ".jpg");
                    icon = new ImageIcon(path);
                    bild.setIcon(icon);
                    
                }
            }
        }
    
    }
    jetz frag ich mich, wenn man als jahr 2008 auswählt, warum er mir nicht die untermenüs geschweige denn das richtige bild anzeigt.
    ich hab des selbe programm nicht als applet, sondern als normale java-applikation, da gehts, aber ich check einfach ned warum....

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •