简体   繁体   English

无法在JFrame中添加多个JPanel,

[英]Cannot add multiple JPanel in JFrame,

I have a following JFrame. 我有一个以下的JFrame。

public class AddActivityWindow extends JFrame {

    //declaration of components go here
     ...
    public AddActivityWindow(ActivityStore store) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel pnl_date = new JPanel();
        JLabel lbl_date = new JLabel("X");
        pnl_date.add(lbl_date);
        add(pnl_date);


        pnl_activities = new JPanel();
        JLabel lbl_act = new JLabel("Y");
        pnl_activities.add(lbl_act);
        add(pnl_activities);

        setLocationRelativeTo(null);
        setVisible(true);

    }
}

When I create this Frame I was expecting that it would create a frame with two panels. 当我创建这个框架时,我期待它会创建一个带有两个面板的框架。 But, I only see the second panel (ie) I only see Y on the screen and not X . 但是,我只看到第二个面板(即)我只在屏幕上看到Y而不是X However, if I remove the code for the second panel, then I can see X . 但是,如果我删除第二个面板的代码,那么我可以看到X

What happens when I add the second panel. 添加第二个面板时会发生什么。 Why does the first panel not show up in the frame? 为什么第一个面板没有显示在框架中?

The default layout of a JFrame is a BorderLayout . JFrame的默认布局是BorderLayout A characteristic of the BorderLayout is that it can only contain one component in each region. BorderLayout一个特征是它只能在每个区域中包含一个组件。 If you just use the add method without specifying any constraints, it is the same as adding the component to the CENTER . 如果只使用add方法而不指定任何约束,则与将组件添加到CENTER So your second add call replaces the element you first added. 因此,您的第二个add调用将替换您首次添加的元素。

Possible solutions: specify constraints, eg 可能的解决方案:指定约束,例如

add(pnl_date, BorderLayout.NORTH);
//...
add(pnl_activities, BorderLayout.CENTER);

or switch to another layout manager (eg a BoxLayout or a FlowLayout ) for your JFrame . 或切换到JFrame另一个布局管理器(例如BoxLayoutFlowLayout )。 It depends on your needs. 这取决于您的需求。 Take a look at the Visual guide to layout managers which is a good starting point for choosing a manager. 看一下布局管理器可视化指南,这是选择经理的一个很好的起点。

You should be using a different Layout Manager, Have a look at : A Visual Guide to Layout Managers to select one for you. 您应该使用不同的布局管理器,请查看:布局管理器的可视指南,为您选择一个。

This works fine for me , 这对我很好,

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FrameTest extends JFrame {

    public FrameTest() {
        setLayout(new FlowLayout());
        setSize(150, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel pnl_date = new JPanel();
        JLabel lbl_date = new JLabel("X");
        pnl_date.add(lbl_date);
        add(pnl_date);


        JPanel pnl_activities = new JPanel();
        JLabel lbl_act = new JLabel("Y");
        pnl_activities.add(lbl_act);
        add(pnl_activities);

        setLocationRelativeTo(null);
        setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FrameTest().setVisible(true);

            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM