# Neues Dialog Mittig auf dem Bildschirm



## Durchstarter (21. Apr 2015)

Ich habe hier diesen Code mit dem ich völlig zufrieden bin (Button 3 & 4 haben derzeit keine Funktion aus Testgründen) Wenn ich jetzt auf den Button(Fenster) 1 oder 2 klicke öffnet sich das neue Fenster wie gewünscht, allerdings hätte ich gerne das sich dieses in der Mitte meines Bildschirms öffnet, danke im Vorraus falls jemand helfen könnte 

Ich probiere weiter rum, falls ich was finde schreibe ich das hier natürlich, freue mich aber über Lösungsvorschläge 


```
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
public class Fensterlernen {
  
  public Fensterlernen() {
    JFrame frame = new JFrame("Fenster");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setResizable(false);
    
    JPanel mainPanel = new JPanel();
    
    JButton openFirstDialogBtn = new JButton("Fenster 1");
    JButton openSecondDialogBtn = new JButton("Fenster 2");
    JButton openThirdDialogBtn = new JButton("Fenster 3");
    JButton openFourthDialogBtn = new JButton("Fenster 4");
    
    openFirstDialogBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        createDialog("Fenster 1");
      }
    });
    openSecondDialogBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        createDialog("Fenster 2");
      }
    });
    
    openFirstDialogBtn.setPreferredSize(new Dimension(100, 25));
    openSecondDialogBtn.setPreferredSize(new Dimension(100, 25));
    openThirdDialogBtn.setPreferredSize(new Dimension(100, 25));
    openFourthDialogBtn.setPreferredSize(new Dimension(100, 25));
    
    mainPanel.add(openFirstDialogBtn);
    mainPanel.add(openSecondDialogBtn);
    mainPanel.add(openThirdDialogBtn);
    mainPanel.add(openFourthDialogBtn);
    
    frame.add(mainPanel);
    
    frame.pack();
    frame.setVisible(true);
  }
  
  private void createDialog(String title) {
    JDialog dialog = new JDialog();
    dialog.setTitle(title);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setSize(200, 80);
    
    JPanel mainPanel = new JPanel();
    
    JButton exitBtn = new JButton("Exit");
    exitBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        dialog.dispose();
        
      }
    });
    mainPanel.add(exitBtn);
    
    dialog.add(mainPanel);
    dialog.setVisible(true);
    
  }
  
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new Fensterlernen();
      }
    });
  }
}
```


----------



## Thallius (21. Apr 2015)

PRobiers mal mit


```
frame.setLocationRelativeTo(null);
```

Gruß

Claus


----------



## Durchstarter (21. Apr 2015)

Danke für die Schnelle antwort @Thallius! (Claus) Allerdings ist es nicht völlig "MITTIG" nur die Obere Linke Ecke des Fensters ist in der Mitte des Monitors.

Ich habe hier eine möglichkeit das erste Fenster Komplett Mittig zu platzieren:


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

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 21.04.2015
  * @author 
  */

public class Mitte extends JFrame {
  // Anfang Attribute
  // Ende Attribute
  
  public Mitte(String title) { 
    // Frame-Initialisierung
    super(title);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 300; 
    int frameHeight = 300;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    setResizable(false);
    Container cp = getContentPane();
    cp.setLayout(null);
    // Anfang Komponenten
    
    // Ende Komponenten
    
    setVisible(true);
  } // end of public Mitte
  
  // Anfang Methoden
  // Ende Methoden
  
