JMenu unter JPanel versteckt

Strasser

Mitglied
Hallo zusammen!

Bei mir versteckt sich das JMenu unter dem JPanel (siehe Bild)
attachment.php


Das ganze ist ein JFrame, darauf ist ein JLayeredPane, auf dem alle Componenten drauf sind.
Mit setJMenuBar ist das Menü auf dem JFrame drauf.

Das Schwarze, was das Menü verdeckt, ist ein JLayeredPane, dass erst nach einem Methodenaufruf dem content-JLayeredPane hinzugefügt wird.

Java:
public class ESTW_Developer extends JFrame
{
    private JLayeredPane contentPanel;
    private JDialog estw_daten;
    private JDialog lupen_daten;
    
    // Menu-Leiste:   
    private JMenuItem Neu;
    private JMenuItem Oeffnen;
    private JMenuItem Speichern;
    private JMenuItem Speichern_unter;
    private JMenuItem Beenden;
    
    private JMenuItem Eigenschaften; 
    
    private JMenuItem Lupe_einfuegen;
    
    // Funktionen:
    private boolean geladen;
    private boolean editiert;
    
    // Geladene Datei:
    private String Datei = "";
    
    // Felder im Standard-Fenster:
    private JLabel lupenname;

    // Lupenscroller:    
    private JScrollPane lupenscroller; 
    private JPanel lupenpanel;
    
    public static void main(String[] args) throws IOException
    {
        new ESTW_Developer();
    }
    
    public ESTW_Developer()     
    {
        super("ESTW-Developer");
        
        contentPanel = new JLayeredPane();
        add(contentPanel);
        
        getContentPane().setBackground(Color.LIGHT_GRAY);
        setBackground(Color.LIGHT_GRAY);
        contentPanel.setBackground(Color.LIGHT_GRAY);
        contentPanel.setLayout(null);
        
        setLocation(0,0);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                Object[] options = { "Ja", "Nein"};
                int antwort = (JOptionPane.showOptionDialog(null, "Möchten Sie das Programm wirklich beenden?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
                if (antwort == 0)
                {
                    System.exit(1); 
                }
            }
        });        
        
        JMenuBar menuBar = new JMenuBar();
        
        JMenu Datei = new JMenu("Datei");
        JMenu Bearbeiten = new JMenu("Bearbeiten");
        JMenu Einfuegen = new JMenu("Einfügen");
        JMenu Hilfe = new JMenu("Hilfe");
        
        Neu = new JMenuItem("Neu");
        Neu.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    if (geladen)
                    {
                        Object[] options = { "Ja", "Nein"};
                        int antwort = (JOptionPane.showOptionDialog(null, "Möchten Sie wirklich ein neues ESTW erstellen?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
                        if (antwort == 0)
                        {
                            name = "";
                            kuerzel = "";
                            ESTW_Daten();
                        }
                    }
                    else
                    {
                        ESTW_Daten();
                    }
                }
            }); 
        Oeffnen = new JMenuItem("Öffnen");
        Oeffnen.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    if (geladen)
                    {
                        Object[] options = { "Ja", "Nein"};
                        int antwort = (JOptionPane.showOptionDialog(null, "Möchten Sie wirklich ein anderes ESTW öffnen?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
                        if (antwort == 0)
                        {
                            name = "";
                            kuerzel = "";
                            Oeffnen();
                        }
                    }
                    else
                    {
                        Oeffnen();
                    }
                }
            }); 
        Speichern = new JMenuItem("Speichern");
        Speichern.setEnabled(false);
        Speichern.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    Speichern();
                }
            }); 
        Speichern_unter = new JMenuItem("Speichern unter");
        Speichern_unter.setEnabled(false);
        Speichern_unter.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    Speichern_unter();
                }
            }); 
        Beenden = new JMenuItem("Beenden");
        Beenden.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    Object[] options = { "Ja", "Nein"};
                    int antwort = (JOptionPane.showOptionDialog(null, "Möchten Sie das Programm wirklich beenden?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
                    if (antwort == 0)
                    {
                        System.exit(1); 
                    }
                }
            }); 
        
        Datei.add(Neu);
        Datei.addSeparator();
        Datei.add(Oeffnen);
        Datei.add(Speichern);
        Datei.add(Speichern_unter);
        Datei.addSeparator();
        Datei.add(Beenden);
        
        Eigenschaften = new JMenuItem("Eigenschaften");
        Eigenschaften.setEnabled(false);
        Eigenschaften.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    ESTW_Daten();
                }
            }); 
            
        Bearbeiten.add(Eigenschaften);
        
        Lupe_einfuegen = new JMenuItem("Lupe einfügen");
        Lupe_einfuegen.setEnabled(false);
        Lupe_einfuegen.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    lupe_dat.setText("");
                    
                    //if (selectedLupe != null)
                    //    contentPanel.remove(selectedLupe);
                        
                    //selectedLupe = null;
                    //lupenname.setText("");
                    editLupe = false;
                    EditLupe();
                }
            }); 
            
        Einfuegen.add(Lupe_einfuegen);
        
        menuBar.add(Datei);
        menuBar.add(Bearbeiten);
        menuBar.add(Einfuegen);
        menuBar.add(Hilfe);
        
        setJMenuBar(menuBar); 
        
        lupenname = new JLabel();
        lupenname.setSize(200,20);
        lupenname.setLocation(420, 600);
        lupenname.setVisible(true);        
        lupenname.setHorizontalAlignment(SwingConstants.CENTER);
        contentPanel.add(lupenname);
        
        lupenpanel = new JPanel();
        lupenscroller = new JScrollPane(lupenpanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        lupenscroller.setSize(1030, 85);
        lupenscroller.setLocation(2, 650);
        lupenscroller.setVisible(false);
        contentPanel.add(lupenscroller);
        
        pack();
        setSize(1050, 800);
    }
    
    public void selectLupe(LUPE lup)
    {        
        if (selectedLupe != null)
        {
            contentPanel.add(lup, JLayeredPane.DEFAULT_LAYER);
            lupenname.setText("L_" + lup.holeName());
        }
    }
}

