JPanel 周围不需要的边框

发布于 2024-08-08 02:51:41 字数 1883 浏览 5 评论 0原文

我正在创建一个表单,其中包含一个 JPanel,用于显示一些图形和一些用于控制该事物的按钮。由于某种原因,我必须将 JPanel 指定为比我想要放入其中的实际图形少 10 px 宽和 30 px 高。是什么原因导致这个问题呢?

这是代码:

public class Window {
 public Sheepness sheepness;

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

 public Window(Sheepness sheepness) {
  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(5, 5, 5, 5));

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

  paintPanel = new PaintPanel(this);
  paintPanel.setPreferredSize(new Dimension(320, 240));
  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, 320, 240);
 }
}

首选尺寸为 320 x 240 的屏幕截图:

不能查找源图像http://www.cmbi.ru.nl/~estens/Sheepness/Sheepness_simulation_border.png

您可以看到 320 x 240 fillRect 没有完全填充 JPanel,保留了 10 px 宽度和 30 px 高的边框。

PreferredSize 为 310 x 210 的屏幕截图:

找不到源图像 http: //www.cmbi.ru.nl/~estens/Sheepness/Sheepness_simulation_noBorder.png

现在它完全适合 320 x 240 fillRect!

有什么想法吗?

I'm creating a form with a JPanel inside it for some graphics and some buttons for controlling the thing. For some reason I have to specify the JPanel to be 10 px less wide and 30 px less high than the actual graphics I want to put inside it. What causes this problem?

This is the code:

public class Window {
 public Sheepness sheepness;

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

 public Window(Sheepness sheepness) {
  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(5, 5, 5, 5));

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

  paintPanel = new PaintPanel(this);
  paintPanel.setPreferredSize(new Dimension(320, 240));
  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, 320, 240);
 }
}

Screenshot with a preferredSize of 320 x 240:

Can't find source image http://www.cmbi.ru.nl/~estens/Sheepness/Sheepness_simulation_border.png.

You can see the 320 x 240 fillRect doesn't fill the JPanel entirely, a border of 10 px width and 30 px height remains.

Screenshot with a preferredSize of 310 x 210:

Can't find source image http://www.cmbi.ru.nl/~estens/Sheepness/Sheepness_simulation_noBorder.png.

Now it fits the 320 x 240 fillRect exactly!

Any ideas?

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

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

发布评论

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

评论(4

呆橘 2024-08-15 02:51:41

不同的布局管理器使用不同的规则来计算实际的托管控件大小,即,您不能期望只有在调用'setPreferredSize()'时面板才具有特定的大小。

请随意检查所有目标布局管理器的 javadoc,以获取有关每种特定情况下使用的算法的更多详细信息。

另请注意,您可以避免使用布局管理器并绝对通过'setBounds()'方法定义所有尺寸。

Different layout managers use different rules for computing actual managed controls sizes, i.e. you can't expect that the panel has particular size only if you call 'setPreferredSize()' on it.

Feel free to check javadoc for all target layout managers for more details about used algorithm in every particular case.

Also note that you can avoid using layout managers and define all sizes absolutely via 'setBounds()' method.

猥琐帝 2024-08-15 02:51:41

不管你相信与否,但尝试颠倒 pack() 和 setResizeable() 的顺序

    ...
    frame.setResizable(false);
    frame.pack();  // should be called after any changes
    frame.setVisible(true);

编辑:使用此检查

    frame.pack();
    frame.setResizable(false);
    System.out.println(paintPanel.getSize());
    frame.setVisible(true);
    System.out.println(paintPanel.getSize());

    frame.setResizable(false);
    frame.pack();
    System.out.println(paintPanel.getSize());
    frame.setVisible(true);
    System.out.println(paintPanel.getSize());

但如果大小不重要,您可以用

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

[]]填充实际区域

believe it or not, but try inverting the order of pack() and setResizeable()

    ...
    frame.setResizable(false);
    frame.pack();  // should be called after any changes
    frame.setVisible(true);

EDIT: checked using this

    frame.pack();
    frame.setResizable(false);
    System.out.println(paintPanel.getSize());
    frame.setVisible(true);
    System.out.println(paintPanel.getSize());

and

    frame.setResizable(false);
    frame.pack();
    System.out.println(paintPanel.getSize());
    frame.setVisible(true);
    System.out.println(paintPanel.getSize());

but if size isn't important, you can fill the actual area with

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

[]]

终难愈 2024-08-15 02:51:41

这是一个基于您的代码的独立测试用例:

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

public class Test {

 public PaintPanel paintPanel;
 public JFrame frame;

 public Test() {
  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(5, 5, 5, 5));

  paintPanel = new PaintPanel(this);
  paintPanel.setPreferredSize(new Dimension(320, 240));
  background.add(BorderLayout.CENTER, paintPanel);

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

  public static class PaintPanel extends JPanel {
    public Test window;

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

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

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

当我在 Linux 上的 IcedTea JVM 上运行它时,我看到 这个

您的问题要么是由于按钮容器迫使窗口变宽,要么可能是由于您正在使用的 Java 版本中的 Swing bug。

我的建议是不要理会内置布局类,而是使用 MigLayout。

Here's a self-contained test case, based on your code:

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

public class Test {

 public PaintPanel paintPanel;
 public JFrame frame;

 public Test() {
  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(5, 5, 5, 5));

  paintPanel = new PaintPanel(this);
  paintPanel.setPreferredSize(new Dimension(320, 240));
  background.add(BorderLayout.CENTER, paintPanel);

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

  public static class PaintPanel extends JPanel {
    public Test window;

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

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

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

When I run it on an IcedTea JVM on Linux, I see this.

Your problem is either due to the button container forcing the window to be wider, or possibly due to a Swing bug in the version of Java you're using.

My recommendation is not to bother with the built-in layout classes and to use MigLayout instead.

眼眸 2024-08-15 02:51:41

我猜你把截图上的标签弄错了。

如果您

g.fillRect(0, 0, 310, 210); 

在 PaintPanel 和

paintPanel.setPreferredSize(new Dimension(320, 240));

Window 中指定。您会看到第一个屏幕截图所示的内容。这是有道理的,因为矩形的宽度减少了 10 像素,高度减少了 30 像素。

如果您为两者设置相同的(宽度/高度;310、210),则蓝色矩形显然会“填充”PaintPanel。

I guess you got the label on the screenshots wrong.

If you specify

g.fillRect(0, 0, 310, 210); 

in PaintPanel and

paintPanel.setPreferredSize(new Dimension(320, 240));

in Window. You get that what the first screenshot shows. Which makes sense as the rectangle has 10px less width, and 30px less height.

If you set the same (width/height; 310, 210) for both, the blue rectangle obviously "fills "the PaintPanel out.

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