JPanel没有正确显示

时间:2013-06-18 09:01:15

标签: java swing jpanel layout-manager

因此,我从基本的可视Java组件开始,并且无法准确地使用它们。

这是我的新问题:我正在尝试实施3部件面板:东部,中部,西部,但无法正确显示中央面板。这是我的代码:

基本上'panelUpMiddle'不可见,所以我想知道为什么?...

public class TestCode_Web {


public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(400, 400);

    JPanel innerPanel = new JPanel(new BorderLayout());     
    JPanel panelUp = new JPanel(new BorderLayout());

    JPanel panelUpLeft = new JPanel();
    JPanel panelUpMiddle = new JPanel();

    window.add(innerPanel, BorderLayout.NORTH);     
    innerPanel.add(panelUp, BorderLayout.NORTH);

    panelUp.add(panelUpLeft, BorderLayout.WEST);
    panelUp.add(panelUpMiddle, BorderLayout.CENTER);

    JLabel label1 = new JLabel("Label 1");

    JLabel label11 = new JLabel("Label 11");
    JLabel label12 = new JLabel("Label 12");

    panelUp.add(label1);
    panelUpLeft.add(label11);       
    panelUpMiddle.add(label12);     

    panelUp.setBackground(new Color(200, 240, 200));

    panelUpLeft.setBackground(new Color(200, 240, 0));
    panelUpMiddle.setBackground(new Color(100, 240, 200));

    panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));        
    panelUpLeft.setPreferredSize(new Dimension(160, 120));

    window.setVisible(true);
}

}

3 个答案:

答案 0 :(得分:3)

试试这段代码。我更新了面板和标签的命名,因此更加清晰。

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(400, 400);

    JPanel centerPanel = new JPanel(new BorderLayout());     
    JPanel eastPanel = new JPanel(new BorderLayout());
    JPanel westPanel = new JPanel(new BorderLayout());

    window.add(centerPanel, BorderLayout.CENTER);     
    window.add(eastPanel, BorderLayout.EAST);
    window.add(westPanel, BorderLayout.WEST);

    JLabel centerLabel = new JLabel("Center");
    JLabel eastLabel = new JLabel("East");
    JLabel westLabel = new JLabel("West");

    eastPanel.add(eastLabel);
    westPanel.add(westLabel);       
    centerPanel.add(centerLabel);

    centerPanel.setPreferredSize(new Dimension(200, 400));
    eastPanel.setPreferredSize(new Dimension(100, 400));
    westPanel.setPreferredSize(new Dimension(100, 400));
    eastPanel.setBackground(new Color(200, 240, 200));
    westPanel.setBackground(new Color(200, 240, 0));

    window.setVisible(true);

答案 1 :(得分:2)

永远不会创建第三个面板并嵌套在另一个面板中。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestCode_Web {


public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(400, 400);

    JPanel innerPanel = new JPanel(new BorderLayout());     
    JPanel panelUp = new JPanel(new BorderLayout());

    JPanel panelUpLeft = new JPanel();
    JPanel panelUpMiddle = new JPanel();
    JPanel panelUpRight = new JPanel();  //Create third panel

    window.add(innerPanel, BorderLayout.NORTH);     
    innerPanel.add(panelUp, BorderLayout.NORTH);

    panelUp.add(panelUpRight, BorderLayout.EAST); //Add Third Panel
    panelUp.add(panelUpLeft, BorderLayout.WEST);
    panelUp.add(panelUpMiddle, BorderLayout.CENTER);

    JLabel label1 = new JLabel("Label 1");

    JLabel label11 = new JLabel("Label 11");
    JLabel label12 = new JLabel("Label 12");

    panelUpRight.add(label1); //Add label for third panel
    panelUpLeft.add(label11);       
    panelUpMiddle.add(label12);     

    panelUp.setBackground(new Color(200, 240, 200));

    panelUpLeft.setBackground(new Color(200, 240, 0));
    panelUpMiddle.setBackground(new Color(100, 240, 200));

    panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));        
    panelUpLeft.setPreferredSize(new Dimension(160, 120));

    window.setVisible(true);
}

}

答案 2 :(得分:1)

实际上,您不需要innerPanel

public class TestCode_Web
{
    public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLayout(new BorderLayout());
    window.setSize(400, 400);

    JPanel panelUp = new JPanel();
    JPanel panelUpLeft = new JPanel();
    JPanel panelUpMiddle = new JPanel();

    window.add(panelUp, BorderLayout.NORTH);     
    window.add(panelUpLeft, BorderLayout.WEST);
    window.add(panelUpMiddle, BorderLayout.CENTER);

    JLabel label1 = new JLabel("Label 1");
    JLabel label11 = new JLabel("Label 11");
    JLabel label12 = new JLabel("Label 12");

    panelUp.add(label1);
    panelUpLeft.add(label11);       
    panelUpMiddle.add(label12);     

    panelUp.setBackground(new Color(200, 240, 200));
    panelUpLeft.setBackground(new Color(200, 240, 0));
    panelUpMiddle.setBackground(new Color(100, 240, 200));

    panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));        
    panelUpLeft.setPreferredSize(new Dimension(160, 120));

    window.setVisible(true);
}
}

NiceCow给出了为什么你的不工作的答案。这应该可以正常工作。