public class LUPE extends JLayeredPane
{       
    public LUPE()
    {        
        setSize(1020,528);
        setLocation(7,60);
        setVisible(true);
        
        setForeground(Color.BLACK);

        // Erstellen der Linien (entfernt)
        
        Canvas black = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.BLACK);
                g.fillRect(0,0,1020,528);
            }
        };
        black.setLocation(0,0);
        black.setSize(1020,528);
        black.setVisible(true);
        add(black, JLayeredPane.DEFAULT_LAYER);
        
        validate();
    }
}
 

Anhänge

  • Fenster.jpg
    Fenster.jpg
    109,1 KB · Aufrufe: 57
Der Sourcecode ist unvollständig und "verwirrend"

Es fehlen die Imports, Globale Variablen und diverse Methoden, daher lässt sich der Fehler nicht nachstellen.

Es sieht aber so aus, als ob du auf deiner JLayeredPane (oder auf einer der darauf liegenden Komponenten) die Paint(...) Methode überschreibst und dabei den super(...) Aufruf vergessen hast.
 
Der Code ist nicht vollständig, dass stimmt.
In dem restlichen Code ist aber nichts, was damit zu tun hat und macht es unübersichtlicher. Auserdem ist er dann nicht in einem Post darzustellen...

Trotzdem:

Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.*;

import java.io.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import java.util.ArrayList; 

public class ESTW_Developer extends JFrame
{
    private JLayeredPane contentPanel;
    private JDialog estw_daten;
    private JDialog lupen_daten;

    // Menu-Leiste:   
    private JMenuItem Neu;
    private JMenuItem Oeffnen;
    private JMenuItem Speichern;
    private JMenuItem Speichern_unter;
    private JMenuItem Beenden;

    private JMenuItem Eigenschaften; 
    private JMenuItem LupeUmbenennen;
    private JMenuItem LupeLoeschen;

    private JMenuItem Lupe_einfuegen;

    // Funktionen:
    private boolean geladen;
    private boolean editiert;

    // Geladene Datei:
    private String Datei = "";

    // Felder im Standard-Fenster:
    private JLabel lupenname;

    // Felder Eigenschaften ESTW:
    private JTextField name_dat;
    private JTextField kuerzel_dat;
    private JButton ok;

    // Eigenschaften des ESTWs:
    private String name = "";
    private String kuerzel = "";

    // Felder Eigenschaften Lupe:
    private JTextField lupe_dat;
    private JButton lupe_ok;

    // Lupen:
    private ArrayList<LUPE> lupen;

    private LUPE selectedLupe;
    private boolean editLupe;

    // Lupenscroller:    
    private JScrollPane lupenscroller; 
    private JPanel lupenpanel;

    // Uebergangsvariablen:
    private LUPE transLupe;

    private class MausiLupenScroller extends MouseAdapter implements MouseMotionListener 
    {