  public static void main(String[] args) {
    new Mitte("Mitte");
  } // end of main
  
} // end of class Mitte
```
 allerdings schaffe ich das nicht auf ein neu geöffnetes Fenster zu übertragen


----------



## Thallius (21. Apr 2015)

Du darfst den SetPosition erst nach dem pack() aufrufen. Dann passt es.

Gruß

Claus


----------



## Durchstarter (21. Apr 2015)

danke!! 

edit:habs gerade versucht klappt doch nicht :lol:


edit: klappt doch :rtfm: (FÜR FENSTER 1 NUR WEIL ICH GERADE AM TÜFTELN BIN) allerdings öffnet sich das Hauptfenster immernochnicht mittig 

[Java]import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Fensterlernen {

  public Fensterlernen() {
    JFrame frame = new JFrame("Fenster");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setResizable(false);

    JPanel mainPanel = new JPanel();

    JButton openFirstDialogBtn = new JButton("Fenster 1");
    JButton openSecondDialogBtn = new JButton("Fenster 2");
    JButton openThirdDialogBtn = new JButton("Fenster 3");
    JButton openFourthDialogBtn = new JButton("Fenster 4");

    openFirstDialogBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        createDialog("Fenster 1");
      }
    });
    openSecondDialogBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        createDialog("Fenster 2");
      }
    });

    openFirstDialogBtn.setPreferredSize(new Dimension(100, 25));
    openSecondDialogBtn.setPreferredSize(new Dimension(100, 25));
    openThirdDialogBtn.setPreferredSize(new Dimension(100, 25));
    openFourthDialogBtn.setPreferredSize(new Dimension(100, 25));

    mainPanel.add(openFirstDialogBtn);
    mainPanel.add(openSecondDialogBtn);
    mainPanel.add(openThirdDialogBtn);
    mainPanel.add(openFourthDialogBtn);

    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
    int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
    frame.setLocation(x, y);


    frame.add(mainPanel);

    frame.pack();
    frame.setVisible(true);
  }

  private void createDialog(String title) {
    JDialog dialog = new JDialog();
    dialog.setTitle(title);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setSize(200, 80);

    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((dimension.getWidth() - dialog.getWidth()) / 2);
    int y = (int) ((dimension.getHeight() - dialog.getHeight()) / 2);
    dialog.setLocation(x, y);


    JPanel mainPanel = new JPanel();

    JButton exitBtn = new JButton("Exit");
    exitBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        dialog.dispose();

      }
    });
    mainPanel.add(exitBtn);

    dialog.add(mainPanel);
    dialog.setVisible(true);

  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new Fensterlernen();
      }
    });
  }
}[/Java]


EDIT: habs hinter pack() geklappt FUNKTIONIERT DANKEE


```
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
public class Fensterlernen {
  
  public Fensterlernen() {
    JFrame frame = new JFrame("Fenster");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setResizable(false);
    
    JPanel mainPanel = new JPanel();
    
    JButton openFirstDialogBtn = new JButton("Fenster 1");
    JButton openSecondDialogBtn = new JButton("Fenster 2");
    JButton openThirdDialogBtn = new JButton("Fenster 3");
    JButton openFourthDialogBtn = new JButton("Fenster 4");
    
    openFirstDialogBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        createDialog("Fenster 1");
      }
    });
    openSecondDialogBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        createDialog("Fenster 2");
      }
    });
    
    openFirstDialogBtn.setPreferredSize(new Dimension(100, 25));
    openSecondDialogBtn.setPreferredSize(new Dimension(100, 25));
    openThirdDialogBtn.setPreferredSize(new Dimension(100, 25));
    openFourthDialogBtn.setPreferredSize(new Dimension(100, 25));
    
    mainPanel.add(openFirstDialogBtn);
    mainPanel.add(openSecondDialogBtn);
    mainPanel.add(openThirdDialogBtn);
    mainPanel.add(openFourthDialogBtn);
    
    
    
    
    frame.add(mainPanel);
    
    frame.pack();
    frame.setVisible(true);
    
    
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
    int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
    frame.setLocation(x, y);
  }
  
  private void createDialog(String title) {
    JDialog dialog = new JDialog();
    dialog.setTitle(title);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setSize(200, 80);
    
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((dimension.getWidth() - dialog.getWidth()) / 2);
    int y = (int) ((dimension.getHeight() - dialog.getHeight()) / 2);
    dialog.setLocation(x, y);
    
    
    JPanel mainPanel = new JPanel();
    
    JButton exitBtn = new JButton("Exit");
    exitBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        dialog.dispose();
        
      }
    });
    mainPanel.add(exitBtn);
    
    dialog.add(mainPanel);
    dialog.setVisible(true);
    
  }
  
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new Fensterlernen();
      }
    });
  }
}
```


----------

