gpt4 book ai didi

java - BorderLayout setSize 问题

转载 作者:行者123 更新时间:2023-11-29 05:42:24 28 4
gpt4 key购买 nike

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

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

最佳答案

  • 使用 pack() 而不是 setSize()(因为您没有考虑 JFrame 插入
  • 使用 setLocationRelativeTo(null)(在调用 pack() 之后)使框架居中。
  • 您在 JComponent 上调用 setSize() 是无用的,因为 LayoutManager 会覆盖它们(同样适用于 setBoundssetLocation)。

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

关于java - BorderLayout setSize 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17027350/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com