        private int startx;
        private int starty;
        private JComponent c = null;
        private int lupe;
        
        public MausiLupenScroller(int lupe)
        {
            this.lupe = lupe;
        }

        public void mouseEntered(MouseEvent e) 
        {
            c = (JComponent) e.getSource();
        }

        public void mousePressed(MouseEvent e) 
        {
            //System.out.println("1: " + e.getX() + "  " + e.getY());
            startx = e.getX();
            starty = e.getY();
        }

        public void mouseReleased(MouseEvent e) 
        {
            //System.out.println("2: " + e.getX() + "  " + e.getY());
            //setPosition(e);
        }

        public void mouseDragged(MouseEvent e) 
        {
            //System.out.println("4: " + e.getX() + "  " + e.getY());
            setPosition(e);
        }

        private void setPosition(MouseEvent e) 
        {
            int neux = c.getLocation().x + e.getX() - startx;
            if (neux < 0)
                neux = 0;
            int neuy = c.getLocation().y /*+ e.getY() - starty*/;
            //c.setLocation(neux, neuy);
            
            if (neux <= c.getLocation().x - 100 && lupe - 1 >= 0)
            {
                LUPE temp = lupen.set(lupe - 1, lupen.get(lupe));
                lupen.set(lupe, temp);
                updateLupenscroller();
                Editieren();
            }
            else if (neux >= c.getLocation().x + 100 && lupen.size() > lupe + 1)
            {
                LUPE temp = lupen.set(lupe + 1, lupen.get(lupe));
                lupen.set(lupe, temp);
                updateLupenscroller();
                Editieren();
            }
        }

        public void mouseMoved(MouseEvent e) 
        {
        }
    }

    public static void main(String[] args) throws IOException
    {
        new ESTW_Developer();
    }

    public ESTW_Developer()     
    {
        super("ESTW-Developer");

        editLupe = false;

        contentPanel = new JLayeredPane();
        add(contentPanel);

        getContentPane().setBackground(Color.LIGHT_GRAY);
        setBackground(Color.LIGHT_GRAY);
        contentPanel.setBackground(Color.LIGHT_GRAY);
        contentPanel.setLayout(null);

        setLocation(0,0);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    Object[] options = { "Ja", "Nein"};
                    int antwort = (JOptionPane.showOptionDialog(null, "Möchten Sie das Programm wirklich beenden?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
                    if (antwort == 0)
                    {
                        System.exit(1); 
                    }
                }
            });        

        JMenuBar menuBar = new JMenuBar();

        JMenu Datei = new JMenu("Datei");
        JMenu Bearbeiten = new JMenu("Bearbeiten");
        JMenu Einfuegen = new JMenu("Einfügen");
        JMenu Hilfe = new JMenu("Hilfe");

