Domanda

Normally adding a customized JPanel with a specific size to another JPanel (container) without a specified size works like a charm. You can't see the container at all. In this example, the red visible border is actually the background color of my container. The blue is the border of the container. Why do the red area appear/why don't it normally appear? I'm pretty sure that:

Jpanel panel = new JPanel;
panel.setBackground(new Color(Color.BLACK));
JPanel panel2 = new JPanel;
panel2.setBackground(new Color(Color.RED));
panel2.setPrefferedSize(new Dimension(200,200));
panel.add(panel2);

will be a fully red window, no black border visible. I can't see what I do very different?

enter image description here

3 classes to run the code:

public class Center extends JPanel {

JPanel centerFrame = new JPanel();

public Center() {
    setLayout(new BorderLayout());
    centerFrame.setBackground(Color.RED);
    centerFrame.setBorder(new LineBorder(Color.BLUE, 6));
    centerFrame.add(panel1());
    add(centerFrame, BorderLayout.CENTER);
    add(new Buttons(), BorderLayout.PAGE_END);
}

public JPanel panel1() {
    JPanel pane = new JPanel(new BorderLayout());
    JPanel content = new JPanel();
    content.setPreferredSize(new Dimension(400,200));
    pane.add(content, BorderLayout.CENTER);
    return pane;
}
}

public class Buttons extends JPanel {

public Buttons() {
    setLayout(new GridLayout(2, 3));
    add(new JButton("Button 1"));
    add(new JButton("Button 2"));
    add(new JButton("Button 3"));
    add(new JButton("Button 4"));
    add(new JButton("Button 5"));
    add(new JButton("Button 6"));
}
}

public class Run extends JFrame {

public Run() {
    add(new TestClass());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
}

public static void main(String[] _) {
    new Run();
}
}
È stato utile?

Soluzione

The problem is that JPanels come with a default FlowLayout that in turn come equipped with default 5 pixel gaps. If you change the gaps to 0, you won't see it

centerFrame.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

-Or-

centerFrame.setBorder(new LineBorder(Color.BLUE, 6));
FlowLayout flow = (FlowLayout)centerFrame.getLayout();
flow.setHgap(0);
flow.setVgap(0);

See the FlowLayout API

Altri suggerimenti

Yours is almost a minimal example program. To truly comply, it must compile (yours doesn't -- new TestClass()?), and it should be in one file with imports. This for example is closer:

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

public class Run extends JFrame {

   public Run() {
      // !!?? add(new TestClass());
      add(new Center());  // !! this is better
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
      setLocationRelativeTo(null);
      setVisible(true);
   }

   public static void main(String[] _) {
      new Run();
   }
}

class Buttons extends JPanel {

   public Buttons() {
      setLayout(new GridLayout(2, 3));
      add(new JButton("Button 1"));
      add(new JButton("Button 2"));
      add(new JButton("Button 3"));
      add(new JButton("Button 4"));
      add(new JButton("Button 5"));
      add(new JButton("Button 6"));
   }
}

class Center extends JPanel {

   JPanel centerFrame = new JPanel();

   public Center() {
      setLayout(new BorderLayout());
      centerFrame.setBackground(Color.RED);
      centerFrame.setBorder(new LineBorder(Color.BLUE, 6));
      centerFrame.add(panel1());
      add(centerFrame, BorderLayout.CENTER);
      add(new Buttons(), BorderLayout.PAGE_END);

      System.out.println(centerFrame.getLayout());  // !! hm, this may be important
   }

   public JPanel panel1() {
      JPanel pane = new JPanel(new BorderLayout());
      JPanel content = new JPanel();
      content.setPreferredSize(new Dimension(400, 200));
      pane.add(content, BorderLayout.CENTER);
      return pane;
   }
}

But more importantly, if you run this program, you'll see that I've added a line of code which will answer your question for you. :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top