在 borderlayout 中使用 JPanel 错误打包 JFrame

发布于 2024-08-08 09:06:59 字数 1944 浏览 1 评论 0原文

我正在用 Java 编写一个简单的应用程序,对一群羊进行一些粒子模拟(不要问)。为此,我想要一个带有用于图形的 JPanel 的窗口(可以通过包含一些标准分辨率的简单组合框来调整大小)和一些其他元素,例如用于启动和暂停模拟的按钮等。

我的问题:我正在使用 JFrame。 pack 方法使用 borderLayout 将所有内容很好地打包在一起。但由于某种原因,JPanel 打包错误,似乎打包忽略了它,因此调整窗口大小以适合我现在拥有的两个按钮的大小。我做错了什么?

这是到目前为止的代码(有点新手,所以如果有的话,不要对我的愚蠢发表评论;)):

public class Window {
 public Sheepness sheepness;

 public ButtonPanel buttonPanel;
 public PaintPanel paintPanel;
 public JFrame frame;

 public Window(Sheepness sheepness, int width, int height) {
  this.sheepness = sheepness;

  frame = new JFrame("Sheepness simulation");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //frame.setSize(width, height);

  BorderLayout frameLayout = new BorderLayout();
  JPanel background = new JPanel(frameLayout);
  background.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

  buttonPanel = new ButtonPanel(this);
  background.add(BorderLayout.SOUTH, buttonPanel.buttonBox);

  paintPanel = new PaintPanel(this);
  paintPanel.setSize(600, 600);
  background.add(BorderLayout.CENTER, paintPanel);

  frame.getContentPane().add(background);
  frame.pack();
  frame.setResizable(false);
  frame.setVisible(true);
 }
}

public class PaintPanel extends JPanel {
 public Window window;

 public PaintPanel(Window window) {
  this.window = window;
 }

 @Override
 public void paintComponent(Graphics g) {
  g.setColor(Color.blue);
  g.fillRect(0, 0, 300, 200);
 }
}

public class ButtonPanel {
 public Window window;
 public Box buttonBox;

 public JButton startButton;
 public JButton resetButton;

 public ButtonPanel(Window window) {
  this.window = window;

  buttonBox = new Box(BoxLayout.X_AXIS);

  startButton = new JButton("Start");
  startButton.addActionListener(new startButtonListener());
  buttonBox.add(startButton);

  resetButton = new JButton("Reset");
  resetButton.addActionListener(new resetButtonListener());
  buttonBox.add(resetButton);
 }
}

I am writing a simple application in Java that does some particle simulation on a bunch of sheep (don't ask). For this I want a window with a JPanel for graphics (which will be resizable with a simple combobox that contains some standard resolutions) and some other elements like buttons to start and pause the simulation etc.

My question: I'm using the JFrame.pack method to pack everything nicely together using a borderLayout. But for some reason the JPanel is packed wrong, it seems like the packing ignores it, so the window is resized to fit the size of only the two buttons that I have now. What am I doing wrong?

This is the code so far (bit of a newbie, so no comments on my dumbness if there is any ;)):

public class Window {
 public Sheepness sheepness;

 public ButtonPanel buttonPanel;
 public PaintPanel paintPanel;
 public JFrame frame;

 public Window(Sheepness sheepness, int width, int height) {
  this.sheepness = sheepness;

  frame = new JFrame("Sheepness simulation");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //frame.setSize(width, height);

  BorderLayout frameLayout = new BorderLayout();
  JPanel background = new JPanel(frameLayout);
  background.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

  buttonPanel = new ButtonPanel(this);
  background.add(BorderLayout.SOUTH, buttonPanel.buttonBox);

  paintPanel = new PaintPanel(this);
  paintPanel.setSize(600, 600);
  background.add(BorderLayout.CENTER, paintPanel);

  frame.getContentPane().add(background);
  frame.pack();
  frame.setResizable(false);
  frame.setVisible(true);
 }
}

public class PaintPanel extends JPanel {
 public Window window;

 public PaintPanel(Window window) {
  this.window = window;
 }

 @Override
 public void paintComponent(Graphics g) {
  g.setColor(Color.blue);
  g.fillRect(0, 0, 300, 200);
 }
}

public class ButtonPanel {
 public Window window;
 public Box buttonBox;

 public JButton startButton;
 public JButton resetButton;

 public ButtonPanel(Window window) {
  this.window = window;

  buttonBox = new Box(BoxLayout.X_AXIS);

  startButton = new JButton("Start");
  startButton.addActionListener(new startButtonListener());
  buttonBox.add(startButton);

  resetButton = new JButton("Reset");
  resetButton.addActionListener(new resetButtonListener());
  buttonBox.add(resetButton);
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自我难过 2024-08-15 09:06:59

尝试:

paintPanel.setPreferredSize(600, 600);

Window.pack() 调整其子组件的首选大小时,JPanel 从其子组件中获取其首选大小(在您的情况下没有)。

Try:

paintPanel.setPreferredSize(600, 600);

As Window.pack() sizes to the preferred sizes of its subcomponents and JPanel gets its preferred size from its child components (in your case there are none).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文