        Neu = new JMenuItem("Neu");
        Neu.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    if (geladen)
                    {
                        Object[] options = { "Ja", "Nein"};
                        int antwort = (JOptionPane.showOptionDialog(null, "Möchten Sie wirklich ein neues ESTW erstellen?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
                        if (antwort == 0)
                        {
                            name = "";
                            kuerzel = "";
                            ESTW_Daten();
                        }
                    }
                    else
                    {
                        ESTW_Daten();
                    }
                }
            }); 
        Oeffnen = new JMenuItem("Öffnen");
        Oeffnen.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    if (geladen)
                    {
                        Object[] options = { "Ja", "Nein"};
                        int antwort = (JOptionPane.showOptionDialog(null, "Möchten Sie wirklich ein anderes ESTW öffnen?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
                        if (antwort == 0)
                        {
                            name = "";
                            kuerzel = "";
                            Oeffnen();
                        }
                    }
                    else
                    {
                        Oeffnen();
                    }
                }
            }); 
        Speichern = new JMenuItem("Speichern");
        Speichern.setEnabled(false);
        Speichern.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    Speichern();
                }
            }); 
        Speichern_unter = new JMenuItem("Speichern unter");
        Speichern_unter.setEnabled(false);
        Speichern_unter.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    Speichern_unter();
                }
            }); 
        Beenden = new JMenuItem("Beenden");
        Beenden.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    Object[] options = { "Ja", "Nein"};
                    int antwort = (JOptionPane.showOptionDialog(null, "Möchten Sie das Programm wirklich beenden?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
                    if (antwort == 0)
                    {
                        System.exit(1); 
                    }
                }
            }); 

        Datei.add(Neu);
        Datei.addSeparator();
        Datei.add(Oeffnen);
        Datei.add(Speichern);
        Datei.add(Speichern_unter);
        Datei.addSeparator();
        Datei.add(Beenden);

        Eigenschaften = new JMenuItem("Eigenschaften");
        Eigenschaften.setEnabled(false);
        Eigenschaften.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    ESTW_Daten();
                }
            }); 

        Bearbeiten.add(Eigenschaften);

        LupeUmbenennen = new JMenuItem("Lupe umbennenen");
        LupeUmbenennen.setEnabled(false);
        LupeUmbenennen.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    if (selectedLupe == null)
                        return;
                        
                    lupe_dat.setText(selectedLupe.holeName());
                        
                    editLupe = true;
                    EditLupe();                    
                }
            }); 

        Bearbeiten.add(LupeUmbenennen);

        LupeLoeschen = new JMenuItem("Lupe löschen");
        LupeLoeschen.setEnabled(false);
        LupeLoeschen.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    if (selectedLupe == null)
                        return;   
                        
                    Object[] options = { "Ja", "Nein"};
                    int antwort = (JOptionPane.showOptionDialog(null, "Möchten Sie die Lupe wirklich löschen?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
                    if (antwort == 0)
                    {
                        if (lupen.remove(selectedLupe))
                        {
                            selectLupe(null);
                            Editieren();
                        }
                    } 
                }
            }); 

        Bearbeiten.add(LupeLoeschen);

        Lupe_einfuegen = new JMenuItem("Lupe einfügen");
        Lupe_einfuegen.setEnabled(false);
        Lupe_einfuegen.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    lupe_dat.setText("");
                    
                    editLupe = false;
                    EditLupe();
                }
            }); 

        Einfuegen.add(Lupe_einfuegen);

        menuBar.add(Datei);
        menuBar.add(Bearbeiten);
        menuBar.add(Einfuegen);
        menuBar.add(Hilfe);

        setJMenuBar(menuBar); 

        contentPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, KeyEvent.ALT_MASK), 0);
        contentPanel.getActionMap().put(0, new AbstractAction() 
            {                    
                @Override
                public void actionPerformed(ActionEvent e) 
                {
                    Object[] options = { "Ja", "Nein"};
                    int antwort = (JOptionPane.showOptionDialog(null, "Möchten Sie das Programm wirklich beenden?", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]));
                    if (antwort == 0)
                    {
                        System.exit(1); 
                    }
                }  
            });

        contentPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_MASK), 1);
        contentPanel.getActionMap().put(1, new AbstractAction() 
            {       
                @Override
                public void actionPerformed(ActionEvent e) 
                {
                    ESTW_Daten();
                }
            });

        contentPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), 2);
        contentPanel.getActionMap().put(2, new AbstractAction() 
            {       
                @Override
                public void actionPerformed(ActionEvent e) 
                {
                    if (geladen && editiert)
                        Speichern();
                }
            });

        lupenname = new JLabel();
        lupenname.setSize(200,20);
        lupenname.setLocation(420, 600);
        lupenname.setVisible(true);        
        lupenname.setHorizontalAlignment(SwingConstants.CENTER);
        contentPanel.add(lupenname);

        lupenpanel = new JPanel();
        lupenscroller = new JScrollPane(lupenpanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        lupenscroller.setSize(1030, 85);
        lupenscroller.setLocation(2, 650);
        lupenscroller.setVisible(false);
        contentPanel.add(lupenscroller);

        pack();
        setSize(1050, 800);

        estw_daten = new JDialog(this, "Eigenschaften des ESTWs");
        estw_daten.setSize(600,400);
        estw_daten.setLocation(getWidth()/2 + getX(), getHeight()/2 + getY());
        estw_daten.setVisible(false);
        estw_daten.setBackground(Color.LIGHT_GRAY);
        estw_daten.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        estw_daten.setAlwaysOnTop(true);
        estw_daten.setModal(true);

        JPanel content = new JPanel();
        estw_daten.add(content);
        content.setLayout(null);
        content.setBackground(Color.LIGHT_GRAY);

        JLabel Name = new JLabel("Name des ESTWs:");
        Name.setLocation(15,10);
        Name.setVisible(true);
        Name.setSize(110,20);
        content.add(Name);

        name_dat = new JTextField();
        name_dat.setLocation(130,10);
        name_dat.setVisible(true);
        name_dat.setSize(200,21);
        name_dat.setDocument(new PlainDocument() 
            {
                @Override
                public void insertString(int offset, String text, AttributeSet a) throws BadLocationException 
                {
                    if (text.length() == 0)
                        return;

                    String erlaubteZeichen = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜß ()-";
                    for (int i=0; i<text.length();i++) 
                    {
                        if (erlaubteZeichen.indexOf(text.charAt(i)) == -1) 
                        {
                            java.awt.Toolkit.getDefaultToolkit().beep(); // :D
                            return;
                        }
                    }

                    if (getLength() + text.length() < 35)
                        super.insertString(offset, text, a);

                    CheckOK();
                }

                @Override
                public void remove(int offs, int len) throws BadLocationException 
                {
                    super.remove(offs, len);

                    CheckOK();
                }
            });            
        content.add(name_dat);

        JLabel Kuerzel = new JLabel("Kürzel des ESTWs:");
        Kuerzel.setLocation(15,40);
        Kuerzel.setVisible(true);
        Kuerzel.setSize(110,20);
        content.add(Kuerzel);

        kuerzel_dat = new JTextField();
        kuerzel_dat.setLocation(130,40);
        kuerzel_dat.setVisible(true);
        kuerzel_dat.setSize(50,21);
        kuerzel_dat.setDocument(new PlainDocument() 
            {
                @Override
                public void insertString(int offset, String text, AttributeSet a) throws BadLocationException 
                {
                    if (text.length() == 0)
                        return;

                    String erlaubteZeichen = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    for (int i=0; i<text.length();i++) 
                    {
                        if (erlaubteZeichen.indexOf(text.charAt(i)) == -1) 
                        {
                            java.awt.Toolkit.getDefaultToolkit().beep(); // :D
                            return;
                        }
                    }

                    if (getLength() + text.length() < 6)
                        super.insertString(offset, text, a);

                    CheckOK();
                }

                @Override
                public void remove(int offs, int len) throws BadLocationException 
                {
                    super.remove(offs, len);

                    CheckOK();
                }
            });            
        kuerzel_dat.setText(kuerzel);
        content.add(kuerzel_dat);

        ok = new JButton("OK");
        ok.setLocation(15,310);
        ok.setVisible(true);
        ok.setSize(100,20);
        ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    if (name_dat.getText().length() == 0 || kuerzel_dat.getText().length() == 0)
                        return;

                    name = name_dat.getText();
                    kuerzel = kuerzel_dat.getText();

                    estw_daten.setVisible(false);
                    Laden();
                    Editieren();
                }
            });
        content.add(ok);

        JButton abbrechen = new JButton("Abbrechen");
        abbrechen.setLocation(130,310);
        abbrechen.setVisible(true);
        abbrechen.setSize(100,20);
        abbrechen.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    estw_daten.setVisible(false);
                }
            });
        content.add(abbrechen);

        lupen_daten = new JDialog(this, "Eigenschaften der Lupe");
        lupen_daten.setSize(300,120);
        lupen_daten.setLocation(getWidth()/2 + getX(), getHeight()/2 + getY());
        lupen_daten.setVisible(false);
        lupen_daten.setBackground(Color.LIGHT_GRAY);
        lupen_daten.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        lupen_daten.setAlwaysOnTop(true);
        lupen_daten.setModal(true);

        content = new JPanel();
        lupen_daten.add(content);
        content.setLayout(null);
        content.setBackground(Color.LIGHT_GRAY);

        Name = new JLabel("Name der Lupe:");
        Name.setLocation(15,10);
        Name.setVisible(true);
        Name.setSize(110,20);
        content.add(Name);

        lupe_dat = new JTextField();
        lupe_dat.setLocation(130,10);
        lupe_dat.setVisible(true);
        lupe_dat.setSize(150,21);
        lupe_dat.setDocument(new PlainDocument() 
            {
                @Override
                public void insertString(int offset, String text, AttributeSet a) throws BadLocationException 
                {
                    if (text.length() == 0)
                        return;

                    String erlaubteZeichen = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
                    for (int i=0; i<text.length();i++) 
                    {
                        if (erlaubteZeichen.indexOf(text.charAt(i)) == -1) 
                        {
                            java.awt.Toolkit.getDefaultToolkit().beep(); // :D
                            return;
                        }
                    }

                    if (getLength() + text.length() < 7)
                        super.insertString(offset, text, a);

                    CheckOK();
                }

                @Override
                public void remove(int offs, int len) throws BadLocationException 
                {
                    super.remove(offs, len);

                    CheckOK();
                }
            }); 
        content.add(lupe_dat);

        lupe_ok = new JButton("OK");
        lupe_ok.setLocation(15,50);
        lupe_ok.setVisible(true);
        lupe_ok.setSize(100,20);
        lupe_ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    if (lupe_dat.getText().length() == 0 )
                        return;
                        
                    for (int i = 0; i < lupen.size(); i++)
                    {
                        if (lupe_dat.getText().equals(lupen.get(i).holeName()))
                        {
                            Object[] options = { "Ok"};
                            JOptionPane.showOptionDialog(null, "Dieser Name ist bereits vergeben!", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
                            return;
                        }
                    }
                    
                    if (!editLupe)
                    {
                        LUPE lup = new LUPE(lupe_dat.getText());
                        lupen.add(lup);
                        selectLupe(lup);
                    }
                    else
                    {
                        selectedLupe.setzeName(lupe_dat.getText());
                        selectLupe(selectedLupe);
                    }

                    lupen_daten.setVisible(false);
                    Editieren();
                }
            });
        content.add(lupe_ok);
        CheckOK();

        abbrechen = new JButton("Abbrechen");
        abbrechen.setLocation(130,50);
        abbrechen.setVisible(true);
        abbrechen.setSize(100,20);
        abbrechen.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    lupen_daten.setVisible(false);
                }
            });
        content.add(abbrechen);

        this.lupen = new ArrayList<LUPE>();
        selectedLupe = null;

        CheckOK();
    }
 
