java JPanel 周围不需要的边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1593683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:13:04  来源:igfitidea点击:

Unwanted border around JPanel

javaswingsizeborderjpanel

提问by FinalArt2005

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?

我正在创建一个带有 JPanel 的表单,用于一些图形和一些用于控制事物的按钮。出于某种原因,我必须将 JPanel 指定为比我想要放入其中的实际图形宽 10 像素和高 30 像素。是什么导致了这个问题?

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:

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

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

找不到源图像 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.

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

Screenshot with a preferredSize of 310 x 210:

优选尺寸为 310 x 210 的屏幕截图:

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

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

Now it fits the 320 x 240 fillRect exactly!

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

Any ideas?

有任何想法吗?

回答by denis.zhdanov

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.

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

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

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

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

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

回答by user85421

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

信不信由你,但尝试颠倒 pack() 和 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());
}

[]]

[]]

回答by uckelman

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.

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

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.

您的问题要么是由于按钮容器迫使窗口变宽,要么是由于您使用的 Java 版本中的 Swing 错误。

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

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

回答by jitter

I guess you got the label on the screenshots wrong.

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

If you specify

如果您指定

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

in PaintPanel and

在 PaintPanel 和

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.

在窗口。你得到了第一个屏幕截图显示的内容。这是有道理的,因为矩形的宽度减少了 10 像素,高度减少了 30 像素。

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

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