Java Fenster mit button schliessen?

CaptainMimbl

Newbie
Registriert
Nov. 2014
Beiträge
2
Hallo Liebe Community, ich bin neu hier und habe begonnen mit Java zu programmieren mein bisheriger Code sieht so aus:
Code:
import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;


    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class frame extends JFrame implements ActionListener {
    	
    private JButton play;
    private JButton options;
    private JButton close;
    	
    	
	public static void main(String[] args){
		
		frame frame = new frame ("GUI");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400,400);
		
		frame.setLayout(null);
		frame.setVisible(true);
	
		
	}
    	
    	public frame (String title){
    		super(title);
    		
    		play = new JButton ("Play");
    		play.setBounds(120, 60, 160, 40);
    		play.addActionListener(this);
    		add(play);
    		
    		options = new JButton ("Options");
    		options.setBounds(120, 150, 160, 40);
    		options.addActionListener(this);
    		add(options);
    		
    		close = new JButton ("Close");
    		close.setBounds(120, 240, 160, 40);
    		close.addActionListener(this);
    		add(close);
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		if (e.getSource()== play){
    			worldeditor();
    		}
    		if (e.getSource()== options){
    			optionsf();
    		}
    		if (e.getSource()== close){
    			System.exit(0);
    		}
    	}
    	public static void worldeditor(){
        	
    		JFrame worldeditor = new JFrame ("World Editor");
    		worldeditor.setSize(500, 500);
    		worldeditor.setVisible(true);
			
		}
    	
    	public static void optionsf() {
    		
    		JFrame optionsf = new JFrame ("Options");
    		optionsf.setSize(500, 500);
    		optionsf.setVisible(true);
		}

    	}
es gibt keine Fehlermeldungen ich wollte einfach wissen was ich machen muss damit ich einen button in dem Fenster worldeditor und optionsf machen kann. Dann wie kann ich machen das wenn ich das Fenster worldeditor öffne das dann das Fenster frame sich schliess und wenn ich dann zurück gehe dass sich das Fenster frame wieder öffnet.

MFG CM
 
Du brauchst noch einen ActionListener auf dem Button.
In der Action Methode sollte dann was stehen von optionsf.dispose();
 