Ups, da hat er wohl den 2. Post nicht genommen 🙁
Nachtrag:
Java:
    private void Laden()
    {
        geladen = true;

        Speichern_unter.setEnabled(true); 
        Eigenschaften.setEnabled(true);  
        LupeUmbenennen.setEnabled(true); 
        LupeLoeschen.setEnabled(true);   
        Lupe_einfuegen.setEnabled(true);     
    }

    private void Editieren()
    {
        editiert = true;

        Speichern.setEnabled(true);
    }

    private void Oeffnen()
    {
        JFileChooser chooser = new JFileChooser("Öffnen");
        chooser.setDialogType(JFileChooser.OPEN_DIALOG);
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        File file = new File("/home");

        chooser.setCurrentDirectory(file);

        FileNameExtensionFilter plainFilter = new FileNameExtensionFilter("ESTW-Projekt: estwpr", "estwpr");
        chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter());
        chooser.setFileFilter(plainFilter);

        chooser.addPropertyChangeListener(new PropertyChangeListener() 
            {
                public void propertyChange(PropertyChangeEvent e) 
                {
                    if (e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
                    || e.getPropertyName().equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) 
                    {
                        final File f = (File) e.getNewValue();
                    }
                }
            });

        chooser.setVisible(true);
        int result = chooser.showOpenDialog(null);

        if (result == JFileChooser.APPROVE_OPTION) 
        {
            File inputVerzFile = chooser.getSelectedFile();
            Datei = inputVerzFile.getPath();
            if (!plainFilter.accept(inputVerzFile))
            {
                chooser.setVisible(false); 
                return;
            }
        }
        chooser.setVisible(false); 

        // Öffnen der Datei:
        String[] fin_dat = new String[0];
        String dat = null;
        try 
        {
            File datei = new File(Datei);
            InputStream fileIn = new FileInputStream(datei);

            byte[] buffer = new byte[1024];
            int anzahlZeichen = 0;
            String uebertrag = "";
            while (true)
            {
                anzahlZeichen = fileIn.read(buffer);
                if (anzahlZeichen < 0)
                    break;
                dat = uebertrag + new String(buffer, 0, anzahlZeichen, "ISO-8859-1");

                // Bearbeiten:
                dat = dat.replace("\n", "");
                dat = dat.replace("\r", "");
                dat = dat.replace("!", "");
                dat = dat.replace("/", "");

                // Verarbeiten:                
                String[] eintraege = dat.split("~");

                int length = eintraege.length;
                if (anzahlZeichen == 1024)
                    length--;

                String[] temp = new String[fin_dat.length + length];
                for (int i = 0; i < fin_dat.length; i++)
                {
                    temp[i] = fin_dat[i];
                }

                for (int i = 0; i < length; i++)
                {
                    temp[i + fin_dat.length] = eintraege[i];
                }
                fin_dat = temp;
                uebertrag = eintraege[eintraege.length - 1];
            }
            fileIn.close();
        } 
        catch( IOException e ) 
        {
            return;
        }

        try
        {
            int i = 0;
            name = fin_dat[i];
            i++;
            kuerzel = fin_dat[i];
            i++;
            
            while(i < fin_dat.length)
            {
                if (fin_dat[i].equals("LUPE"))
                {
                    i++;
                    lupen.add(new LUPE(fin_dat[i]));
                    i++;
                    ArrayList<String> arr = new ArrayList<String>();
                    while(!fin_dat[i].equals("ENDE LUPE"))
                    {
                        arr.add(fin_dat[i]);
                        i++;
                    }
                    i++;
                    lupen.get(lupen.size() - 1).fromSpeicherstring(arr.toArray(new String[0]));
                    selectLupe(lupen.get(lupen.size() - 1));
                    updateLupenscroller();
                }
                //else if ()
                //{
                //    i++;
                //}
            }

            Laden();
        }
        catch( IndexOutOfBoundsException e )
        {
            return;
        }

        setTitle("ESTW-Developer - " + Datei);
    }

    private void Speichern()
    {
        if (Datei.length() > 0)
        {
            String speicherstring = name + "~" + kuerzel + "~"; 
            
            // Lupen speichern:
            for (int i = 0; i < lupen.size(); i++)
            {
                speicherstring += lupen.get(i).toSpeicherstring();
            }
           
            try
            {
                BufferedWriter datei = new BufferedWriter(
                        new OutputStreamWriter(
                            new FileOutputStream( Datei ) ) );
                datei.write( speicherstring, 0, speicherstring.length() );          
                datei.newLine();
                datei.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            editiert = false;
            Speichern.setEnabled(false);

            setTitle("ESTW-Developer - " + Datei);
        }
        else
            Speichern_unter();
    }

    private void Speichern_unter()
    {
        JFileChooser chooser;

        if (Datei == null)
            Datei = System.getProperty("user.home");
        File file = new File(Datei.trim());

        chooser = new JFileChooser(Datei);
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        FileNameExtensionFilter plainFilter = new FileNameExtensionFilter("ESTW-Projekt: estwpr", "estwpr");
        chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter());
        chooser.setFileFilter(plainFilter);
        chooser.setDialogTitle("Speichern unter...");
        chooser.setVisible(true);

        int result = chooser.showSaveDialog(this);

        if (result == JFileChooser.APPROVE_OPTION) 
        {

            Datei = chooser.getSelectedFile().toString() + ".estwpr";
            file = new File(Datei);
            if (plainFilter.accept(file))
                Speichern();

            chooser.setVisible(false);
        }
        chooser.setVisible(false);
    }

    public void ESTW_Daten()
    {
        name_dat.setText(name);
        kuerzel_dat.setText(kuerzel);

        estw_daten.setVisible(true);
    }

    private void CheckOK()
    {
        ok.setEnabled(name_dat.getText().length() != 0 && kuerzel_dat.getText().length() != 0);
        lupe_ok.setEnabled(lupe_dat.getText().length() != 0);
    }

    private void EditLupe()
    {
        lupen_daten.setVisible(true);
    }

    private void updateLupenscroller()
    {                
        lupenpanel = new JPanel();
        lupenpanel.setSize(100 * lupen.size() + 10, 70);   
        GridBagConstraints gbc = new GridBagConstraints();
        lupenpanel.setLayout(new GridBagLayout());
        lupenpanel.setVisible(true);
        lupenpanel.setLocation(0,0);

        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, 10, 0, 10);
        gbc.gridy = 0;
        gbc.gridx = 0;  
        gbc.ipadx = 80;    
        gbc.ipady = 60;   
        
        for (int i = 0; i < lupen.size(); i++)
        {
            transLupe = lupen.get(i);

            JLabel lupenLabel = new JLabel("L_" + lupen.get(i).holeName());
            lupenLabel.setForeground(Color.WHITE);
            lupenLabel.setBackground(Color.BLACK);
            lupenLabel.setOpaque(true);
            lupenLabel.setSize(80, 54);
            lupenLabel.setLocation(100 * i + 5, 5);
            lupenLabel.setVisible(true);
            lupenLabel.setHorizontalAlignment(SwingConstants.CENTER);
            lupenLabel.addMouseListener(new MouseAdapter()
                {
                    LUPE lupe = transLupe;
                    @Override
                    public void mouseClicked(MouseEvent e)
                    {
                        selectLupe(lupe);
                    }
                });
            lupenpanel.add(lupenLabel, gbc);

            if (selectedLupe != null)
            {
                if (lupen.get(i).holeName().equals(selectedLupe.holeName()))
                    lupenLabel.setBorder(new LineBorder(Color.BLUE, 3));
            }
            
            MausiLupenScroller mausi = new MausiLupenScroller(i);
            lupenLabel.addMouseListener(mausi);
            lupenLabel.addMouseMotionListener(mausi);

            gbc.gridx++;
        }

        contentPanel.remove(lupenscroller);
        if (lupen.size() == 0)
        {
            lupenname.setVisible(false);           
            return;
        }
        lupenname.setVisible(true);
        lupenscroller = new JScrollPane(lupenpanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        lupenscroller.setSize(1030, 85);
        lupenscroller.setLocation(2, 650);
        lupenscroller.setVisible(false);
        lupenscroller.setVisible(lupen.size() > 0);   
        contentPanel.add(lupenscroller);
    }

    public void selectLupe(LUPE lup)
    {
        if (selectedLupe != null)
            contentPanel.remove(selectedLupe);

        selectedLupe = lup;

        if (selectedLupe != null)
        {
            contentPanel.add(selectedLupe, JLayeredPane.DEFAULT_LAYER);
            lupenname.setText("L_" + selectedLupe.holeName());
        }

        updateLupenscroller();
    }
}
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

