gui fenster nur einmal aufrufen

Permafrost

Mitglied
hallo🙂
ich möchte das wenn ich auf erstellen gehe zum zweiten fenster komme.
beim ersten mal funktioniert alles
nur wenn ich dann noch mal aufs zweite fenster gehe möchte ich nicht dass es noch mal erstellt wird sonder möchte das alte benutzen können.
geht das? wenn ja wie?


Fentser nummer 1
Java:
public class Main {

    /**
     * @param args the command line arguments
     */

   private static JFrame oJFrame = new JFrame();
    private static JButton oJBstart  = new JButton("Hauptmenü");
    private static JButton oJBcreate = new JButton("Erstelle");
    private static JButton oJBview   = new JButton("Ansicht");
    private static JButton oJBload   = new JButton("Laden");
    private static JButton oJBsave   = new JButton("Speichern");
    private static JButton oJBopen   = new JButton("Öffnen");
    private static boolean mbRunning;
    static LinkedList<Animal> moZoo = new LinkedList<Animal>();
    static String[] maSort;
    static String moDatei = ("C:\\Users\\F.Schrader\\Downloads\\SpeicherAnimal.txt");
    static String msName = null, msNickname = null, msDate = null;

    public static void main(String[] args) {

        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        
               oJFrame.setTitle("Animals");
               oJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               oJFrame.setSize(500, 600);
               oJFrame.setResizable(false);

       JPanel oJPanel1 = new JPanel();
              oJPanel1.setLayout(new BorderLayout());

               JPanel oJPanel2 = new JPanel();

                      oJPanel2.setLayout(new BorderLayout());

                      JLabel oJLabel = new JLabel();
                             oJLabel.setIcon(new ImageIcon ("C:\\Users\\F.Schrader\\Pictures\\animals.jpg"));

                      oJPanel2.setPreferredSize(new Dimension(500, 200));
                      oJPanel2.add(oJLabel, BorderLayout.CENTER);

              oJPanel1.add(oJPanel2, BorderLayout.NORTH);

               JPanel oJPanel3 = new JPanel();
                      oJPanel3.setBackground(Color.WHITE);
                      oJPanel3.setLayout(new BoxLayout(oJPanel3, BoxLayout.Y_AXIS));

                      
                              oJBcreate.setMaximumSize(new Dimension(150, 40));
                              oJBcreate.addActionListener(new buttonAction());

                      oJPanel3.add(oJBcreate);

                      
                              oJBview.setMaximumSize(new Dimension(150, 40));
                              oJBview.addActionListener(new buttonAction());

                      oJPanel3.add(oJBview);

                     
                              oJBload.setMaximumSize(new Dimension(150, 40));
                              oJBload.addActionListener(new buttonAction());

                      oJPanel3.add(oJBload);

                      
                              oJBsave.setMaximumSize(new Dimension(150, 40));
                              oJBsave.addActionListener(new buttonAction());

                      oJPanel3.add(oJBsave);

                      
                              oJBopen.setMaximumSize(new Dimension(150, 40));
                              oJBopen.addActionListener(new buttonAction());

                      oJPanel3.add(oJBopen);
                      
              oJPanel1.add(oJPanel3, BorderLayout.CENTER);

               JPanel oJPanel4 = new JPanel();
                      oJPanel4.setBackground(Color.WHITE);
                      oJPanel4.setPreferredSize(new Dimension(180, 100));
                      
              oJPanel1.add(oJPanel4, BorderLayout.WEST);

               JPanel oJPanel5 = new JPanel();
                      oJPanel5.setBackground(Color.WHITE);
                      oJPanel5.setPreferredSize(new Dimension(170, 100));
                      
              oJPanel1.add(oJPanel5, BorderLayout.EAST);

               JPanel oJPanel6 = new JPanel();
                      oJPanel6.setBackground(Color.WHITE);
                      oJPanel6.setLayout(new BorderLayout());

                      
                              oJBstart.setPreferredSize(new Dimension(170, 50));
                              oJBstart.addActionListener(new buttonAction());

                      oJPanel6.add(oJBstart);
                     
              oJPanel1.add(oJPanel6, BorderLayout.SOUTH);

                      oJFrame.add(oJPanel1);
                      oJFrame.setVisible(true);
                      oJFrame.toFront();



    }
    private static class buttonAction implements ActionListener {


