Unwanted border around JPanel

I am creating a form with a JPanel inside it for some graphic elements and some buttons for managing the thing. For some reason, I have to specify the JPanel 10 pixels less than the width and 30 pixels less than the actual graphics that I want to place 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 preferred 320 x 240 size:

Cannot find the original image http://www.cmbi.ru.nl/~estens/Sheepness/Sheepness_simulation_border.png .

You can see that 320 x 240 fillRect does not completely fill the JPanel, there remains a border of width 10 px and height 30 px.

Screenshot with preferred size 310 x 210:

http://www.cmbi.ru.nl/~estens/Sheepness/Sheepness_simulation_noBorder.png.

320 x 240 fillRect!

?

+3
4

, .. , , "setPreferredSize()" .

javadoc .

, setBounds().

+3

, pack() setResizeable()

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

EDIT:

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

[]]

+2

, :

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

JVM IcedTea Linux, this.

, , , , - Swing Java.

MigLayout.

+1

I think you incorrectly stuck a shortcut on the screenshots.

If you specify

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

in paintpanel and

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

in the window. You will get what the first screenshot shows. This makes sense since the rectangle is less than 10px wide and 30 pixels smaller.

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

0
source

Source: https://habr.com/ru/post/1720552/


All Articles