Question

I'm trying to put my jpanel in the middle of the frame... so when the user try to change the size of the window frame, it remain in the middle... something like this:
the red box is my panel

so if i change size it have to remain in the middle:
if you change the size of the frame it remains in the middle

i Tried to change the layout of my contentPane with the BorderLayout and put my jpanel in the center position... but when i change size of my frame the panel go in the top left corner.
In my windows builder the situation is this:
my situation my jpanel have to work exactly how works the redbox. i tried everything but the result is everytime the same:
enter image description here this is my code:

    package StudApp;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

    public class StudApp {
        private JFrame frame;
        private JPanel homeFirstRun;
        private ArrayList<Corso> corsi = new ArrayList<Corso>();
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new StudApp();
                }
            });
        }

        /**
         * Create the frame.
         */
        public StudApp() {
            frame = new JFrame("Student Note");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setBounds(100, 100, 450, 300);

            JMenuBar menuBar = new JMenuBar();
            frame.setJMenuBar(menuBar);

            JMenu menuHelp = new JMenu("Help");
            menuBar.add(menuHelp);

            JMenuItem menuIstrStud = new JMenuItem("Intructions Student Note");
            menuIstrStud.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    frame.remove(homeFirstRun);
                    frame.revalidate();
                    frame.repaint();
                    homeFirstRun = null;
                }
            });
            menuHelp.add(menuIstrStud);
            homeFirstRun = new JPanel();
            homeFirstRun.setBorder(new EmptyBorder(5, 5, 5, 5));
            homeFirstRun.setLayout(null);
            frame.getContentPane().add(homeFirstRun);


            JLabel welcomeMessage = new JLabel("Welcome to Student Note");
            welcomeMessage.setBounds(5, 5, 424, 18);
            welcomeMessage.setForeground(Color.DARK_GRAY);
            welcomeMessage.setHorizontalAlignment(SwingConstants.CENTER);
            welcomeMessage.setFont(new Font("Verdana", Font.BOLD, 14));
            homeFirstRun.add(welcomeMessage);



            JTextArea welcomeTextArea = new JTextArea();
            welcomeTextArea.setFont(new Font("Verdana", Font.PLAIN, 13));
            welcomeTextArea.setText(" I think it's your first time here.\n\n"
                                + " So the first step is to create a new course to\n insert your grades.\n\n"
                                + " If you want my advice, read how this program\n works in the help section (it is very simple),\n "
                                + "just 2 minutes ... believe me");
            welcomeTextArea.setEditable(false);
            welcomeTextArea.setBounds(27, 34, 381, 184);
            homeFirstRun.add(welcomeTextArea);

            frame.setVisible(true);
        }
    }
Was it helpful?

Solution

There are several ways to do this, but one of the easiest is to give the contentPane a GridBagLayout, and then add your JPanel of interest with no GridBagConstraints. If the JPanel of interest is the only thing added to this container, this will then place that JPanel into a central position.

e.g.,

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

public class CentralPanel {

   private static void createAndShowGui() {
      JFrame frame = new JFrame("CentralPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setContentPane(new MyContentPane());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

class MyContentPane extends JPanel {
   private static final int PREF_W = 700;
   private static final int PREF_H = 550;

   public MyContentPane() {
      setLayout(new GridBagLayout());
      add(new JPanelOfInterest());
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }
}

class JPanelOfInterest extends JPanel {
   private static final int PREF_W = 400;
   private static final int PREF_H = PREF_W;

   public JPanelOfInterest() {
      setBorder(BorderFactory.createTitledBorder("JPanel of Interest"));
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top