public class LUPE extends JLayeredPane
{    
    private String name;
    
    public LUPE(String name)
    {
        this.name = name;
        
        setSize(1020,528);
        setLocation(7,60);
        setVisible(true);
        
        setForeground(Color.BLACK);
        
        Canvas line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,05);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,45);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,85);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,125);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,165);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,205);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,245);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,285);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,325);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,365);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,405);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,445);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,485);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        line = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.DARK_GRAY);
                g.drawLine(0,0,1024,0);
            }
        };
        line.setLocation(0,525);
        line.setSize(1024,1);
        line.setVisible(true);
        add(line);
        
        Canvas black = new Canvas()
        {
            public void paint(Graphics g)
            {
                g.setColor(Color.BLACK);
                g.fillRect(0,0,1020,528);
            }
        };
        black.setLocation(0,0);
        black.setSize(1020,528);
        black.setVisible(true);
        add(black, JLayeredPane.DEFAULT_LAYER);
        
        validate();
    }
    
    public void setzeName(String name)
    {
        this.name = name;
    }
    
    public String holeName()
    {
        return name;
    }
    
    public String toSpeicherstring()
    {
        return "LUPE" + "~" + this.name + "~" + "ENDE LUPE" + "~";
    }
    
    public void fromSpeicherstring(String[] arr)
    {
    }
}