Ich hoffe ich habe deine Frage richtig verstanden:

Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Frame extends JFrame implements ActionListener {
  private final JButton play;
  private final JButton options;
  private final JButton close;

  public static void main(String[] args) {
    Frame frame = new Frame("GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setLayout(null);
    frame.setVisible(true);
  }

  public Frame(String title) {
    super(title);
    this.play = new JButton("Play");
    this.play.setBounds(120, 60, 160, 40);
    this.play.addActionListener(this);
    add(this.play);
    this.options = new JButton("Options");
    this.options.setBounds(120, 150, 160, 40);
    this.options.addActionListener(this);
    add(this.options);
    this.close = new JButton("Close");
    this.close.setBounds(120, 240, 160, 40);
    this.close.addActionListener(this);
    add(this.close);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.play) {
      worldeditor(this);
    }
    if (e.getSource() == this.options) {
      optionsf(this);
    }
    if (e.getSource() == this.close) {
      System.exit(0);
    }
  }

  public static void worldeditor(final Frame frame) {
    JFrame worldeditor = new JFrame("World Editor");
    worldeditor.setSize(500, 500);
    worldeditor.setVisible(true);

    worldeditor.addWindowListener(new WindowListener() {

      @Override
      public void windowOpened(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowIconified(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowDeiconified(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowDeactivated(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowClosing(WindowEvent e) {
        frame.setVisible(true);

      }

      @Override
      public void windowClosed(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowActivated(WindowEvent e) {
        // TODO Auto-generated method stub

      }
    });

    frame.setVisible(false);

  }

  public static void optionsf(final Frame frame) {
    JFrame optionsf = new JFrame("Options");
    optionsf.setSize(500, 500);
    optionsf.setVisible(true);

    optionsf.addWindowListener(new WindowListener() {

      @Override
      public void windowOpened(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowIconified(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowDeiconified(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowDeactivated(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowClosing(WindowEvent e) {
        frame.setVisible(true);

      }

      @Override
      public void windowClosed(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowActivated(WindowEvent e) {
        // TODO Auto-generated method stub

      }
    });

    frame.setVisible(false);

  }

}
 
Villt solltest du auch einfach mit JDialog arbeiten und die Frames nur einmal instanzieren und nicht jedes mal wieder bei Button Press.
 
Danke dir gozza das war es was ich suchte doch ich habe immer noch eine frage und zwar wie kann ich einen Button im Fenster worldeditor machen wenn ich es gleich wie Fenster frame mache geht es irgendwie nicht weiss auch nicht mher was es wahr.
 
Ich verstehe zwar immer die Frage noch nicht ganz, aber probiere das:

Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Frame extends JFrame {

  public static void main(String[] args) {
    Frame frame = new Frame("GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setLayout(null);
    frame.setVisible(true);
  }

  public Frame(String title) {
    super(title);

    final JButton play = new JButton("Play");
    final JButton options = new JButton("Options");
    final JButton close = new JButton("Close");

    ActionListener al = new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == play) {
          worldeditor(Frame.this);
        }
        if (e.getSource() == options) {
          optionsf(Frame.this);
        }
        if (e.getSource() == close) {
          System.exit(0);
        }

      }
    };

    play.setBounds(120, 60, 160, 40);
    play.addActionListener(al);
    add(play);

    options.setBounds(120, 150, 160, 40);
    options.addActionListener(al);
    add(options);

    close.setBounds(120, 240, 160, 40);

    close.addActionListener(al);
    add(close);
  }

  //  @Override
  //  public void actionPerformed(ActionEvent e) {
  //    if (e.getSource() == play) {
  //      worldeditor(this);
  //    }
  //    if (e.getSource() == options) {
  //      optionsf(this);
  //    }
  //    if (e.getSource() == close) {
  //      System.exit(0);
  //    }
  //  }

  public static void worldeditor(final JFrame worldeditor2) {
    final JFrame worldeditor = new JFrame("World Editor");
    worldeditor.setSize(500, 500);
    worldeditor.setLayout(null);

    final JButton play = new JButton("Play");
    final JButton options = new JButton("Options");
    final JButton close = new JButton("Close");

    ActionListener al = new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == play) {
          worldeditor(worldeditor);
        }
        if (e.getSource() == options) {
          optionsf(worldeditor);
        }
        if (e.getSource() == close) {
          System.exit(0);
        }

      }
    };

    play.setBounds(120, 60, 160, 40);
    play.addActionListener(al);
    worldeditor.add(play);

    options.setBounds(120, 150, 160, 40);
    options.addActionListener(al);
    worldeditor.add(options);

    close.setBounds(120, 240, 160, 40);

    close.addActionListener(al);
    worldeditor.add(close);

    worldeditor.setVisible(true);

    worldeditor.addWindowListener(new WindowListener() {

      @Override
      public void windowOpened(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowIconified(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowDeiconified(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowDeactivated(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowClosing(WindowEvent e) {
        worldeditor2.setVisible(true);

      }

      @Override
      public void windowClosed(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowActivated(WindowEvent e) {
        // TODO Auto-generated method stub

      }
    });

    worldeditor2.setVisible(false);

  }

  public static void optionsf(final JFrame worldeditor) {
    JFrame optionsf = new JFrame("Options");
    optionsf.setSize(500, 500);
    optionsf.setVisible(true);
    optionsf.addWindowListener(new WindowListener() {

      @Override
      public void windowOpened(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowIconified(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowDeiconified(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowDeactivated(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowClosing(WindowEvent e) {
        worldeditor.setVisible(true);

      }

      @Override
      public void windowClosed(WindowEvent e) {
        // TODO Auto-generated method stub

      }

      @Override
      public void windowActivated(WindowEvent e) {
        // TODO Auto-generated method stub

      }
    });

    worldeditor.setVisible(false);

  }

}
 
Zurück
Oben