0

I am currently having a problem with my Frame. I am trying to add a panel into a Frame, but instead it opens two different windows, how can I fix that? I want the button and panel to be in the frame.

public class MenuSample extends JFrame{

Panel and Button that open up in a different window

 private JButton button;
 private JPanel panel;

   public MenuSample () {
     Frame testFrame = new Frame("Test Frame");
     testFrame.addWindowListener(new WindowAdapter () {
         public void windowClosing (final WindowEvent e)
         {
            System.exit(0);
         }
     });
     testFrame.setMenuBar(this.getMenubar());
     testFrame.setSize(500,300);
     testFrame.setLocation(400,300);
     testFrame.setVisible(true);

Panel and Button that are supposed to be in the Frame

     panel = new JPanel(new GridLayout(1,1));
     button = new JButton("erster Button");

     panel.add(button);

     getContentPane().add(panel);

     pack();
     setVisible(true);

  }

  protected MenuBar getMenubar () {
    some irrelevant MenuBar stuff

    menuLeiste.add(...);
    return menuLeiste;
  }
}
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • 1
    Is there a reason why you are mixing AWT and Swing components? Also, if stuff like the menu bar is irrelevant for the question, please remove it - ideally, provide a [mcve] – Andreas Fester Dec 15 '15 at 09:50

2 Answers2

2

... it opens two different windows, how can I fix that?

The reason is that you are creating two distinct main windows, one JFrame (note that your MenuSample class is a JFrame) and one additional Frame. Either use the MenuSample itself as the top level JFrame or delegate to a separate one - from the comments below, my assumption is that you want to use the delegation approach, so simply do

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MenuSample {

    private JFrame testFrame = new JFrame("Test Frame");
    private JButton button = new JButton("erster Button");
    private JPanel panel = new JPanel(new GridLayout(1, 1)) {
        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 300);
        }
    };

    public MenuSample() {
        panel.add(button);

        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        testFrame.add(panel);
        testFrame.pack();
        testFrame.setLocation(400, 300);
        testFrame.setVisible(true);
    }

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

Notes:

  • Notice the way the UI is launched in the main() method. This is to avoid concurrency issues (see Best practice to start a swing application)

  • Instead of adding a WindowListener, simply set the defaultCloseOperation of JFrame to EXIT_ON_CLOSE to make sure the application terminates when the JFrame is closed. See JFrame Exit on close Java.

  • If this is a new application, you might consider JavaFX instead of Swing.

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Thanks for the answer, but I am trying to integrate the content of the panel into my frame. Your methode simply deletes my frame and the menubar. And correct me if I am wrong, but arent panels the equivalent of 'div' that are used in css to divide the frame into different panels? –  Dec 15 '15 at 09:56
  • Well, you had two Frames in any case - you need to pick one of the alternatives, either inherit from JFrame or delegate to JFrame. You did both (and, additionally, one of them was an AWT Frame) so you ended up with two top level windows. That is the reason I deleted one of them, but I have updated my answer to show the alternative (also note how to launch a Swing application in order to avoid concurrency issues). I deleted the menu bar since it is irrelevant for the issue. If that is a new application, you might consider JavaFX instead of Swing. – Andreas Fester Dec 15 '15 at 10:05
  • And, yes, both `div` in HTML (not CSS, btw) and `JPanel` are containers which can be used to layout child components. – Andreas Fester Dec 15 '15 at 10:09
1

You are already adding correctly your new panel inside a Frame. Your problem does not lie there. The reason you have 2 windows is because you are creating 2 frames:

MenuSample extends JFrame

Creating a MenuSample will create a Swing window since it is a JFrame.

Frame testFrame = new Frame("Test Frame");

Creating a new Frame(...) will create an AWT window.

To create only one Window you either need to:

  • remove "extends JFrame" and add everything to testFrame

    or

  • remove Frame testFrame = new Frame("Test Frame"); and add everything to this

Raphaël
  • 3,646
  • 27
  • 28