        public void actionPerformed(ActionEvent e) {

            
            if (e.getSource() == oJBcreate) {
                
                oJFrame.setVisible(false);
                Create oCreate = new Create();
                oCreate.create(mbRunning);
            }
            if (e.getSource() == oJBview)   {
                JOptionPane.showMessageDialog(null, "Schau dir alles an", "Ansehen", 1);
            }
            if (e.getSource() == oJBload)   {
                JOptionPane.showMessageDialog(null, "Laden", "Laden", 1);
            }
            if (e.getSource() == oJBsave)   {
                JOptionPane.showMessageDialog(null, "Speichern", "Speichern", 1);
            }
            if (e.getSource() == oJBopen)   {
                try {
                    Desktop oDesktop = Desktop.getDesktop();
                    oDesktop.open(new File(moDatei));
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
            if (e.getSource() == oJBstart)  {
                JOptionPane.showMessageDialog(null, "Sie sind schon im Hauptmenü", "Hauptmenü", 1);
            }
        }
    }
    public static void create(){
        oJFrame.toFront();
        oJFrame.setVisible(true);
        mbRunning = false;

    }

Fenster nummer 2

Java:
package animalgui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Create {
     
    private static JButton oJBstart     = new JButton("Hauptmenü");
    private static JButton oJBcat       = new JButton("Katze");
    private static JButton oJBdog       = new JButton("Hund");
    private static JButton oJBmouse     = new JButton("Maus");
    private static JButton oJBnewAnimal = new JButton("Neues Tier");
    private static JFrame  oJFrame1     = new JFrame();
    private static boolean mbRunning;

    public boolean create(boolean bRunning){
            bRunning = mbRunning;
        if (bRunning = false) {
            oJFrame1.setVisible(true);
            oJFrame1.toFront();
            
        

        }
        else{
            try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }


               oJFrame1.setTitle("Animals");
               oJFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               oJFrame1.setSize(500, 600);
               oJFrame1.setResizable(false);

       JPanel oJPanel1 = new JPanel();
              oJPanel1.setLayout(new BorderLayout());

               JPanel oJPanel2 = new JPanel();

                      oJPanel2.setLayout(new BorderLayout());

                      JLabel oJLabel = new JLabel();
                             oJLabel.setIcon(new ImageIcon ("C:\\Users\\F.Schrader\\Pictures\\animals.jpg"));

                      oJPanel2.setPreferredSize(new Dimension(500, 200));
                      oJPanel2.add(oJLabel, BorderLayout.CENTER);

              oJPanel1.add(oJPanel2, BorderLayout.NORTH);

               JPanel oJPanel3 = new JPanel();
                      oJPanel3.setBackground(Color.WHITE);
                      oJPanel3.setLayout(new BoxLayout(oJPanel3, BoxLayout.Y_AXIS));

                            JLabel oJLabel1 = new JLabel("Bitte suchen Sie sich ein Tier aus");
                                   oJLabel1.setFont( new Font( "Comic Sans MS", Font.BOLD, 10 ) );
                      oJPanel3.add(oJLabel1);
                      JLabel oJLabel2 = new JLabel("            ");
                                   oJLabel2.setFont( new Font( "Comic Sans MS", Font.BOLD, 10 ) );
                      oJPanel3.add(oJLabel2);


                              oJBcat.setMaximumSize(new Dimension(170, 40));
                              oJBcat.addActionListener(new buttonAction());

                      oJPanel3.add(oJBcat);


                              oJBdog.setMaximumSize(new Dimension(170, 40));
                              oJBdog.addActionListener(new buttonAction());

                      oJPanel3.add(oJBdog);


                              oJBmouse.setMaximumSize(new Dimension(170, 40));
                              oJBmouse.addActionListener(new buttonAction());

                      oJPanel3.add(oJBmouse);


                              oJBnewAnimal.setMaximumSize(new Dimension(170, 40));
                              oJBnewAnimal.addActionListener(new buttonAction());

                      oJPanel3.add(oJBnewAnimal);


              oJPanel1.add(oJPanel3, BorderLayout.CENTER);

               JPanel oJPanel4 = new JPanel();
                      oJPanel4.setBackground(Color.WHITE);
                      oJPanel4.setPreferredSize(new Dimension(160, 100));

              oJPanel1.add(oJPanel4, BorderLayout.WEST);

               JPanel oJPanel5 = new JPanel();
                      oJPanel5.setBackground(Color.WHITE);
                      oJPanel5.setPreferredSize(new Dimension(140, 100));

              oJPanel1.add(oJPanel5, BorderLayout.EAST);

               JPanel oJPanel6 = new JPanel();
                      oJPanel6.setBackground(Color.WHITE);
                      oJPanel6.setLayout(new BorderLayout());


                              oJBstart.setPreferredSize(new Dimension(170, 50));
                              oJBstart.addActionListener(new buttonAction());

                      oJPanel6.add(oJBstart);

              oJPanel1.add(oJPanel6, BorderLayout.SOUTH);

                      oJFrame1.add(oJPanel1);
                      oJFrame1.setVisible(true);
                      oJFrame1.toFront();

                      mbRunning = false;

        }
        return false;
    }
    private static class buttonAction implements ActionListener {


        public void actionPerformed(ActionEvent e) {


            if (e.getSource() == oJBcat) {
                JOptionPane.showMessageDialog(null, "Das ist der Bereich zum Erstellen", "Erstellen", 1);
            }
            if (e.getSource() == oJBdog)   {
                JOptionPane.showMessageDialog(null, "Schau dir alles an", "Ansehen", 1);
            }
            if (e.getSource() == oJBmouse)   {
                JOptionPane.showMessageDialog(null, "Laden", "Laden", 1);
            }
            if (e.getSource() == oJBnewAnimal)   {
                JOptionPane.showMessageDialog(null, "Speichern", "Speichern", 1);
            }
            
            if (e.getSource() == oJBstart)  {
                oJFrame1.setVisible(false);
                oJFrame1.toBack(); 
                Main.create();
                mbRunning = false;

            }
        }
    }

}
 
Zuletzt bearbeitet:
Ohne jetzt alles ruchgelesen zu haben, mach dir doch einfach eine static boolean bRunning, die dir anzeigt, ob das zweite Fenster geöffnet wurde oder nicht. Ist dem nicht so, muss es neu erstellt werden...
 
grundsätzlich mit ner referenz arbeit und dann auf isVisible() prüfen ...
wenn true kommt einfach das fenster in den vordergrund holen ...

! achtung ! : NPE vorher checken
 
Sehr undurchsichtig dein Code, versuchs doch erstmal in nem kleineren Rahmen:

Java:
public class FirstFrame extends JFrame {

	private JFrame secondFrame;

	public FirstFrame() {
		setTitle("First frame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JButton button = new JButton("show second Frame");
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (secondFrame == null) {
					secondFrame = new SecondFrame();
				}

				secondFrame.setVisible(true);
			}
		});

		add(button);
		pack();
	}

	public static void main(String[] args) {
		new FirstFrame().setVisible(true);
	}
}

Java:
public class SecondFrame extends JFrame {
	public SecondFrame() {
		setTitle("second frame");
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		setSize(new Dimension(100,100));
	}
}
 

Zurück
Oben