Probleme mit MouseWheelListener und JLayeredPane in Applet

Dreloc

Neues Mitglied
Also ich hab folgendes Problem:

-1 Applet mit einem JPanel
-Das JPanel hat ein JLayeredPane
-Das(der?) JLayeredPane hat:

-1 rotes Panel (das über das ganze JLayeredPane aufgespannt ist und auf dem per Mausrad gezoomt werden soll)
-1 gelbes Panel das
-mehrere grüne Panel beinhaltet auf denen Aktionen (Buttons angeklickt werden).

Ablauf:
1. Insofern man nun auf den Button im roten Panel klickt soll das gelbe Panel erscheinen.
2. Sobald man im roten Panel irgendwo anders hinklickt soll das gelbe Panel wieder verschwinden.
3. Auf den grünen Panel soll man eine Aktion ausführen können.

Währenddessen soll man immer im Panel1 scrollen können. D.h.: das Mausrad soll erkannt werden.

Ich hab das Ganze einmal einfach nachprogrammiert:

normal.jpg


Das funktioniert ohne Probleme, jedoch sobald ich den Button im grünen Panel anwähle, funktioniert sobald ich das gelbe Panel wieder ausblende, die Erkennung des MouseWheelListeners auf dem Roten Panel nicht mehr. Mousemovement wird jedoch sehr wohl noch erkannt

anormal.jpg



Wenn ich nun das Panel das ich dem Applet zuweise in einem JFrame öffne, funktioniert alles ohne Probleme(Das Mousewheel wird auf dem roten Panel immer erkannt, auch wenn ich den Button im grünen Panel klicke), aber sobald ich das ganze im Applet mache, erkennt er den MouseWheelListener auf dem roten Panel nicht mehr.

und hier noch der Code zum downloaden und nachprobieren:

http://stud3.tuwien.ac.at/~e0400242/upload/src.zip

Example.java

Java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

public class Example extends JPanel {

    JPanel panel1 = new Panel1();
    JPanel panel2 = new Panel2();

    public Example() {

        this.setBackground(Color.BLUE);

        SpringLayout layout = new SpringLayout();

        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setLayout(layout);

        layeredPane.setPreferredSize(new Dimension(250, 250));

        layout.putConstraint(SpringLayout.EAST, panel2, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.NORTH, panel2, 0, SpringLayout.NORTH, this);
        layeredPane.add(panel2, 2, 1);

        panel2.setVisible(false);

        layout.putConstraint(SpringLayout.SOUTH, panel1, 0, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.EAST, panel1, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, panel1, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, panel1, 0, SpringLayout.NORTH, this);
        layeredPane.add(panel1, 1, 1);
        this.add(layeredPane);
        layeredPane.setVisible(true);

    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("LayeredPaneDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new Example();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }

    class Panel1 extends JPanel implements ActionListener, MouseMotionListener, MouseWheelListener, MouseListener {

        public Panel1() {

            JButton button1 = new JButton("Display Side Bar");
            button1.addActionListener(this);
            SpringLayout layout = new SpringLayout();
            this.setLayout(layout);
            this.add(button1);
            layout.putConstraint(SpringLayout.WEST, button1, 0, SpringLayout.WEST, this);
            layout.putConstraint(SpringLayout.NORTH, button1, 0, SpringLayout.NORTH, this);

            this.setBackground(Color.red);
            this.setPreferredSize(new Dimension(250, 250));

            this.addMouseMotionListener(this);
            this.addMouseListener(this);
            this.addMouseWheelListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            System.out.println("Panel1 - Action performed");
            panel2.setVisible(true);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println("Panel1 - Mouse Dragged");
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            System.out.println("Panel1 - Mousemoved");
        }

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            System.out.println("Panel1 - Mouse Wheel rotated");
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println("Panel1 - Mouse clicked");
        }

        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println("Panel1 - Mouse pressed");
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            panel2.setVisible(false);
            System.out.println("Panel1 - Mouse relased");
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println("Panel1 - Mouse entered");
        }

        @Override
        public void mouseExited(MouseEvent e) {
            System.out.println("Panel1 - Mouse exited");
        }
    }

    class Panel2 extends JPanel {

        public Panel2() {
            this.setBackground(Color.YELLOW);
            this.setPreferredSize(new Dimension(125, 250));

            Panel3 panel3 = new Panel3();

            this.add(panel3);

        }
    }

    class Panel3 extends JPanel implements ActionListener, MouseMotionListener, MouseWheelListener {

        public Panel3() {
            this.setBackground(Color.GREEN);
            this.setPreferredSize(new Dimension(125, 50));

            JButton button2 = new JButton("Do some Action");
            button2.addActionListener(this);

            this.add(button2);

            this.addMouseMotionListener(this);
            this.addMouseWheelListener(this);

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Panel3 - Action performed");
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println("Panel3 - Mouse Dragged");
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            System.out.println("Panel3 - Mousemoved");
        }

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            System.out.println("Panel3 - Mouse Wheel rotated");
        }
    }
}

TestApplet.java

Java:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.HeadlessException;
import javax.swing.SpringLayout;

public class TestApplet extends Applet{
    
    private static Applet instance;
    private SpringLayout layout = new SpringLayout();

    public TestApplet() throws HeadlessException {
        super();
        instance = this;
    }

    public static Applet getApplet() {
        return instance;
    }

    public void init() {

        this.setLayout(layout);

        int width = 250;
        int height = 250;
        this.setSize(width, height);
        this.setPreferredSize(new Dimension(width, height));
        this.setBackground(Color.BLACK);

        Example ex = new Example();

        layout.putConstraint(SpringLayout.NORTH, ex, 0, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, ex, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.EAST, ex, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.SOUTH, ex, 0, SpringLayout.SOUTH, this);

        this.add(ex);

    }


    @Override
    public void start() {
        super.start();
        super.init();



    }

}

Hoffe jemand hat ne Idee,
Grüße Dre
 
Dein Code des Applets ist fragwürdig. Es ist eine AWT-Komponente, der du eine Swing-Komponente einpflanzt. AWT und Swing sollten besser nicht gemischt werden. Man erspart sich einige Probleme bei der Darstellung -> FAQ.

Und deine start()-Methode im Applet solltest du besser löschen, die haut gar nicht hin.
Erstmal falsche Reihenfolge und dann werden die leeren Implementierungen in der Superklasse aufgerufen...
 

Zurück
Oben