Also wird die Paint-Methode nicht überschrieben 😉
 
ich weiss garnicht so recht, wo ich anfangen soll....

es gibt so vieles was ich anmerken möchte, aber ich denke das würde die Genfer Konventionen verletzen...

daher habe ich eine schnelle einfache Lösung für dich, ersetze deine Klasse LUPE durch diese klasse "Lupe", dann sollte es funktionieren.

Java:
import java.awt.*;
import javax.swing.*;
 
public class Lupe extends JLayeredPane
{    
    private String name;
    
    public void setzeName(String name)
    {
        this.name = name;
    }
    
    public String holeName()
    {
        return name;
    }
    
    public String toSpeicherstring()
    {
        return "LUPE" + "~" + this.name + "~" + "ENDE LUPE" + "~";
    }
    
    public void fromSpeicherstring(String[] arr)
    {
    }
    
    public Lupe(String name)
    {
        this.name = name;
        setSize(1020,528);
        setLocation(7,60);
        setVisible(true);
    }
    
    @Override
    public void paint(Graphics g)
    {
    	final int[] LINEHEIGHTS = {5,45,85,125,165,205,245,285,325,365,405,445,485,525};
    	
    	super.paint(g);
    	// set background to "black"
    	g.setColor(Color.black);
    	g.fillRect(0, 0, this.getWidth(), this.getHeight());
    	
    	// draw darkGray lines
    	g.setColor(Color.DARK_GRAY);
    	for (int height : LINEHEIGHTS)
    	{
    		g.drawLine(0, height, this.getWidth(), height);
    	}
    }
}
 
Zuletzt bearbeitet:
Hallo!

Danke für die Antwort.

Das ist aber nur eine halbe Lösung.
In erster Linie ist das Problem weg, aber wenn ich andere Objekte (Bsp: JTextField) auf das JLayeredPane Lupe hinzufüge mit der add-Methode, dann verdeckt dieses Objekt das Menü wieder 🙁

Viele Grüße!
 

Zurück
Oben