Java JFrame方法包()

时间:2010-03-14 00:56:49

标签: java user-interface jframe jpanel packaging

我有一个包含4个JPanels和1个JScrollPane的框架,这4个面板的边界布局是北,东,南,西和中心的滚动窗格。

我一直在尝试获取框架功能的pack方法,但是当你运行时,你只需要获得窗口的标题栏。

任何想法?

    JFrame conFrame;
    JPanel panel1;
    JPanel panel2;
    JPanel panel3;
    JPanel panel4;
    JScrollPane listPane;
    JList list;
    Object namesAr[];
    ...
    ...
    ...
    namesAr= namesA.toArray();
    list = new JList(namesAr); 
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    list.setVisibleRowCount(-3);
    list.addListSelectionListener(this);

    listPane = new JScrollPane(list);

    panel1 = new JPanel();
    panel2 = new JPanel();
    panel3 = new JPanel();
    panel4 = new JPanel();

    conFrame.setLayout(new BorderLayout());

    panel1.setPreferredSize(new Dimension(100, 100));
    panel2.setPreferredSize(new Dimension(100, 100));
    panel3.setPreferredSize(new Dimension(100, 100));
    panel4.setPreferredSize(new Dimension(100, 100));

    panel1.setBackground(Color.red);
    panel2.setBackground(Color.red);
    panel3.setBackground(Color.red);
    panel4.setBackground(Color.red);

    conFrame.pack();

    conFrame.add(panel1, BorderLayout.NORTH);
    conFrame.add(panel2, BorderLayout.EAST);
    conFrame.add(panel3, BorderLayout.SOUTH);
    conFrame.add(panel4, BorderLayout.WEST);
    conFrame.add(listPane, BorderLayout.CENTER);
    conFrame.setVisible(true);

1 个答案:

答案 0 :(得分:2)

您需要在执行pack()之前将面板添加到框架中,否则无需打包。

此外,框架的默认布局是BorderLayout。