user5681278
user5681278

Reputation:

JAVA - add a panel into a Frame

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;
  }
}

Upvotes: 0

Views: 6034

Answers (2)

Andreas Fester
Andreas Fester

Reputation: 36630

... 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.

Upvotes: 2

Raphaël
Raphaël

Reputation: 3726

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

Upvotes: 1

Related Questions