我的包含JPanel如何将所有空间分配给包含的组件?

时间:2014-03-28 17:56:03

标签: java swing jpanel containers

通常将具有特定大小的自定义JPanel添加到没有指定大小的另一个JPanel(容器)就像魅力一样。你根本看不到容器。在此示例中,红色可见边框实际上是我的容器的背景颜色。蓝色是容器的边界。为什么出现红色区域/为什么不出现? 我很确定:

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

将是一个完全红色的窗口,没有黑色边框可见。我看不出我做的非常不同?

enter image description here

运行代码的3个类:

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

2 个答案:

答案 0 :(得分:3)

你的几乎是一个最小的示例程序。要真正遵守,它必须编译(你的不是 - new TestClass()?),它应该在一个带有导入的文件中。例如,这更接近:

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

但更重要的是,如果你运行这个程序,你会发现我已经添加了一行代码来回答你的问题。 :)

答案 1 :(得分:3)

问题是JPanels带有默认FlowLayout,而默认的centerFrame.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); 则配备默认的5像素间隙。如果将间隙更改为0,则无法看到

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

-Or-

{{1}}

请参阅FlowLayout API