JPanel在框架中间

时间:2013-12-13 02:02:19

标签: java swing layout centering gridbaglayout

我正试图将我的jpanel放在框架的中间......所以当用户试图改变窗框的大小时,它仍然在中间...... 这样的事情:
the red box is my panel

所以如果我改变大小,它必须保持在中间:
if you change the size of the frame it remains in the middle

我尝试使用BorderLayout更改contentPane的布局并将我的jpanel放在中心位置...但是当我更改框架的大小时,面板会进入左上角。
在我的Windows构建器中,情况如下:
my situation 我的jpanel必须正确地工作如何工作redbox。我尝试了一切,但结果是每次都相同:
enter image description here 这是我的代码:

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

1 个答案:

答案 0 :(得分:4)

有几种方法可以做到这一点,但最简单的方法之一就是给contentPane一个GridBagLayout,然后添加你感兴趣的JPanel而没有GridBagConstraints。如果感兴趣的JPanel是唯一添加到此容器的内容,则会将该JPanel置于中心位置。

如,

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