本文介绍了BorderLayout setSize问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在您提出GridBagLayout建议之前,我已经尝试过了,但是无法正常工作.我想要一个尺寸为800 x 800且中心面板为600x600的框架.现在,当我运行它时,中央面板为600x578.有人可以告诉我哪里出了问题吗?只有22像素.

Before you come with GridBagLayout suggestions, I've tried that but I couldn't get it to work.I want a frame with the size 800 x 800 and with a center panel of 600x600. Right now, when I run it the center panel is 600x578. Can someone tell me where it goes wrong? It's just 22 pixels.

public void createPlayground()
{
    JFrame frame = new JFrame("ForFun Maze");
    frame.setSize(WIDTH, HEIGHT);
    frame.setResizable(false);
    frame.setLayout(new BorderLayout());


    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(3,1));
    buttonPanel.setPreferredSize(new Dimension(100,600));
    buttonPanel.setMaximumSize(new Dimension(100,600));
    buttonPanel.setBackground(SIDEBAR);

    JButton reset = new JButton();
    reset.setText("Reset");
    reset.addActionListener(new RestartListener());
    reset.setSize(100,180);
    JButton pause = new JButton();
    pause.setText("Pause");
    pause.setSize(100,180);
    pause.addActionListener(new PauseListener());
    JButton quit = new JButton();
    quit.setText("Quit");
    quit.setSize(100,180);
    quit.addActionListener(new QuitListener());

    buttonPanel.add(pause);
    buttonPanel.add(reset);
    buttonPanel.add(quit);

    Location[][] array = null;
    if(level == 1)
    {
        array = glevel1;
    }
    CenterPanel centerPanel = new CenterPanel(array);
    centerPanel.setPreferredSize(new Dimension(600,600));
    centerPanel.setMinimumSize(new Dimension(600,600));
    centerPanel.setBackground(BACKGROUND);

    JPanel leftPanel = new JPanel();
    leftPanel.setBackground(SIDEBAR);
    leftPanel.setPreferredSize(new Dimension(100,600));
    JPanel northPanel = new JPanel();
    northPanel.setBackground(SIDEBAR);
    northPanel.setPreferredSize(new Dimension(800,100));
    JPanel bottomPanel = new JPanel();
    bottomPanel.setBackground(SIDEBAR);
    bottomPanel.setPreferredSize(new Dimension(800,100));

    frame.add(northPanel, BorderLayout.NORTH);
    frame.add(leftPanel, BorderLayout.WEST);
    frame.add(centerPanel, BorderLayout.CENTER);
    frame.add(buttonPanel, BorderLayout.EAST);
    frame.add(bottomPanel, BorderLayout.SOUTH);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    System.out.println("Size of centerpane"+centerPanel.getWidth()+"x"+centerPanel.getHeight());
}

推荐答案

  • setSize()上使用pack()(因为您没有考虑JFrame insets
  • 使用setLocationRelativeTo(null)(在调用pack()之后)将框架居中.
  • 您对JComponent上的setSize()的调用没有用,因为LayoutManager会覆盖它们(setBoundssetLocation相同).
    • Use pack() over setSize() (because you don't take the JFrame insets into account
    • Use setLocationRelativeTo(null) (after calling pack()) to center the frame.
    • Your calls to setSize() on JComponent's are useless because the LayoutManager's will override them (same goes for setBounds and setLocation).
    • import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.GridLayout;
      
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.SwingUtilities;
      import javax.xml.stream.Location;
      
      public class Example {
      
          public void createPlayground() {
              JFrame frame = new JFrame("ForFun Maze");
              frame.setResizable(false);
              frame.setLayout(new BorderLayout());
      
              JPanel buttonPanel = new JPanel();
              buttonPanel.setLayout(new GridLayout(3, 1));
              buttonPanel.setPreferredSize(new Dimension(100, 600));
              buttonPanel.setMaximumSize(new Dimension(100, 600));
      
              JButton reset = new JButton();
              reset.setText("Reset");
              reset.setSize(100, 180);
              JButton pause = new JButton();
              pause.setText("Pause");
              pause.setSize(100, 180);
              JButton quit = new JButton();
              quit.setText("Quit");
              quit.setSize(100, 180);
      
              buttonPanel.add(pause);
              buttonPanel.add(reset);
              buttonPanel.add(quit);
      
              Location[][] array = null;
              JPanel centerPanel = new JPanel();
              centerPanel.setPreferredSize(new Dimension(600, 600));
              centerPanel.setMinimumSize(new Dimension(600, 600));
      
              JPanel leftPanel = new JPanel();
              leftPanel.setPreferredSize(new Dimension(100, 600));
              JPanel northPanel = new JPanel();
              northPanel.setPreferredSize(new Dimension(800, 100));
              JPanel bottomPanel = new JPanel();
              bottomPanel.setPreferredSize(new Dimension(800, 100));
      
              frame.add(northPanel, BorderLayout.NORTH);
              frame.add(leftPanel, BorderLayout.WEST);
              frame.add(centerPanel, BorderLayout.CENTER);
              frame.add(buttonPanel, BorderLayout.EAST);
              frame.add(bottomPanel, BorderLayout.SOUTH);
      
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
              System.out.println("Size of centerpane" + centerPanel.getWidth() + "x" + centerPanel.getHeight());
          }
      
          public static void main(String[] args) {
              SwingUtilities.invokeLater(new Runnable() {
      
                  @Override
                  public void run() {
                      new Example().createPlayground();
                  }
              });
          }
      }
      

      这篇关于BorderLayout setSize问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 17:00